我能够居中提供给DropdownMenuItem
的{{1}}中使用的文本项,但是关闭菜单后如何使所选项目居中?
答案 0 :(得分:1)
类似这样的东西:
DropdownMenuItem<int>(
value: model.id,
child: SizedBox(
width: width,
child: Text(
model.toString(),
textAlign: TextAlign.center, //this will do that
),
),
)
答案 1 :(得分:0)
我正在使用类似这样的东西:
String dropdownValue = 'One';
DropdownButton<String>(
value: dropdownValue,
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: <String>['One', 'Two', 'three', 'Four'].map((item) {
return DropdownMenuItem<String>(
value: item,
child: Builder(builder: (BuildContext context) {
final bool isDropDown = context.ancestorStateOfType(TypeMatcher<_MyAppState>()) == null;
if (!isDropDown) {
return Center(child: Text(item), widthFactor: 2,);
} else {
return Text(item);
}
},)
);
}).toList(),
),
答案 2 :(得分:0)
您可以通过将DropdownMenuItem
的孩子Text
包裹在Container
中并将其width
与alignment
一起提供来实现。
int _value = 0;
Widget _buildDropdown() {
return DropdownButton(
value: _value,
items: [
DropdownMenuItem(
value: 0,
child: Container(
child: Text("Zero"),
width: 200,
alignment: Alignment.center,
),
),
DropdownMenuItem(
value: 1,
child: Container(
child: Text("One"),
width: 200,
alignment: Alignment.center,
),
),
],
onChanged: (value) => setState(() => _value = value),
);
}
答案 3 :(得分:0)
这对我有用:
List<String> targetOptions = ['No target', '1', '2', '3', '4'];
return DropdownButton<String>(
value: target,
selectedItemBuilder: (BuildContext context) {
return targetOptions.map<Widget>((String item) {
return SizedBox(width: 70, child: Center(child: Text(item, style: TextStyle(color: Colors.white))));
}).toList();
},
items: targetOptions.map((String value) {
return new DropdownMenuItem<String>(
value: value == 'No target' ? '0' : value,
child: Center(
child: new Text(
value,
style: TextStyle(color: Colors.white),
),
),
);
}).toList(),
onChanged: (val) {
SettingManager.put(SettingManager.SETTING_DAILY_TARGET, val);
setState(() {
target = val;
});
},
)