Line data Source code
1 : import 'package:flutter/material.dart'; 2 : 3 : import 'styles/date_picker_styles.dart'; 4 : 5 : /// 6 : mixin CommonDatePickerFunctions { 7 : 8 : /// Builds widgets showing abbreviated days of week. The first widget in the 9 : /// returned list corresponds to the first day of week for the current locale. 10 : /// 11 : /// Examples: 12 : /// 13 : /// ``` 14 : /// ┌ Sunday is the first day of week in the US (en_US) 15 : /// | 16 : /// S M T W T F S <-- the returned list contains these widgets 17 : /// _ _ _ _ _ 1 2 18 : /// 3 4 5 6 7 8 9 19 : /// 20 : /// ┌ But it's Monday in the UK (en_GB) 21 : /// | 22 : /// M T W T F S S <-- the returned list contains these widgets 23 : /// _ _ _ _ 1 2 3 24 : /// 4 5 6 7 8 9 10 25 : /// ``` 26 0 : List<Widget> getDayHeaders( 27 : DayHeaderStyleBuilder headerStyleBuilder, 28 : List<String> narrowWeekdays, 29 : int firstDayOfWeekIndex) { 30 : 31 0 : final List<Widget> result = <Widget>[]; 32 : 33 0 : for (int i = firstDayOfWeekIndex; true; i = (i + 1) % 7) { 34 : DayHeaderStyle? headerStyle = headerStyleBuilder(i); 35 0 : final String weekday = narrowWeekdays[i]; 36 : 37 0 : Widget header = ExcludeSemantics( 38 0 : child: Container( 39 0 : decoration: headerStyle?.decoration, 40 0 : child: Center( 41 0 : child: Text( 42 : weekday, 43 0 : style: headerStyle?.textStyle 44 : ) 45 : ), 46 : ), 47 : ); 48 : 49 0 : result.add(header); 50 0 : if (i == (firstDayOfWeekIndex - 1) % 7) { 51 : break; 52 : } 53 : } 54 : return result; 55 : } 56 : 57 : /// Computes the offset from the first day of week that the first day of the 58 : /// [month] falls on. 59 : /// 60 : /// For example, September 1, 2017 falls on a Friday, which in the calendar 61 : /// localized for United States English appears as: 62 : /// 63 : /// ``` 64 : /// S M T W T F S 65 : /// _ _ _ _ _ 1 2 66 : /// ``` 67 : /// 68 : /// The offset for the first day of the months is the number of leading blanks 69 : /// in the calendar, i.e. 5. 70 : /// 71 : /// The same date localized for the Russian calendar has a different offset, 72 : /// because the first day of week is Monday rather than Sunday: 73 : /// 74 : /// ``` 75 : /// M T W T F S S 76 : /// _ _ _ _ 1 2 3 77 : /// ``` 78 : /// 79 : /// So the offset is 4, rather than 5. 80 : /// 81 : /// This code consolidates the following: 82 : /// 83 : /// - [DateTime.weekday] provides a 1-based index into days of week, with 1 84 : /// falling on Monday. 85 : /// - [MaterialLocalizations.firstDayOfWeekIndex] provides a 0-based index 86 : /// into the [MaterialLocalizations.narrowWeekdays] list. 87 : /// - [MaterialLocalizations.narrowWeekdays] list provides localized names of 88 : /// days of week, always starting with Sunday and ending with Saturday. 89 0 : int computeFirstDayOffset( 90 : int year, int month, int firstDayOfWeekFromSunday) { 91 : // 0-based day of week, with 0 representing Monday. 92 0 : final int weekdayFromMonday = DateTime(year, month).weekday - 1; 93 : // firstDayOfWeekFromSunday recomputed to be Monday-based 94 0 : final int firstDayOfWeekFromMonday = (firstDayOfWeekFromSunday - 1) % 7; 95 : // Number of days between the first day of week appearing on the calendar, 96 : // and the day corresponding to the 1-st of the month. 97 0 : return (weekdayFromMonday - firstDayOfWeekFromMonday) % 7; 98 : } 99 : }