如何在Flutter中的日历轮播中单击按钮颜色

时间:2019-01-24 09:47:29

标签: calendar flutter widget

我正在使用日历轮播calendar carousel小部件进行颤动。我试图单击日历上的随机日期并更改按钮颜色并保留该颜色,但是现在我无法获得此行为。这是我的代码:

import 'package:flutter/material.dart';
import 'package:father_home_flutter/model/constants.dart';
import 'package:flutter_calendar_carousel/classes/event.dart';
import 'package:flutter_calendar_carousel/classes/event_list.dart';
import 'package:flutter_calendar_carousel/flutter_calendar_carousel.dart'
    show CalendarCarousel;

class ScreenCalendar extends StatefulWidget {
  @override
  _ScreenCalendarState createState() => new _ScreenCalendarState();
}

class _ScreenCalendarState extends State<ScreenCalendar> {
  static String noEventText = "No event here";
  String calendarText = noEventText;

  @override
  void initState() {
    _markedDateMap.add(
        new DateTime(2019, 1, 25),
        new Event(
          date: new DateTime(2019, 1, 25),
          title: 'Event 5',
          icon: _eventIcon,
        ));

    _markedDateMap.add(
        new DateTime(2019, 1, 10),
        new Event(
          date: new DateTime(2019, 1, 10),
          title: 'Event 4',
          icon: _eventIcon,
        ));

    _markedDateMap.addAll(new DateTime(2019, 1, 11), [
      new Event(
        date: new DateTime(2019, 1, 11),
        title: 'Event 1',
        icon: _eventIcon,
      ),
      new Event(
        date: new DateTime(2019, 1, 11),
        title: 'Event 2',
        icon: _eventIcon,
      ),
      new Event(
        date: new DateTime(2019, 1, 11),
        title: 'Event 3',
        icon: _eventIcon,
      ),
    ]);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(
            'Календар',
            style: Constants.myTextStyleAppBar,
          ),
          iconTheme: Constants.myIconThemeDataAppBar,
          elevation: Constants.myElevationAppBar,
          backgroundColor: Constants.myAppBarColor,
        ),
        body: SingleChildScrollView(
            child: Column(children: <Widget>[
          Card(
              child: CalendarCarousel(
            weekendTextStyle: TextStyle(
              color: Colors.red,
            ),
            weekFormat: false,
            selectedDayBorderColor: Colors.green,
            markedDatesMap: _markedDateMap,
            selectedDayButtonColor: Colors.green,
            selectedDayTextStyle: TextStyle(color: Colors.green),
            todayBorderColor: Colors.transparent,
            weekdayTextStyle: TextStyle(color: Colors.black),
            height: 420.0,
            daysHaveCircularBorder: true,
            todayButtonColor: Colors.indigo,
            locale: 'RUS',
            onDayPressed: (DateTime date, List<Event> events) {
              this.setState(() => refresh(date));
            },
          )),
          Card(
              child: Container(
                  child: Padding(
                      padding: EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 16.0),
                      child: Center(
                          child: Text(
                        calendarText,
                        style: Constants.textStyleCommonText,
                      )))))
        ])));
  }

  void refresh(DateTime date) {
    print('selected date ' +
        date.day.toString() +
        date.month.toString() +
        date.year.toString() +
        ' ' +
        date.toString());
    if(_markedDateMap.getEvents(new DateTime(date.year, date.month, date.day)).isNotEmpty){
      calendarText = _markedDateMap
          .getEvents(new DateTime(date.year, date.month, date.day))[0]
          .title;
    } else{
      calendarText = noEventText;
    }
    }
  }

  EventList<Event> _markedDateMap = new EventList<Event>(events: {
    new DateTime(2019, 1, 24): [
      new Event(
        date: new DateTime(2019, 1, 24),
        title: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, '
            'sed eiusmod tempor incidunt ut labore et dolore magna aliqua.'
            ' \n\nUt enim ad minim veniam,'
            ' quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat.'
            ' \n\nQuis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. '
            'Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
        icon: _eventIcon,
      )
    ]
  });

   Widget _eventIcon = new Container(
    decoration: new BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(1000)),
        border: Border.all(color: Colors.blue, width: 2.0)),
    child: new Icon(
      Icons.person,
      color: Colors.amber,
    ),
  );

和当前行为:

enter image description here

在我正在寻找时,使用这些参数似乎不起作用:

 selectedDayBorderColor: Colors.green,
 selectedDayButtonColor: Colors.green,
 selectedDayTextStyle: TextStyle(color: Colors.green),

2 个答案:

答案 0 :(得分:1)

如果您要更改初始颜色,则可能需要从当前主题中覆盖初始颜色,尝试将日历轮播小部件包装在Theme中,并按如下所示覆盖ThemeData:< / p>

 Theme(data: ThemeData(
      splashColor: Colors.green,
    )
    child: YourCarouselWidget(),
);

此外,请确保您使用setState属性上的onDayPressed更新所选的日期。

答案 1 :(得分:0)

要解决此问题,我按照@miguelpruivo的建议做了,以这种方式解决了onDayPressed:

 onDayPressed: (DateTime date, List<Event> events) {
              this.setState(() => refresh(date));
            },

void refresh(DateTime date) {
    _currentDate = date;

也在CalendarCarousel参数中:

selectedDateTime: _currentDate,

下面是_ScreenCalendarState声明:

  DateTime _currentDate = DateTime.now();