[英]App crashes when DropdownButton is clicked (Flutter)
所以我在我的应用程序中创建了一个 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(),
)),
),
);
}
}
尝试下面的代码希望它对您尝试删除SizedBox Widget 的const
关键字有所帮助
为默认下拉值声明一个字符串变量
String? dropdownValue;
你的下拉列表
List gender = [
'Male',
'Female',
'Other',
];
你的下拉小部件
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(),
),
),
将您的下拉代码封装在 SingleChildScrollView 中。
前任。
return Scaffold(
body: SingleChildScrollView(
child: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(),
)),
),
)
)
主要是不要让 DropDown 对顶部很粘。 它喜欢上面的一些空间。 此外,这是由于父小部件的布局错误造成的。 也许您已经创建了一个包含单个子项的列,而这个子项是一个堆栈,而崩溃的小部件位于堆栈内。 尝试使父小部件的布局更清晰。 此外,将屏幕的主要父级放在 Material Widget 中。 问题是因为框架无法计算超出菜单的高度。
我得到了同样的错误。 经过两天的挣扎,我发现问题出在两个因素上。 一个是我在 showModalBottomSheet 中使用了下拉菜单,第二个是我没有在 mydropdown 所在的脚手架中使用 appBar。当我找到包含我的下拉菜单的脚手架时,将其添加到另一个屏幕并添加 appBar。 它工作得很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.