Android 颤振:未选择下拉列表值
我正在开发一个带有Android 颤振:未选择下拉列表值,android,ios,flutter,mobile,Android,Ios,Flutter,Mobile,我正在开发一个带有下拉列表的应用程序。下面是我的代码。我删除了UI设计代码,以隔离下拉列表部分本身 class ShoppingCartUIState extends State<ShoppingCartUI> { final _formKey = GlobalKey<FormState>(); String _checkoutDropdownValue=null; //**UI design Code Removed**// _showCheckoutP
下拉列表的应用程序。下面是我的代码。我删除了UI设计代码,以隔离下拉列表
部分本身
class ShoppingCartUIState extends State<ShoppingCartUI> {
final _formKey = GlobalKey<FormState>();
String _checkoutDropdownValue=null;
//**UI design Code Removed**//
_showCheckoutPopup() {
String date=DateTime.now().toString();
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0)), //this right here
child: Container(
height: MediaQuery.of(context).size.height/3,
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Container(
margin: EdgeInsets.all(10),
child: Text(
"What is Your Required Delivery Date?",
style: Theme.of(context).textTheme.subtitle,
),)
],
),
Row(
children: <Widget>[
IconButton(
icon: Icon(Icons.calendar_today),
color: Colors.green,
onPressed: () {
date = "111";
},
),
Text(date)
],
),
Row(
children: <Widget>[
Container(
margin: EdgeInsets.only(top:20, left:10),
child: Text(
"What is your Airport of delivery?",
style: Theme.of(context).textTheme.subtitle,
),)
],
),
Row(
children: <Widget>[
Container(
margin: EdgeInsets.only(top:5, left:10),
child: DropdownButton(
hint: Text(
"Please Select ",
style: TextStyle(
fontSize: 14,
),
),
items: <String>[
'Skinless Boneless, Full Loins',
'brown',
'silver'
].map((data) {
return DropdownMenuItem(
child: new Text(data,
style: Theme.of(context).textTheme.body1),
value: data,
);
}).toList(),
onChanged: (String newValue) {
setState(() {
_checkoutDropdownValue = newValue;
print(newValue);
});
},
value: _checkoutDropdownValue),
)
],
),
],
),
));
}
@override
void initState() {
// TODO: implement initState
super.initState();
}
}
类ShoppingCartuState扩展状态{
final _formKey=GlobalKey();
字符串_checkoutDropdownValue=null;
//**已删除UI设计代码**//
_showCheckoutPopup(){
字符串日期=DateTime.now().toString();
返回对话框(
形状:圆形矩形边框(
borderRadius:borderRadius.circular(20.0)),//就在这里
子:容器(
高度:MediaQuery.of(context).size.height/3,
子:列(
儿童:[
划船(
儿童:[
容器(
保证金:所有(10),
子:文本(
“您要求的交货日期是什么时候?”,
风格:Theme.of(context).textTheme.subtitle,
),)
],
),
划船(
儿童:[
图标按钮(
图标:图标(今天的图标、日历),
颜色:颜色。绿色,
已按下:(){
日期=“111”;
},
),
文本(日期)
],
),
划船(
儿童:[
容器(
页边距:仅限边集(顶部:20,左侧:10),
子:文本(
“你们的交货机场是什么?”,
风格:Theme.of(context).textTheme.subtitle,
),)
],
),
划船(
儿童:[
容器(
页边距:仅限边集(顶部:5,左侧:10),
孩子:下拉按钮(
提示:文本(
“请选择”,
样式:TextStyle(
尺寸:14,
),
),
项目:[
“去皮去骨,腰部丰满”,
“棕色”,
“银色”
].map((数据){
返回下拉菜单项(
子项:新文本(数据、,
风格:Theme.of(context.textTheme.body1),
价值:数据,
);
}).toList(),
onChanged:(字符串newValue){
设置状态(){
_checkoutDropdownValue=新值;
打印(新值);
});
},
值:_checkoutDropdownValue),
)
],
),
],
),
));
}
@凌驾
void initState(){
//TODO:实现initState
super.initState();
}
}
问题是当我更改下拉项时,新值永远不会被选中。始终显示以前选择的值。但是,由于我在下拉列表中使用了打印
,因此我可以看到该项目已更改
如何解决此问题?能否将onPressed更改为print(\u checkoutDropdownValue);代替打印(newValue);通过这种方式,我们可以查看分配是否有问题将您的对话框
小部件包装为StatefulBuilder
,以重建对话框
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyPage(), //TODO: Add Scaffold
);
}
}
class MyPage extends StatefulWidget {
@override
_MyPageState createState() => _MyPageState();
}
class _MyPageState extends State<MyPage> {
String date = "";
String _checkoutDropdownValue;
_showCheckoutPopup() {
return StatefulBuilder(
builder: (context, setState){
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
), //this r// ight here
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
margin: EdgeInsets.all(10),
alignment: Alignment.centerLeft,
child: Text(
"What is Your Required Delivery Date?",
style: Theme.of(context).textTheme.subtitle,
),
),
Row(
children: <Widget>[
IconButton(
icon: Icon(Icons.calendar_today),
color: Colors.green,
onPressed: () {
setState(() {
date = "111";
});
},
),
Text(date)
],
),
Container(
margin: EdgeInsets.only(top: 20, left: 10),
alignment: Alignment.centerLeft,
child: Text(
"What is your Airport of delivery?",
style: Theme.of(context).textTheme.subtitle,
),
),
Row(
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 5, left: 10),
child: DropdownButton<String>(
hint: Text(
"Please Select ",
style: TextStyle(
fontSize: 14,
),
),
items: <String>[
'Skinless Boneless, Full Loins',
'brown',
'silver'
].map((data) {
return DropdownMenuItem(
child: new Text(data,
style: Theme.of(context).textTheme.body1),
value: data,
);
}).toList(),
onChanged: (String newValue) {
setState(() {
_checkoutDropdownValue = newValue;
});
},
value: _checkoutDropdownValue,
),
)
],
),
],
),
),
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
child: Text("Click Here"),
onPressed: () {
showDialog(
context: context,
builder: (context) => _showCheckoutPopup(),
);
},
),
),
);
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyPage(),//TODO:添加脚手架
);
}
}
类MyPage扩展了StatefulWidget{
@凌驾
_MyPageState createState()=>\u MyPageState();
}
类MyPageState扩展了状态{
字符串日期=”;
字符串_checkoutDropdownValue;
_showCheckoutPopup(){
返回状态生成器(
生成器:(上下文,设置状态){
返回对话框(
形状:圆形矩形边框(
边界半径:边界半径。圆形(20.0),
),//这个r//就在这里
孩子:填充(
填充:常数边集全部(8.0),
子:列(
mainAxisSize:mainAxisSize.min,
儿童:[
容器(
保证金:所有(10),
对齐:alignment.centerLeft,
子:文本(
“您要求的交货日期是什么时候?”,
风格:Theme.of(context).textTheme.subtitle,
),
),
划船(
儿童:[
图标按钮(
图标:图标(今天的图标、日历),
颜色:颜色。绿色,
已按下:(){
设置状态(){
日期=“111”;
});
},
),
文本(日期)
],
),
容器(
页边距:仅限边集(顶部:20,左侧:10),
对齐:alignment.centerLeft,
子:文本(
“你们的交货机场是什么?”,
风格:Theme.of(context).textTheme.subtitle,
),
),
划船(
儿童:[
容器(
页边距:仅限边集(顶部:5,左侧:10),
孩子:下拉按钮(
提示:文本(
“请选择”,
样式:TextStyle(
字体大小