Conversation

Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!

Join the discussion and share your insights now!

Comments 0

Sharpen your coding skills—try JavaScript challenges on TOOLX now!

advertisement

The DatePicker and TimePicker Widget in Flutter

datepicker and timepicker in flutter

This is a Material widget in a Flutter that allows the user to pick a date and time. If there is no widget available to create a date and time picker, the showDatePicker() and showTimePicker() functions can be utilized to achieve the same. These two functions display the Material Design date and time selection in a dialog by calling the built-in Flutter functions showDatePicker() and showTimePicker() respectively. Additionally, it is important to note that the date-picker constructor has three major values.

Following are some examples of values or options that can be given in an application while using DatePicker:

  • firstDate: The first date from which the user can choose. They cannot choose a date before the specified starting date.

  • initialDate: The initial date that will be selected when the dialog is first opened.

  • lastDate: The last date beyond which the user cannot select.



Time selector constructor

The time selector constructor has an initial time parameter. DatePicker cannot be used without initialTime. Additionally, the start time has a Time0fDay type that describes the time in the beginning when the user opens the dialog box.

It is important to note that initialTime can also display the current time The current time represents time as of today and will be highlighted in the time grid. If no value is specified, the Time0f Day.now() which denotes the present time and date will be used.


The use of the DatePicker Widget in FLutter:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Customized DatePicker'),
        ),
        body: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _selectedDate = 'No date chosen';
  void _pickDate() async {
    DateTime? date = await showDatePicker(
      context: context,
      initialDate: DateTime.now(),
      firstDate: DateTime(2000),
      lastDate: DateTime(2100),
      builder: (BuildContext context, Widget? child) {
        return Theme(
          data: ThemeData.light().copyWith(
            colorScheme: ColorScheme.light(
              primary: Colors.deepPurple,
              onPrimary: Colors.white,
              onSurface: Colors.black,
            ),
            dialogBackgroundColor: Colors.white,
            textButtonTheme: TextButtonThemeData(
              style: TextButton.styleFrom(
                foregroundColor: Colors.deepPurple,
              ),
            ),
          ),
          child: child!,
        );
      },
    );
    if (date != null) {
      setState(() {
        _selectedDate = date.toString();
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(
            _selectedDate,
            style: TextStyle(fontSize: 24),
          ),
          SizedBox(height: 20),
          ElevatedButton(
            onPressed: _pickDate,
            child: Text('Select Date'),
            style: ElevatedButton.styleFrom(
              primary: Colors.deepPurple,
              onPrimary: Colors.white,
              padding: EdgeInsets.symmetric(horizontal: 30, vertical: 15),
            ),
          ),
        ],
      ),
    );
  }
}


Date Picker Widget in Flutter - Complete Guide - Flutter Tutorial



The use of the TimePicker Widget in FLutter:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Styled TimePicker'),
        ),
        body: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _selectedTime = 'No time chosen';
  void _pickTime() async {
    TimeOfDay? time = await showTimePicker(
      context: context,
      initialTime: TimeOfDay.now(),
      builder: (BuildContext context, Widget? child) {
        return Theme(
          data: ThemeData.light().copyWith(
            colorScheme: ColorScheme.light(
              primary: Colors.blue,
              onPrimary: Colors.white,
              onSurface: Colors.black,
            ),
            textButtonTheme: TextButtonThemeData(
              style: TextButton.styleFrom(
                foregroundColor: Colors.blue, // button text color
              ),
            ),
          ),
          child: child!,
        );
      },
    );
    if (time != null) {
      setState(() {
        _selectedTime = time.format(context);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(_selectedTime),
          SizedBox(height: 10.0,),
          ElevatedButton(
            onPressed: _pickTime,
            child: Text('Select Time'),
          ),
        ],
      ),
    );
  }
}

Time Picker in Flutter - Complete Guide - Flutter tutorial



Flutter Widgets DatePicker Widget Date picker widget in flutter Date picker in flutter TimePicker Widget Time Picker widget in flutter Time Picker in Flutter

advertisement