gpt4 book ai didi

flutter - 在下拉菜单中显示带有下拉选项的 Bottom Sheet 单击 flutter

转载 作者:行者123 更新时间:2023-12-03 10:11:31 25 4
gpt4 key购买 nike

我有一个显示各种选项的 DropDown。当前行为将选项显示为 DropdownMenuItem

如何将 DropdownMenuItem 切换为显示 DropDown 中所有选项的 BottomSheet

当前代码:

DropdownButtonHideUnderline(
child: Container(
color: Color.fromRGBO(216, 216, 216, 0.33),
padding: const EdgeInsets.fromLTRB(32, 8, 16, 8),
child: DropdownButton<String>(
hint: Text("TEST DROPDOWN"),
items: <String>['A', 'B', 'C', 'D'].map((String value) {

// this crashes
return showModalBottomSheet(context: context, builder: (builder) {
return Container(
child: Text('Hello From Modal Bottom Sheet'),
padding: EdgeInsets.all(40.0),
);
});

// this works
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);

}).toList(),
...

编辑:我试图在点击下拉菜单时显示类似于 iOS 日期选择器的内容。

最佳答案

如果你想自己做,这里有一个我做的简单例子:

class ModalDropDown extends StatefulWidget {
@override
_ModalDropDownState createState() => _ModalDropDownState();
}

class _ModalDropDownState extends State<ModalDropDown> {
String _selected = '';
List<String> _items = ['A', 'B', 'C', 'D'];

@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text('Show Modal'),
onPressed: () => showModal(context),
),
Text('Selected item: $_selected')
],
),
);
}

void showModal(context){
showModalBottomSheet(
context: context,
builder: (context){
return Container(
padding: EdgeInsets.all(8),
height: 200,
alignment: Alignment.center,
child: ListView.separated(
itemCount: _items.length,
separatorBuilder: (context, int) {
return Divider();
},
itemBuilder: (context, index) {
return GestureDetector(
child: Text(_items[index]),
onTap: () {
setState(() {
_selected = _items[index];
});
Navigator.of(context).pop();
}
);
}
),
);
}
);
}
}

如果您想要 Flutter 为 iOS 制作的选择器,您可以使用 CupertinoPicker

关于flutter - 在下拉菜单中显示带有下拉选项的 Bottom Sheet 单击 flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59953071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com