How to get flutter datepicker value to the textfield?(如何将颤动的日期选择器值添加到文本字段?)
本文介绍了如何将颤动的日期选择器值添加到文本字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将颤振日期选择器值添加到文本字段?当我单击文本字段时,它会在选择日期后弹出日期选择器.我想成为文本字段中的那个日期
How to get flutter date picker value to the textfield?when i click the text field it is pop up date picker after choosing date. i wanted to be that date in the textfield
还有.
我想自定义日期选择器的颜色
i want to customise the color of the date picker
onTap: () {
FocusScope.of(context).requestFocus(new FocusNode());
showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2019, 1),
lastDate: DateTime(2021,12),
);
},
推荐答案
我将你的代码修改如下(你可以从 DatePicker 中选择日期,你可以自定义 DatePicker 的主题):
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class TestPickerWidget extends StatefulWidget {
@override
_TestPickerWidgetState createState() => _TestPickerWidgetState();
}
class _TestPickerWidgetState extends State<TestPickerWidget> {
DateTime _selectedDate;
TextEditingController _textEditingController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: TextField(
focusNode: AlwaysDisabledFocusNode(),
controller: _textEditingController,
onTap: () {
_selectDate(context);
},
),
),
);
}
_selectDate(BuildContext context) async {
DateTime newSelectedDate = await showDatePicker(
context: context,
initialDate: _selectedDate != null ? _selectedDate : DateTime.now(),
firstDate: DateTime(2000),
lastDate: DateTime(2040),
builder: (BuildContext context, Widget child) {
return Theme(
data: ThemeData.dark().copyWith(
colorScheme: ColorScheme.dark(
primary: Colors.deepPurple,
onPrimary: Colors.white,
surface: Colors.blueGrey,
onSurface: Colors.yellow,
),
dialogBackgroundColor: Colors.blue[500],
),
child: child,
);
});
if (newSelectedDate != null) {
_selectedDate = newSelectedDate;
_textEditingController
..text = DateFormat.yMMMd().format(_selectedDate)
..selection = TextSelection.fromPosition(TextPosition(
offset: _textEditingController.text.length,
affinity: TextAffinity.upstream));
}
}
}
class AlwaysDisabledFocusNode extends FocusNode {
@override
bool get hasFocus => false;
}
您还必须在 pubspec.yaml 中添加 Intl 依赖项:
dev_dependencies:
flutter_test:
sdk: flutter
intl: ^0.16.1
最后在你的 main 中调用 TestPickerWidget 来测试它.
Finally call TestPickerWidget in your main for testing it.
这篇关于如何将颤动的日期选择器值添加到文本字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何将颤动的日期选择器值添加到文本字段?


基础教程推荐
猜你喜欢
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01