我想在 DropdownButtonFormField 重置值,值更改为 null,但 DropdownButtonFormField 不会更改。问题出在哪儿?如果我使用了 DropdownButton,它会正确更改值,值被清除。我需要使用 DropdownButtonFormField。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
String abc;
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center (
child: Column(
children: <Widget>[DropdownButtonFormField(
hint: Text('select value'),
value: abc,
items: <String>['A', 'B', 'C', 'D'].map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (String newValue) {
setState(() {
abc = newValue; …Run Code Online (Sandbox Code Playgroud) 我正在学习 GetX 状态管理并偶然发现 DropdownButton 小部件。无法观察到如何使用所选值更新所选值。这是我的 DropdownButton 小部件
DropdownButton(
hint: Text(
'Book Type',
),
onChanged: (newValue) {
print(newValue);
},
value: selectedType,
items: bookController.listType.map((selectedType) {
return DropdownMenuItem(
child: new Text(
selectedType,
),
value: selectedType,
);
}).toList(),
),
Run Code Online (Sandbox Code Playgroud)
这
var selectedType;
Run Code Online (Sandbox Code Playgroud)
在小部件构建中声明。我试图使这个变量可观察,但布局会引发溢出错误。我也用 obx 包装了小部件,但它仍然抛出相同的错误。这个小部件是如何使用 GetX 实现的。我在这里拉头发。我可以使用 getX 处理其他小部件。
如何在颤动中更改 DropdownButton 的高度。我曾尝试使用 Padding 和 SizedBox,但没有一个真正起作用。
SizedBox 只是增加了容器的大小,而 DropdownButton 被固定在左上角,因此不再居中。填充被忽略或将内容移到按钮之外。
我不想更改下拉叠加层的大小,而是更改按钮本身。
build(BuildContext context) {
return ThemeData(
data: ThemeData(canvasColor: Colors.white),
child: DropdownButton(
items: _items.map((item) => DropdownMenuItem(child: Text(item), value: item)).toList(),
isExpanded: true,
selectedItemBuilder: (_) {
return _items.map<Widget>((String lang) {
return Center(
widthFactor: 1,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12),
child: Text(lang, style: TextStyle(color: Colors.black))
),
);
}).toList();
}
)
)
}
Run Code Online (Sandbox Code Playgroud) 有没有办法在执行 onTap 函数时关闭包含所有 DropdownMenuItems的DropdownButton的选择菜单(DropdownMenuItem 内的 GestureDetector)?
这是我对 Alperen Baskaya 方法的实现(稍微简化的版本,以便于理解)。然而,这种方法还不起作用,我不确定是因为我错误地实施了它,还是因为该方法不适用于我的问题。
class _BoatSelectionState extends State<BoatSelection> {
FocusNode focusNode;
@override
void initState() {
super.initState();
focusNode = FocusNode();
}
@override
Widget build(BuildContext context) {
return Row(
children: [
Expanded(
child:
DropdownButtonHideUnderline(
child: DropdownButton<Boat>(
focusNode: focusNode,
icon: Icon(
Icons.keyboard_arrow_down_rounded,
color: Colors.black,
),
isExpanded: true,
value: selectedBoat,
onChanged: (Boat _boat) => Provider.of<BoatStreamsCubit>(context, listen: false).setBoat(_boat),
selectedItemBuilder: (BuildContext context) {
return widget.boats.map<Widget>((Boat boat) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
BoatClassLogo(boat: boat),
Expanded(
child: …Run Code Online (Sandbox Code Playgroud) 我有一个正常工作的下拉按钮,但是当我尝试设置默认值时,它将失败,并显示以下错误:
'package:flutter / src / material / dropdown.dart':失败的断言:620行pos 15:'item == null || items.isEmpty || 值== null || items.where(((DropdownMenuItem item)=> item.value == value).length == 1':不正确。
这是我的下拉按钮:
Widget changeWorkspace() {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
Padding(
padding: EdgeInsets.all(8.0),
child: DropdownButton<AssignedWorkspace>(
isExpanded: true,
hint: Text("SELECT WORKSPACE"),
value: selectedWorkspace,
onChanged: (dropdownValueSelected) {
setState(() {
selectedWorkspace = dropdownValueSelected;
});
},
items: workspaces != null && workspaces.length > 0
? workspaces.map((AssignedWorkspace workspace) {
return new DropdownMenuItem<AssignedWorkspace>(
value: workspace, …Run Code Online (Sandbox Code Playgroud)