android-studio - 如何在将小部件弹出之前从小部件传递日期?
问题描述
我正在使用来自 Flutter https://pub.dartlang.org/packages/datetime_picker_formfield的表单域小部件
但是,我想将用户输入的日期传递回我以前的小部件(addReminder),但无法做到这一点。
我已经尝试将它放在一个静态变量中并访问它,但是没有成功,我对 dart 很弱,所以无法让类有一个互变量,我可以通过将小部件初始化为具有特定对象的对象来使用它多变的
并试图通过吸气剂获取变量,但未能这样做。
调用类 dateTime 的父小部件:
class addReminder extends StatelessWidget{
dateTimeWidget = new dateTime();
DateTime date;
@override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
title: Text('Add a Reminder'),
),
body:
new Container(
padding: new EdgeInsets.all(20.0),
child: new Form(
child: new ListView(
children: <Widget>[
new TextFormField(
keyboardType: TextInputType.text, // Use email input type for emails.
decoration: new InputDecoration(
hintText: 'Title of reminder',
),
),
dateTimeWidget,
RaisedButton(
child: Text('Save'),
onPressed:(){
//This is where I want to extract the date and save it to a local variable (date in this case)
Navigator.pop(context);
},
)
],
),
),
),
);
} }
将“DateTimePickerFormField”与小部件和状态类一起使用的 DateTime 小部件:
import 'package:flutter/material.dart';
import 'package:datetime_picker_formfield/datetime_picker_formfield.dart';
import 'package:intl/intl.dart';
class dateTime extends StatefulWidget{
@override
dateTimeState createState() => dateTimeState();
}
class dateTimeState extends State<dateTime>{
static DateTime dateT;
InputType inputType = InputType.both;
final formats = {
InputType.both: DateFormat("EEEE, MMMM d, yyyy 'at' h:mma"),
InputType.date: DateFormat('yyyy-MM-dd'),
InputType.time: DateFormat("HH:mm"),
};
Widget build(BuildContext context) => Container(
child: DateTimePickerFormField(
inputType: InputType.both,
editable: true,
format: formats[inputType],
decoration: InputDecoration(
labelText: 'Date/Time', hasFloatingPlaceholder: false),
onChanged: (dt) => setState(() => dateT = dt),
)
);
}
我后来尝试了这个:
将此方法添加到 addReminder 类:
dateTimee(BuildContext context) async {
final result = await Navigator.push(
context,
MaterialPageRoute(builder: (context)=> dateTime()),
);
}
并这样称呼它:
dateTimee(context),
在我添加的 onchanged 参数的 dateTime 类中
onChanged: (dt) {
setState(() => dateT = dt);
Navigator.of(context).pop(dateT);
},
但是我得到了错误:
I/flutter (18662): Another exception was thrown:
'package:flutter/src/widgets/navigator.dart': Failed assertion: line1995
pos 12: '!_debugLocked': is not true.
我认为这是因为该方法是 Future 类型,应该调用一个小部件所以我不知道该怎么做我正在调用的小部件 dateTimee 是基于调用小部件 dateTimePickerFromField
解决方案
看看食谱https://flutter.io/docs/cookbook/navigation/returning-data
总之:Navigator.push
返回一个将在推送的小部件调用时完成的未来Navigator.pop
。您可以将返回值传递pop
给未来解析的值。
// In widget 1..
final result = await Navigator.of(context).push(...);
// In widget 2..
Navigator.of(context).pop('output');
然后一旦小部件 2 调用 pop,小部件 1 正在等待的未来将完成,并且result
变量将被分配给'output'
字符串。
推荐阅读
- python - groupby 表 Python Pandas 有问题吗?
- java - 通知安卓 (FCM)
- c# - 是否可以使用 ML.NET 为神经网络构建自定义深度学习层?
- java - 如何从 Java 上的字符串中修剪特定字符
- python - Numpy 索引解释
- javascript - nanoid 的随机算法真的比随机 % 字母更好吗?
- python - 在 Python 中将字符串对象转换为日期对象
- tryton - Tryton:如何使用更多的视图空间制作 One2Many 字段视图
- ios - 访问使用情节提要添加的 tableView 标头
- react-native - React Native AsyncStorage 可以用来永久存储数据吗?