flutter/dart 中的下拉按钮值文本未更改
Drop down button value text is not change in flutter/dart
我的代码有问题,我定义了一个下拉按钮菜单,它会显示参数,当我点击参数时,select 下拉菜单中的值不会改变按钮。加上我的代码是在有状态小部件中定义的。
这是我的代码:
String dropdownValue;
return DropdownButton<String>(
value: dropdownValue,
//icon: const Icon(Icons.arrow_downward,),
iconSize: 22,
elevation: 16,
style: TextStyle(color: Colors.black),
underline: Container(
height: 2,
color: Colors.black,
),
isDense: true,
hint: Padding(
padding: EdgeInsets.only(right: MediaQuery.of(context).size.width / 36),
child: Text('انتخاب موقعیت',
style: TextStyle(
fontFamily: "IranSans"
),
),
),
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: <String>['One', 'Two', 'Three', 'Four', 'Five']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
你试试:
String dropdownValue = 'One';
return DropdownButton<String>(
value: dropdownValue,
icon: const Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: const TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: <String>['One', 'Two', 'Free', 'Four']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);
您正在本地状态中声明变量。取而代之的是,您应该全局声明您的变量。
class DropDown extends StatefulWidget {
@override
_DropDowneState createState() =>
_DropDownState();
}
class _DropDownState extends State<DropDown> {
String dropdownValue;
@override
Widget build(BuildContext context) {
return DropdownButton<dynamic>(
value: dropdownValue,
//icon: const Icon(Icons.arrow_downward,),
iconSize: 22,
elevation: 16,
style: TextStyle(color: Colors.black),
underline: Container(
height: 2,
color: Colors.black,
),
isDense: true,
hint: Padding(
padding: EdgeInsets.only(right: MediaQuery.of(context).size.width / 36),
child: Text(
'انتخاب موقعیت',
style: TextStyle(fontFamily: "IranSans"),
),
),
items: <String>['One', 'Two', 'Three', 'Four', 'Five']
.map<DropdownMenuItem<dynamic>>((String value) {
return DropdownMenuItem<dynamic>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (newValue) {
setState(() {
dropdownValue = newValue;
print(dropdownValue);
});
},
);
}}
我的代码有问题,我定义了一个下拉按钮菜单,它会显示参数,当我点击参数时,select 下拉菜单中的值不会改变按钮。加上我的代码是在有状态小部件中定义的。 这是我的代码:
String dropdownValue;
return DropdownButton<String>(
value: dropdownValue,
//icon: const Icon(Icons.arrow_downward,),
iconSize: 22,
elevation: 16,
style: TextStyle(color: Colors.black),
underline: Container(
height: 2,
color: Colors.black,
),
isDense: true,
hint: Padding(
padding: EdgeInsets.only(right: MediaQuery.of(context).size.width / 36),
child: Text('انتخاب موقعیت',
style: TextStyle(
fontFamily: "IranSans"
),
),
),
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: <String>['One', 'Two', 'Three', 'Four', 'Five']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
你试试:
String dropdownValue = 'One';
return DropdownButton<String>(
value: dropdownValue,
icon: const Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: const TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: <String>['One', 'Two', 'Free', 'Four']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);
您正在本地状态中声明变量。取而代之的是,您应该全局声明您的变量。
class DropDown extends StatefulWidget {
@override
_DropDowneState createState() =>
_DropDownState();
}
class _DropDownState extends State<DropDown> {
String dropdownValue;
@override
Widget build(BuildContext context) {
return DropdownButton<dynamic>(
value: dropdownValue,
//icon: const Icon(Icons.arrow_downward,),
iconSize: 22,
elevation: 16,
style: TextStyle(color: Colors.black),
underline: Container(
height: 2,
color: Colors.black,
),
isDense: true,
hint: Padding(
padding: EdgeInsets.only(right: MediaQuery.of(context).size.width / 36),
child: Text(
'انتخاب موقعیت',
style: TextStyle(fontFamily: "IranSans"),
),
),
items: <String>['One', 'Two', 'Three', 'Four', 'Five']
.map<DropdownMenuItem<dynamic>>((String value) {
return DropdownMenuItem<dynamic>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (newValue) {
setState(() {
dropdownValue = newValue;
print(dropdownValue);
});
},
);
}}