Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - showDatePicker set first day of week to Monday

Tags:

flutter

I am using the flutter showDatePicker widget https://api.flutter.dev/flutter/material/showDatePicker.html

When the date picker popup loads by default the start of the week is Sunday, I would like to change this to Monday.

Any suggestions on how I can achieve this?

Future<void> _selectDate(BuildContext context) async {
    final DateTime picked = await showDatePicker(
      context: context,
      initialDate: _selectedDate,
      firstDate: firstDate,
      lastDate: lastDate,
      locale: const Locale('en', 'GB'),
    );
    if (picked != null && picked != _selectedDate) {
      // update the calendar slider with new date
      setState(() {
        _selectedDate = picked.add(Duration(hours: 1));
      });
    }

enter image description here

like image 592
Billy Mahmood Avatar asked Sep 17 '19 13:09

Billy Mahmood


1 Answers

Add a locale that uses Monday as the first day of the week, for example:

  Future<DateTime> selectedDate = showDatePicker(
    context: context,
    locale: const Locale('en', 'GB'),
    // etc

You have to add the localization library too, as described here, adding this to your MaterialApp:

  localizationsDelegates: GlobalMaterialLocalizations.delegates,
  supportedLocales: [
    const Locale('en', 'US'),
    const Locale('en', 'GB'),
  ],
like image 106
Richard Heap Avatar answered Oct 02 '22 16:10

Richard Heap