Exemplo n.º 1
0
        public override Widget build(BuildContext context)
        {
            ThemeData             themeData     = Theme.of(context);
            MaterialLocalizations localizations = MaterialLocalizations.of(context);
            int           year           = this.displayedMonth.Year;
            int           month          = this.displayedMonth.Month;
            int           daysInMonth    = getDaysInMonth(year, month);
            int           firstDayOffset = this._computeFirstDayOffset(year, month, localizations);
            List <Widget> labels         = new List <Widget>();

            labels.AddRange(this._getDayHeaders(themeData.textTheme.caption, localizations));
            for (int i = 0; true; i += 1)
            {
                int day = i - firstDayOffset + 1;
                if (day > daysInMonth)
                {
                    break;
                }

                if (day < 1)
                {
                    labels.Add(new Container());
                }
                else
                {
                    DateTime dayToBuild = new DateTime(year, month, day);
                    bool     disabled   = dayToBuild > this.lastDate ||
                                          dayToBuild < this.firstDate ||
                                          (this.selectableDayPredicate != null &&
                                           !this.selectableDayPredicate(dayToBuild));
                    BoxDecoration decoration    = null;
                    TextStyle     itemStyle     = themeData.textTheme.body1;
                    bool          isSelectedDay = this.selectedDate.Year == year && this.selectedDate.Month == month &&
                                                  this.selectedDate.Day == day;
                    if (isSelectedDay)
                    {
                        itemStyle  = themeData.accentTextTheme.body2;
                        decoration = new BoxDecoration(
                            color: themeData.accentColor,
                            shape: BoxShape.circle
                            );
                    }
                    else if (disabled)
                    {
                        itemStyle = themeData.textTheme.body1.copyWith(color: themeData.disabledColor);
                    }
                    else if (this.currentDate.Year == year && this.currentDate.Month == month &&
                             this.currentDate.Day == day)
                    {
                        itemStyle = themeData.textTheme.body2.copyWith(color: themeData.accentColor);
                    }

                    Widget dayWidget = new Container(
                        decoration: decoration,
                        child: new Center(
                            child: new Text(localizations.formatDecimal(day), style: itemStyle)
                            )
                        );
                    if (!disabled)
                    {
                        dayWidget = new GestureDetector(
                            behavior: HitTestBehavior.opaque,
                            onTap: () => { this.onChanged(dayToBuild); },
                            child: dayWidget,
                            dragStartBehavior: this.dragStartBehavior
                            );
                    }

                    labels.Add(dayWidget);
                }
            }

            return(new Padding(
                       padding: EdgeInsets.symmetric(horizontal: 8.0f),
                       child: new Column(
                           children: new List <Widget> {
                new Container(
                    height: DatePickerUtils._kDayPickerRowHeight,
                    child: new Center(
                        child: new Text(
                            localizations.formatMonthYear(this.displayedMonth),
                            style: themeData.textTheme.subhead
                            )
                        )
                    ),
                new Flexible(
                    child: GridView.custom(
                        gridDelegate: DatePickerUtils._kDayPickerGridDelegate,
                        childrenDelegate: new SliverChildListDelegate(labels, addRepaintBoundaries: false)
                        )
                    )
            }
                           )
                       ));
        }
Exemplo n.º 2
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
                               )
                           )
                       ));
        }