我使用的颤振qazxsw POI的表单字段部件
不过,我想通过用户输入的回我以前的小部件(addReminder),但不能让这样做的日期。
我试图把它在一个静态变量和访问它,但没有奏效,我软弱,所以镖不能得到的类有,我可以通过与特定的初始化窗口小部件为对象使用一个共同的变量变量
并试图通过一个getter来获取变量,但未能如愿。
调用类的日期时间父窗口部件:
https://pub.dartlang.org/packages/datetime_picker_formfield
} }
使用“DateTimePickerFormField”与两个小部件和状态类的日期时间窗口小部件:
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);
},
)
],
),
),
),
);
}
我后来尝试这样的:
添加了这个方法的addReminder类:
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),
)
);
}
并把它称为是这样的:
dateTimee(BuildContext context) async {
final result = await Navigator.push(
context,
MaterialPageRoute(builder: (context)=> dateTime()),
);
而在给onChanged参数DateTime类加我
dateTimee(context),
但是我得到的错误:
onChanged: (dt) {
setState(() => dateT = dt);
Navigator.of(context).pop(dateT);
},
我认为这是因为该方法是未来型的,那里电话应该是一个widget所以我不知道该怎么办,我呼吁小部件是dateTimee基于调用控件dateTimePickerFromField
看看菜谱 I/flutter (18662): Another exception was thrown:
'package:flutter/src/widgets/navigator.dart': Failed assertion: line1995
pos 12: '!_debugLocked': is not true.
总结:https://flutter.io/docs/cookbook/navigation/returning-data返回未来,当推控件调用Navigator.push
将完成。你可以通过一个返回值Navigator.pop
是未来与解析值。
pop
然后,一旦部件2个来电弹出,对未来的窗口小部件1正在等待将完成,而// In widget 1..
final result = await Navigator.of(context).push(...);
// In widget 2..
Navigator.of(context).pop('output');
变量将被分配到result
字符串。