gpt4 book ai didi

flutter - 在 Flutter 的 Bottom Sheet 单中创建一个下拉菜单

转载 作者:行者123 更新时间:2023-12-03 02:45:43 25 4
gpt4 key购买 nike

我正在尝试做这样的事情,根据列表内容创建一个下拉列表。
我的 list 看起来像这样,

[
{
id: val,
displayName: Enter value,
type: string,
value: "any"
},
{
id: si,
displayName: Source,
type: list,
value: [
MO
],
data: [
{id: 1, displayId: MO},
{id: 2, displayId: AO},
{id: 3, displayId: OffNet}
]
}
]

目前有2个条目。显示包含这些选项的下拉列表(输入值和来源)作为下拉列表的 2 个条目

如果选择了输入值,则应显示其旁边的文本框,因为它具有字符串类型。
如果选择下拉列表中的 Source 选项,则包含这些条目(MO、AO、Offnet)的另一个下拉列表应作为下拉值出现,因为它具有列表类型。

根据第一个下拉列表的选择,应该选择要显示的小部件(文本框或另一个下拉列表)。

我有一个这样的代码,这将是必要的,但这里将整个页面带到容器中,只要选项更改了一个调用的setstate,它会重建构建方法,但我想在底线内部实现相同的东西不知道管理状态,即一旦下拉列表中的选项发生变化,我希望 Bottom Sheet 格能够使用数据重建。

代码:
import 'package:flutter/material.dart';

void main() {
runApp(DropdownExample());
}

class DropdownExample extends StatefulWidget {
@override
_DropdownExampleState createState() => _DropdownExampleState();
}

class _DropdownExampleState extends State<DropdownExample> {
String type;
int optionId;

final items = [
{
"displayName": "Enter value",
"type": "string",
},
{
"displayName": "Source",
"type": "list",
"data": [
{"id": 1, "displayId": "MO"},
{"id": 2, "displayId": "AO"},
{"id": 3, "displayId": "OffNet"}
]
}
];

@override
Widget build(BuildContext context) {
Widget supporting = buildSupportingWidget();

return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("Dropdown Example")),
body: Center(
child: Container(
height: 600,
width: 300,
child: Row(
children: <Widget>[
buildMainDropdown(),
if (supporting != null) supporting,
],
),
),
),
),
);
}

Expanded buildMainDropdown() {
return Expanded(
child: DropdownButtonHideUnderline(
child: DropdownButton(
value: type,
hint: Text("Select a type"),
items: items
.map((json) => DropdownMenuItem(
child: Text(json["displayName"]), value: json["type"]))
.toList(),
onChanged: (newType) {
setState(() {
type = newType;
});
},
),
),
);
}

Widget buildSupportingWidget() {
if (type == "list") {
List<Map<String, Object>> options = items[1]["data"];
return Expanded(
child: DropdownButtonHideUnderline(
child: DropdownButton(
value: optionId,
hint: Text("Select an entry"),
items: options
.map((option) => DropdownMenuItem(
child: Text(option["displayId"]), value: option["id"]))
.toList(),
onChanged: (newId) => setState(() {
this.optionId = newId;
}),
),
),
);
} else if (type == "string") {
return Expanded(child: TextFormField());
}
return null;
}
}

上面的代码工作正常,但我试图做的是同样的事情应该进入具有确切功能的底层。

每当按下“打开 Bottom Sheet ”按钮时,应弹出 Bottom Sheet 并将代码结果显示为 Bottom Sheet 的内容。

我做过这样的事情,但它不起作用
import 'package:flutter/material.dart';

void main() {
runApp(DropdownExample());
}

class DropdownExample extends StatefulWidget {
@override
_DropdownExampleState createState() => _DropdownExampleState();
}

class _DropdownExampleState extends State<DropdownExample> {
String type;
int optionId;

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("Dropdown Example")),
body: Center(
child: Container(
height: 600,
width: 300,
child: Row(
children: <Widget>[
Align(
alignment: Alignment.topRight,
child: FlatButton.icon(
label: Text('Filters'),
icon: Icon(Icons.filter_list),
onPressed: showModalSheet(),
)),
],
),
),
),
),
);
}

showModalSheet() {

final items = [
{
"displayName": "Enter value",
"type": "string",
},
{
"displayName": "Source",
"type": "list",
"data": [
{"id": 1, "displayId": "MO"},
{"id": 2, "displayId": "AO"},
{"id": 3, "displayId": "OffNet"}
]
}
];

showModalBottomSheet<void>(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
context: context,
builder: (BuildContext context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter state) {
return createBox(context, items, state);
});
});

}

createBox(BuildContext context, List<Map<String,Object>> val,StateSetter state) {
Widget supporting = buildSupportingWidget(val);
return SingleChildScrollView(
child: LimitedBox(
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
buildMainDropdown(val),
if (supporting != null) supporting
]
)
)
);

}



Expanded buildMainDropdown(List<Map<String,Object>> items) {
return Expanded(
child: DropdownButtonHideUnderline(
child: DropdownButton(
value: type,
hint: Text("Select a type"),
items: items
.map((json) => DropdownMenuItem(
child: Text(json["displayName"]), value: json["type"]))
.toList(),
onChanged: (newType) {
setState(() {
type = newType;
});
},
),
),
);
}

Widget buildSupportingWidget(List<Map<String,Object>>items) {
if (type == "list") {
List<Map<String, Object>> options = items[1]["data"];
return Expanded(
child: DropdownButtonHideUnderline(
child: DropdownButton(
value: optionId,
hint: Text("Select an entry"),
items: options
.map((option) => DropdownMenuItem(
child: Text(option["displayId"]), value: option["id"]))
.toList(),
onChanged: (newId) => setState(() {
this.optionId = newId;
}),
),
),
);
} else if (type == "string") {
return Expanded(child: TextFormField());
}
return null;
}
}

让我知道需要的更改,谢谢

最佳答案

第 1 步:LimitedBox 需要 maxHeight
第 2 步:函数 showModalSheet 需要传递上下文
第 3 步: createBox 、 buildMainDropdown 和 buildSupportingWidget 需要为 StatefulBuilder 传递状态

完整代码

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: DropdownExample(),
);
}
}

class DropdownExample extends StatefulWidget {
@override
_DropdownExampleState createState() => _DropdownExampleState();
}

class _DropdownExampleState extends State<DropdownExample> {
String type;
int optionId;


@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("Dropdown Example")),
body: Center(
child: Container(
height: 600,
width: 300,
child: Row(
children: <Widget>[
Align(
alignment: Alignment.topRight,
child: FlatButton.icon(
label: Text('Filters'),
icon: Icon(Icons.filter_list),
// onPressed: showModalSheet(),
onPressed: () {
showModalSheet(context);
},
)),
],
),
),
),
),
);
}

void showModalSheet(BuildContext context) {
final items = [
{
"displayName": "Enter value",
"type": "string",
},
{
"displayName": "Source",
"type": "list",
"data": [
{"id": 1, "displayId": "MO"},
{"id": 2, "displayId": "AO"},
{"id": 3, "displayId": "OffNet"}
]
}
];

showModalBottomSheet<void>(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
context: context,
builder: (BuildContext context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter state) {
return createBox(context, items, state);
});
});
}



createBox(BuildContext context, List<Map<String,Object>> val,StateSetter state) {
Widget supporting = buildSupportingWidget(val,state);
return SingleChildScrollView(
child: LimitedBox(
maxHeight: 300,
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
buildMainDropdown(val,state),
if (supporting != null) supporting
]
)
)
);

}



Expanded buildMainDropdown(List<Map<String,Object>> items,StateSetter setState) {
return Expanded(
child: DropdownButtonHideUnderline(
child: DropdownButton(
value: type,
hint: Text("Select a type"),
items: items
.map((json) => DropdownMenuItem(
child: Text(json["displayName"]), value: json["type"]))
.toList(),
onChanged: (newType) {
setState(() {
type = newType;
});
},
),
),
);
}

Widget buildSupportingWidget(List<Map<String,Object>>items, StateSetter setState) {
if (type == "list") {
List<Map<String, Object>> options = items[1]["data"];
return Expanded(
child: DropdownButtonHideUnderline(
child: DropdownButton(
value: optionId,
hint: Text("Select an entry"),
items: options
.map((option) => DropdownMenuItem(
child: Text(option["displayId"]), value: option["id"]))
.toList(),
onChanged: (newId) => setState(() {
this.optionId = newId;
}),
),
),
);
} else if (type == "string") {
return Expanded(child: TextFormField());
}
return null;
}
}

enter image description here

关于flutter - 在 Flutter 的 Bottom Sheet 单中创建一个下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58273628/

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