Flutter - DrowpdownMunu 项目未从列表中填充
Flutter - DrowpdownMunu Items not populating from List
我遇到了未填充任何数据的 DrowdownButton。
它是从上一屏幕传递的列表中填充的。
我的调试显示列表中有可以解析的数据。输出只是产生一个空的 DropdownButton。
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'menuList.dart';
import 'package:flutter_calendar_carousel/flutter_calendar_carousel.dart' show CalendarCarousel;
class OrderInitiate extends StatefulWidget {
final List childData;
OrderInitiate(this.childData);
@override
_OrderInitiateState createState() => new _OrderInitiateState();
}
class _OrderInitiateState extends State<OrderInitiate> {
double buttonWidth = 200.0;
DateTime selectedDate = DateTime.now();
DateTime tempDate = DateTime.now();
@override
void initState() {
print("OrderInitiate: initstate begin");
print("ChildData: " + widget.childData.toString());
}
_dateSelected(date) {
setState(() {
selectedDate = date;
});
}
String itemSel;
@override
Widget build(BuildContext context) => new Scaffold(
appBar: new AppBar(title: new Text("Start an Order"), actions:
<Widget>[
]),
body: new Column(
children: <Widget>[
Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text("Select child:"),
new DropdownButton<String>(
value: itemSel,
onChanged: null,
hint: Text("Select child"),
items:
widget.childData.map ((item) {
print("DropDownButton item: " + item.toString());
print("Child Id: " + item['ChildId']);
return new DropdownMenuItem<String>(
value: item['ChildId'],
child:
new Text("ChildFirstName"),
);
}).toList(),
),
Text("${selectedDate.toLocal()}"),
SizedBox(
height: 20.0,
),
],
),
),
SizedBox(
width: buttonWidth,
child: RaisedButton(
onPressed: () => _StartOrder(this.context),
child: Text('Start Order'),
),
),
],
),
);
_StartOrder(BuildContext context) {
Navigator.push(
context,
new MaterialPageRoute(
builder: (BuildContext context) => new MenuList()));
}
}
我的调试行给出以下输出:
DropDownButton item: {ParentId: 4, ParentLastName: testparent, ParentFirstName: TestParentSur, ParentUserName: TestParent, ChildId: 3, ChildLastName: Test2Last, ChildFirstName: Test2, ChildUserName: test2}
Child Id: 3
过去 2 晚我一直被这个代码困住了,所以希望有人能提供帮助。
我研究了很多列表和项目生成教程和 Whosebug 项目,但没有成功。
尝试将 DropDownButton
的 onChanged
值更改为:
onChanged: (int i) {
setState(() => itemSel = i);
},
如果 onChanged == null
则该字段基本上被禁用,因此虽然您的数据可能在那里,但当您点击它时它不会显示任何内容。该字段的选定值也不会自动更新,因此使用新选定值设置状态也很重要。
我遇到了未填充任何数据的 DrowdownButton。
它是从上一屏幕传递的列表中填充的。
我的调试显示列表中有可以解析的数据。输出只是产生一个空的 DropdownButton。
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'menuList.dart';
import 'package:flutter_calendar_carousel/flutter_calendar_carousel.dart' show CalendarCarousel;
class OrderInitiate extends StatefulWidget {
final List childData;
OrderInitiate(this.childData);
@override
_OrderInitiateState createState() => new _OrderInitiateState();
}
class _OrderInitiateState extends State<OrderInitiate> {
double buttonWidth = 200.0;
DateTime selectedDate = DateTime.now();
DateTime tempDate = DateTime.now();
@override
void initState() {
print("OrderInitiate: initstate begin");
print("ChildData: " + widget.childData.toString());
}
_dateSelected(date) {
setState(() {
selectedDate = date;
});
}
String itemSel;
@override
Widget build(BuildContext context) => new Scaffold(
appBar: new AppBar(title: new Text("Start an Order"), actions:
<Widget>[
]),
body: new Column(
children: <Widget>[
Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text("Select child:"),
new DropdownButton<String>(
value: itemSel,
onChanged: null,
hint: Text("Select child"),
items:
widget.childData.map ((item) {
print("DropDownButton item: " + item.toString());
print("Child Id: " + item['ChildId']);
return new DropdownMenuItem<String>(
value: item['ChildId'],
child:
new Text("ChildFirstName"),
);
}).toList(),
),
Text("${selectedDate.toLocal()}"),
SizedBox(
height: 20.0,
),
],
),
),
SizedBox(
width: buttonWidth,
child: RaisedButton(
onPressed: () => _StartOrder(this.context),
child: Text('Start Order'),
),
),
],
),
);
_StartOrder(BuildContext context) {
Navigator.push(
context,
new MaterialPageRoute(
builder: (BuildContext context) => new MenuList()));
}
}
我的调试行给出以下输出:
DropDownButton item: {ParentId: 4, ParentLastName: testparent, ParentFirstName: TestParentSur, ParentUserName: TestParent, ChildId: 3, ChildLastName: Test2Last, ChildFirstName: Test2, ChildUserName: test2}
Child Id: 3
过去 2 晚我一直被这个代码困住了,所以希望有人能提供帮助。
我研究了很多列表和项目生成教程和 Whosebug 项目,但没有成功。
尝试将 DropDownButton
的 onChanged
值更改为:
onChanged: (int i) {
setState(() => itemSel = i);
},
如果 onChanged == null
则该字段基本上被禁用,因此虽然您的数据可能在那里,但当您点击它时它不会显示任何内容。该字段的选定值也不会自动更新,因此使用新选定值设置状态也很重要。