gpt4 book ai didi

flutter - 日期选择器未在TextFormField中设置输入

转载 作者:行者123 更新时间:2023-12-03 03:32:23 28 4
gpt4 key购买 nike

https://dartpad.dev/c8fc4e640c882616c372653c92362b17上查看问题
Datepicker正在打开并在状态中设置日期,但是所选日期在TextFormField中不可见。有什么办法吗?

最佳答案

您可以在下面复制粘贴运行完整代码
您可以输入onTap await _selectDate(context);并使用date设置TextEditingController()字符串
程式码片段

  TextEditingController _textEditingController = TextEditingController();

Widget buildDateField() {
return TextFormField(
controller: _textEditingController,
onTap: () async {
// Below line stops keyboard from appearing
FocusScope.of(context).requestFocus(new FocusNode());
// Show Date Picker Here
await _selectDate(context);
_textEditingController.text = DateFormat('yyyy/MM/dd').format(date);
//setState(() {});
},
工作演示
enter image description here
完整的代码
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
DateTime selectedDate = DateTime.now();

// Future<Null> _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;
// });
// }

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: CreatePublicEventScreen(),
),
);
}
}

class CreatePublicEventScreen extends StatefulWidget {
@override
State<CreatePublicEventScreen> createState() {
return CreatePublicEventState();
}
}

class CreatePublicEventState extends State<CreatePublicEventScreen> {
String title;
DateTime date;
String strDate;
String startTime;
String endTime;
String venue;
final String createdBy;

CreatePublicEventState({this.createdBy});

final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

// @override
// void initState() {
// super.initState();
// _focusNode.addListener(() {
// print('>>object>>');
// _selectDate(context);
// });
// }
//
// @override
// void dispose() {
// _focusNode.dispose();
// super.dispose();
// }

Future<void> _selectDate(BuildContext context) async {
final now = DateTime.now();
final DateTime picked = await showDatePicker(
context: context,
initialDate: date ?? now,
firstDate: now,
lastDate: DateTime(2101));
if (picked != null && picked != date) {
print('hello $picked');
setState(() {
date = picked;
});
}
}

Widget buildTitleField() {
return TextFormField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Event Title',
contentPadding: EdgeInsets.symmetric(
vertical: 0.0,
horizontal: 12.0,
),
),
validator: (String value) {
if (value.isEmpty) {
return 'Event Title is Required.';
}
return null;
},
onSaved: (String value) {
title = value;
},
);
}

TextEditingController _textEditingController = TextEditingController();
Widget buildDateField() {
return TextFormField(
controller: _textEditingController,
onTap: () async {
// Below line stops keyboard from appearing
FocusScope.of(context).requestFocus(new FocusNode());
// Show Date Picker Here
await _selectDate(context);
_textEditingController.text = DateFormat('yyyy/MM/dd').format(date);
//setState(() {});
},
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Event Date',
contentPadding: EdgeInsets.symmetric(
vertical: 0.0,
horizontal: 12.0,
),
),
validator: (String value) {
print('date:: ${date.toString()}');
if (value.isEmpty) {
return 'Event Date is Required.';
}
return null;
},
onSaved: (String val) {
strDate = val;
},
);
}

Widget buildTimeField({labelText, labelType}) {
return TextFormField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: labelText,
contentPadding: EdgeInsets.symmetric(
vertical: 0.0,
horizontal: 12.0,
),
),
validator: (String value) {
if (value.isEmpty) {
return 'Time is Required.';
}
return null;
},
onSaved: (String value) {
if (labelType == 'startTime') {
startTime = value;
} else {
endTime = value;
}
},
);
}

Widget buildVenueField() {
return TextFormField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Venue',
contentPadding: EdgeInsets.symmetric(
vertical: 0.0,
horizontal: 12.0,
),
),
validator: (String value) {
if (value.isEmpty) {
return 'Venue is Required.';
}
return null;
},
onSaved: (String value) {
venue = value;
},
);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Create Public Event'),
),
body: SingleChildScrollView(
child: Container(
margin: EdgeInsets.all(20.0),
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
Text(
'Create Public Event',
style: Theme.of(context).textTheme.headline5,
),
SizedBox(
height: 20.0,
),
buildTitleField(),
SizedBox(
height: 20.0,
),
buildDateField(),
SizedBox(
height: 20.0,
),
buildTimeField(labelText: 'Start Time', labelType: 'startTime'),
SizedBox(
height: 20.0,
),
buildTimeField(labelText: 'End Time', labelType: 'endTime'),
SizedBox(
height: 20.0,
),
buildVenueField(),
SizedBox(
height: 20.0,
),
RaisedButton(
color: Color(0xFFF97F8B),
child: Text(
'Submit Event',
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
),
),
onPressed: () {
if (!_formKey.currentState.validate()) {
return;
}
_formKey.currentState.save();
// createPublicEvent(
// date: date,
// startTime: startTime,
// endTime: endTime,
// title: title,
// venue: venue)
// .then((onValue) {
// print('value $onValue');
// });
},
),
],
),
),
),
),
);
}
}

关于flutter - 日期选择器未在TextFormField中设置输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63165902/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com