Flutter 如何正确地从另一个有状态小部件调用的有状态小部件返回数据?
我正在从有状态小部件调用日期时间选择器小部件。我需要能够在调用datetime小部件的小部件中使用datetime小部件中选择的值。正确的方法是什么 这就是调用它的小部件的外观。当然瘦了Flutter 如何正确地从另一个有状态小部件调用的有状态小部件返回数据?,flutter,Flutter,我正在从有状态小部件调用日期时间选择器小部件。我需要能够在调用datetime小部件的小部件中使用datetime小部件中选择的值。正确的方法是什么 这就是调用它的小部件的外观。当然瘦了 SingleChildScrollView( child:Container( //WRAPS ENTIRE SHEET decoration: BoxDecoration( gradient:
SingleChildScrollView(
child:Container(
//WRAPS ENTIRE SHEET
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.blue, Colors.red],
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
stops: [0.1, 1])),
height: MediaQuery.of(context).size.height * .40,
child: ListView( children: [
SizedBox(
height: 10,
),
Container(
child: DatePicker(optionalText: "")
),
],),
),
)
],);
}
这是我需要返回数据的小部件
class _DatePickerState extends State<DatePicker> {
DateTime selectedDate = DateTime.now();
@override
Widget build(BuildContext context) {
return Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width * .50,
height: MediaQuery.of(context).size.height * .07,
child: Padding(
padding: EdgeInsets.fromLTRB(33, 0, 5, 0),
child: RaisedButton(
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
child: dateHyphenReplacer(selectedDate),
onPressed: () => _selectDate(context),
),
),
),
Padding(
padding: EdgeInsets.fromLTRB(5, 10, 0, 10),
child: Icon(
Icons.calendar_today,
color: Colors.white,
size: 30,
),
),
Container(
child: Padding(
padding: EdgeInsets.all(8.0),
child: AutoSizeText(
'Choose ${widget.optionalText} Date',
style: TextStyle(
color: Colors.white,
fontStyle: FontStyle.italic,
fontFamily: 'Montserrat'),
),
),
),
],
),
);
}
Future<void> _selectDate(BuildContext context) async {
final DateTime picked = await showDatePicker(
context: context,
initialDate: selectedDate,
firstDate: DateTime(2015, 8),
lastDate: DateTime(2101));
if (picked != null && picked != selectedDate)
setState(() {
selectedDate = picked;
});
return selectedDate;
}
dateHyphenReplacer(date){
date = formatDate(date, [mm, '/',dd,'/',yy]);
return Text('$date', style: TextStyle(color: Colors.blue, fontSize: 20));
}
}
class\u日期选择器状态扩展状态{
DateTime selectedDate=DateTime.now();
@凌驾
小部件构建(构建上下文){
返回容器(
孩子:排(
mainAxisAlignment:mainAxisAlignment.start,
儿童:[
容器(
宽度:MediaQuery.of(context).size.width*.50,
高度:MediaQuery.of(context).size.height*.07,
孩子:填充(
填充:来自LTRB(33,0,5,0)的边缘设置,
孩子:升起按钮(
颜色:颜色,白色,
形状:圆形矩形边框(
边界半径:边界半径。圆形(10)),
子项:datehyphereplacer(selectedDate),
按下时:()=>\u选择日期(上下文),
),
),
),
填充物(
填充:从LTRB(5,10,0,10)开始的边缘设置,
子:图标(
Icons.calendar_今天,
颜色:颜色,白色,
尺码:30,
),
),
容器(
孩子:填充(
填充:边缘设置。全部(8.0),
子对象:AutoSizeText(
'选择${widget.optionalText}日期',
样式:TextStyle(
颜色:颜色,白色,
fontStyle:fontStyle.italic,
fontFamily:“蒙特塞拉特”),
),
),
),
],
),
);
}
Future\u selectDate(BuildContext上下文)异步{
选择的最终日期时间=等待showDatePicker(
上下文:上下文,
初始日期:selectedDate,
firstDate:DateTime(2015年8月),
lastDate:DateTime(2101));
如果(已拾取!=null&&picked!=selectedDate)
设置状态(){
selectedDate=已拾取;
});
返回所选日期;
}
日期连字符替换器(日期){
日期=格式日期(日期,[mm',/',dd',/',yy]);
返回文本(“$date”,样式:TextStyle(颜色:Colors.blue,fontSize:20));
}
}
将DateTime
变量存储在父窗口小部件中,并将其传递给DatePicker
构造函数。选择DateTime
时,设置小部件。选择DateTime
。但是,这种方法不会自动重建父窗口小部件,因此不会更新对DateTime
的任何可视引用,并使用一些占位符值(如“1970年1月1日”)声明一些变量。然后将从DatePicker中选择的日期指定给此变量。然后设定状态