Bur*_*kay 5 android dart flutter flutter-dropdownbutton
所以我在我的应用程序中创建了一个 DropdownButton。问题是,每当我单击下拉菜单时,应用程序就会崩溃。我很困惑,因为当我在单击 DropdownButton 之前单击其他小部件(如 TextFormFields)时,它似乎工作正常。
错误信息:
'package:flutter/src/material/dropdown.dart': Failed assertion: line 581 pos 12: 'menuHeight == menuBottom - menuTop': is not true.
这是我的下拉按钮:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: DropDownTry(),
);
}
}
class DropDownTry extends StatefulWidget {
const DropDownTry({Key? key}) : super(key: key);
@override
_DropDownTryState createState() => _DropDownTryState();
}
class _DropDownTryState extends State<DropDownTry> {
String dropdownValue = 'Male';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: DropdownButton<String>(
value: dropdownValue,
icon: const Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
underline: SizedBox(),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: <String>['Male', 'Female']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
)),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
尝试下面的代码希望它能帮助您尝试删除SizedBox Widgetconst的关键字
为默认下拉值声明一个字符串变量
String? dropdownValue;
Run Code Online (Sandbox Code Playgroud)
您的下拉列表
List gender = [
'Male',
'Female',
'Other',
];
Run Code Online (Sandbox Code Playgroud)
您的下拉小部件
DropdownButtonHideUnderline(
child: DropdownButton(
hint: Text(
'Select Gender',
style: TextStyle(
color: Colors.black,
fontSize: 15,
),
textAlign: TextAlign.center,
),
value: dropdownValue,
onChanged: (String? genderNewValue) {
setState(
() {
dropdownValue = genderNewValue;
},
);
},
items: gender.map<DropdownMenuItem<String>>(
(value) {
return DropdownMenuItem<String>(
value: value,
child: Text(
value,
style: TextStyle(
fontSize: 15,
),
),
);
},
).toList(),
),
),
Run Code Online (Sandbox Code Playgroud)