到目前为止,我做了什么:我在TextFormField中有一个onTap()操作。此onTap()操作调用_selectDate来打开日期选择器。在_selectDate内部,我使用了一个我创建的辅助变量presentText,并在TextFormField的initialValue内部分配了此变量。问题是,即使我更改日期选择器中的日期,initialValue始终为空,TextFormField的initialValue仍为空白。在调试中,我看到正确填充了presentText,但是我不知道为什么该值不进入TextFormField。
const doc = new PDFDocument();
doc.image('./image.png', {
fit: [doc.page.witdh, doc.page.height],
});
答案 0 :(得分:1)
在这种情况下,您必须在TextFormField中使用控制器,如下所示:
DateTime selectedDate = DateTime.now();
TextEditingController controller = TextEditingController();
String presentText;
void _selectDate() async {
final DateTime picked = await showDatePicker(
context: context,
initialDate:DateTime.now(),
firstDate: DateTime(2015, 8),
lastDate: DateTime(2101));
if (picked != null && picked != selectedDate)
setState(() {
selectedDate = picked;
controller.text=picked.toString();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
padding: EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Form(
autovalidate: true,
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 15),
child: Text(
'Birthday',
textAlign: TextAlign.left,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontWeight: FontWeight.bold),
)),
GestureDetector(
onTap: () => _selectDate(),
child: AbsorbPointer(
child: TextFormField(
controller: controller,
initialValue: presentText,
))),
],
),
),
),
);
}
答案 1 :(得分:1)
您可以在下面复制粘贴运行完整代码
您可以使用TextEditingController
代码段
final myController = TextEditingController();
myController.text = "${widget.user.birthday.toLocal()}" != null
? "${widget.user.birthday.toLocal()}".split(' ')[0].toString()
: "${selectedDate.toLocal()}".split(' ')[0].toString();
AbsorbPointer(
child: TextFormField(controller: myController
工作演示
完整代码
import 'package:flutter/material.dart';
class User {
DateTime birthday;
User({this.birthday});
}
class DetailForm extends StatefulWidget {
User user;
DetailForm({this.user});
@override
_DetailFormState createState() => _DetailFormState();
}
class _DetailFormState extends State<DetailForm> {
DateTime selectedDate = DateTime.now();
String presentText;
final _formKey = GlobalKey<FormState>();
final myController = TextEditingController();
Future<Null> _selectDate(BuildContext context) async {
final DateTime picked = await showDatePicker(
context: context,
initialDate:
widget.user.birthday != null ? widget.user.birthday : selectedDate,
firstDate: DateTime(2015, 8),
lastDate: DateTime(2101));
if (picked != null && picked != selectedDate)
setState(() {
selectedDate = picked;
widget.user.birthday = picked;
myController.text = "${widget.user.birthday.toLocal()}" != null
? "${widget.user.birthday.toLocal()}".split(' ')[0].toString()
: "${selectedDate.toLocal()}".split(' ')[0].toString();
});
}
@override
void dispose() {
// Clean up the controller when the widget is removed from the
// widget tree.
myController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
padding: EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Form(
key: _formKey,
autovalidate: true,
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 15),
child: Text(
'Birthday',
textAlign: TextAlign.left,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontWeight: FontWeight.bold),
)),
GestureDetector(
onTap: () => _selectDate(context),
child: AbsorbPointer(
child: TextFormField(controller: myController
//initialValue: presentText
))),
],
),
),
),
);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: DetailForm(user: User()),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}