What is the correct way to add date picker in flutter app? –

Development issue/problem:

In my request I create a registration page where I have to add an NMS. I want to add a date selection function, but I can’t find the right way to do it.

How can I solve this problem?

Solution 1:

A simple application that demonstrates its use:

import dart:async ;

import package: flutter/materiaal.dart ;

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

The MyApp class extends the StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp (
title : ‘Flutter Demo’,
home : MyHomePage(title : ‘Flutter Demo Home Page’),
) ;
} }.

The MyHomePage class expands StatefulWidget {
MyHomePage({key, this.title}): super(key: key) ;

is the final name of the chain;

_MyHomePageState createState() => _MyHomePageState();

The _MyHomePageState class extends State {
DateTime selectedDate = DateTime.now() ;

Future _selectDate(BuildContext context) async {
final DateTime picked = waitit showDatePicker(
context : context,
initialDate : selectedDate,
firstDate : DateTime(2015, 8),
lastDate : DateTime(2101)) ;
if(pick != null && picked != selectedDate)
setState(() {
selectedDate = picked;
}) ;

Widget build(BuildContext context) {
returns Scaffold(
appBar : AppBar(
title : Text(widget.title),
), body
: Centre(
child : Column(
children :
SizeBox(height : 20.0,),
raised button(
onPressed : () => _selectDate(context),
child : Text(‘Select Date’),
) ;

And a dart to go with it:


Solution 2:

Flutter offers the showDatePicker function for this purpose. This is part of the library of fluttering material.

The complete documentation can be found on the showDatePicker website.

You can also find an implementation example here: Selection of date and time

Solution 3:

for a time collector…

Report this variable at class level

TimeOfDay selectedTime =TimeOfDay.now() ;

and call this method:-

Future _selectTime(BuildContext) async {
final TimeOfDay picked_s = wait showTimePicker(
context : context,
initialTime : selectedTime, builder : (BuildContext context, Widget child) {
return data MediaQuery(
: MediaQuery.of(context).copyWith(alwaysUse24HourFormat : false),
child : child,

If (pick_s != null && pick_s != selectedTime )
setState(() {
selectedTime = pick_s ;

Solution 4:

A simple way is to use the CupertinoDatePicker class:

First import your package that is integrated in Flutter:

import package: flutter/cupertino.dart;

Then all you have to do is add this widget to your form:

Container (
height : 200,
child : CupertinoDatePicker (
mode : CupertinoDatePickerMode.date,
initialDateTime : DateTime(1969, 1, 1),
onDateTimeChanged : (DateTime newDateTime) {
// Do some

The result is the same as in this picture:

You can also change the mode in (date and time, time) …… z. B. for date and time mode :

Container (
height : 200,
child : CupertinoDatePicker (
mode : CupertinoDatePickerMode.dateAndTime,
initialDateTime : DateTime(1969, 1, 1, 11, 33),
onDateTimeChanged : (DateTime newDateTime) {
//Do Some thing
use24hFormat : false,
minuteInterval : 1,

The result is the same as in this picture:

Solution No 5:

You must first create a variable. You can save the selected date in this variable as follows:

import ‘package:flutter/materiaal.dart’;
import ‘package:intl/intl.dart’; // this is an external package for date and time formatting.

The DatePicker class extends the StatefulWidget {
_DatePickerState createState() => _DatePickerState() ;

The _DatePickerState class expands State {
DateTime _selectedDate ;

//timestamp display method invalid _pickDateDialog() {showDatePicker(context,initialDate: DateTime.now(),//who is the date displayed when the user picker opentfirstDate: DateTime(1950),//who is supported the previous year in pickerlastDate: DateTime.now()) //which is the previous date supported in picker.then((pickDate) {/Then usually performs a future order, if (pickDate == null) {//as a user type, it will not return ;}setState() {//to the user interface_selectedDate = pickDate ;}) ;} } }.

@overrideWidget build(BuildContext context) {return Column(children) : Button up: Text (‘Add Date’), onPressed: _pickDateDialog),SizeBox(height: 20),Text(_selectedDate == null //intermediate expression to check if the date is null? No Date Selected: Date Selected: ${DateFormat.yMMd().format(_selectedDate)},]);}}.

Solution No 6:

It’s also a very good way:

input ‘package:flutter/material.dart’ ; input
‘dart:async’ ;

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

The MyApp class expands the StatelessWidget {// This widget is the root of your [email protected] build(BuildContext context) {returns new MaterialApp(title : ‘Flutter Demo’,theme : new ThemaData(primarySwatch : Colors.blue,),home : new MyHomePage(title : ‘Flutter Date Picker Example’),) ;}} }}.

The MyHomePage class extends the StatefulWidget {
MyHomePage({key, this.title}): super(key : key);
last String title;
_MyHomePageState createState() => new _MyHomePageState();

The _MyHomePageState class expands State {
var finaldate ;

Invalid callDatePicker() async {
var order = waitit getDate();
setState(() {
finaldate = order;
} });

In the future, getDate() {// Imagine that this function/// is more complex and slower.returns showDatePicker(context : context,initialDate : DateTime.now(),firstDate: DateTime(2018),lastDate : DateTime(2030), engineer: (BuildContext, child Widget) {contains the theme (data: ThemeData.light(),child: child,);},}.

@overrideWidget build(BuildContext context) {renderen nieuwe Steiger(appBar : new AppBar(title : new Text(widget.title),),body : new Center(child : new Column(mainAxisAlignment) : MainAxisAlignment.centre, children : [Container(decoration : BoxDecoration(colour : Colors.grey [200])), padding : EdgeInsets.symmetric(horizontal : 30.0),child : finaldate == null ? Text(Use the button below to select the date,textScaleFactor : 2.0,) Text ($finaldate,textScaleFactor: 2.0,),new RaisedButton(unprinted: callDatePicker,color: Colors.blueAccent,child:new Text (‘Click here’, style: TextStyle(color: Colors.white),),),}}}.

This is from https://fluttercentral.com/Articles/Post/1199/Flutter_DatePicker_Example.

Good luck!

Related Tags:

jolly soccer ball,jolly soccer ball australia,20 inch boomer ball,planet dog orbee-tuff soccer ball,large dog ball with handle,one world soccer ball,jolly soccer ball 6,green jolly ball,jolly ball for dogs amazon,jolly ball pitbull,jolly soccer ball canada,can a soccer ball pop,flat soccer ball,chuckit soccer ball,how many inches is a soccer ball,kong flexball,one world futbol,sustainable soccer ball,indestructible soccer ball,extra large dog ball,indestructible dog ball,jolly pets soccer ball,jolly pets soccer ball 8 inch,puncture proof football,dog proof basketball