コード例 #1
0
        public override Widget build(BuildContext context)
        {
            ColorScheme           colorScheme   = Theme.of(context).colorScheme;
            MaterialLocalizations localizations = MaterialLocalizations.of(context);
            TextTheme             textTheme     = Theme.of(context).textTheme;
            TextStyle             dayStyle      = textTheme.caption;
            Color enabledDayColor       = colorScheme.onSurface.withOpacity(0.87f);
            Color disabledDayColor      = colorScheme.onSurface.withOpacity(0.38f);
            Color selectedDayColor      = colorScheme.onPrimary;
            Color selectedDayBackground = colorScheme.primary;
            Color todayColor            = colorScheme.primary;

            int year  = displayedMonth.Year;
            int month = displayedMonth.Month;

            int daysInMonth = utils.getDaysInMonth(year, month);
            int dayOffset   = utils.firstDayOffset(year, month, localizations);

            List <Widget> dayItems = new List <Widget>();
            // 1-based day of month, e.g. 1-31 for January, and 1-29 for February on
            // a leap year.
            int day = -dayOffset;

            while (day < daysInMonth)
            {
                day++;
                if (day < 1)
                {
                    dayItems.Add(new Container());
                }
                else
                {
                    DateTime dayToBuild = new DateTime(year, month, day);
                    bool     isDisabled = dayToBuild > lastDate ||
                                          dayToBuild < firstDate ||
                                          (selectableDayPredicate != null && !selectableDayPredicate(dayToBuild));

                    BoxDecoration decoration    = null;
                    Color         dayColor      = enabledDayColor;
                    bool          isSelectedDay = utils.isSameDay(selectedDate, dayToBuild);
                    if (isSelectedDay)
                    {
                        // The selected day gets a circle background highlight, and a
                        // contrasting text color.
                        dayColor   = selectedDayColor;
                        decoration = new BoxDecoration(
                            color: selectedDayBackground,
                            shape: BoxShape.circle
                            );
                    }
                    else if (isDisabled)
                    {
                        dayColor = disabledDayColor;
                    }
                    else if (utils.isSameDay(currentDate, dayToBuild))
                    {
                        // The current day gets a different text color and a circle stroke
                        // border.
                        dayColor   = todayColor;
                        decoration = new BoxDecoration(
                            border: Border.all(color: todayColor, width: 1),
                            shape: BoxShape.circle
                            );
                    }

                    Widget dayWidget = new Container(
                        decoration: decoration,
                        child: new Center(
                            child: new Text(localizations.formatDecimal(day), style: dayStyle.apply(color: dayColor))
                            )
                        );

                    if (!isDisabled)
                    {
                        dayWidget = new GestureDetector(
                            behavior: HitTestBehavior.opaque,
                            onTap: () => onChanged(dayToBuild),
                            child: dayWidget
                            );
                    }

                    dayItems.Add(dayWidget);
                }
            }

            return(new Padding(
                       padding: EdgeInsets.symmetric(
                           horizontal: material_._monthPickerHorizontalPadding
                           ),
                       child: GridView.custom(
                           physics: new ClampingScrollPhysics(),
                           gridDelegate: material_._dayPickerGridDelegate,
                           childrenDelegate: new SliverChildListDelegate(
                               dayItems,
                               addRepaintBoundaries: false
                               )
                           )
                       ));
        }