【发布时间】:2019-06-29 00:25:28
【问题描述】:
我正在使用来自 Flutter 的表单域小部件 https://pub.dartlang.org/packages/datetime_picker_formfield
但是,我想将用户输入的日期传递回我之前的小部件 (addReminder),但无法做到这一点。
我尝试将它放在一个静态变量中并访问它,但没有成功,我对 dart 很弱,所以无法让类有一个互变量,我可以通过将小部件初始化为对象来使用它有一个特定的变量
并试图通过 getter 获取变量但没有成功。
调用 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
【问题讨论】:
标签: android-studio flutter widget hybrid