Home > Software engineering >  How to make use of arguments in the build function flutter?
How to make use of arguments in the build function flutter?

Time:01-07

I'm trying to make use of the "Awesome Calendar" package in flutter. What I want to do is to access the tapped date from a parent widget.

AwesomeCalendarDialog(
  selectedDates: widget.habit.days.keys.toList(),
  dayTileBuilder: CustomDayTileBuilder()),
  selectionMode: SelectionMode.multi,
  canToggleRangeSelection: true,
                            )


class CustomDayTileBuilder extends DayTileBuilder {
  CustomDayTileBuilder();

  @override
  Widget build(BuildContext context, DateTime date,
      void Function(DateTime datetime)? onTap) {
    return DefaultDayTile(
      date: date,
      onTap: onTap,
      selectedDayColor: Colors.cyan,
      currentDayBorderColor: Colors.grey,
    );
  }
}



abstract class DayTileBuilder {
  Widget build(BuildContext context, DateTime date,
      void Function(DateTime datetime)? onTap);
}

How do I send an onTap argument from the AwesomeCalendarDialog via the CustomDayTileBuilder? I don't understand how to make use of the onTap in the build function.

CodePudding user response:

You don't need to declare

abstract class DayTileBuilder {
  Widget build(BuildContext context, DateTime date,
      void Function(DateTime datetime)? onTap);
}

this class is provided by the awesome_calendar package.

If you want to implement a custom builder and access the tapped date, try this:

AwesomeCalendarDialog(
  selectedDates: widget.habit.days.keys.toList(),
  dayTileBuilder: CustomDayTileBuilder(myOnTap: (date) {print(date);})),
  selectionMode: SelectionMode.multi,
  canToggleRangeSelection: true,
                            )

class CustomDayTileBuilder extends DayTileBuilder {
  CustomDayTileBuilder({required this.myOnTap});
  final void Function(DateTime datetime)? myOnTap
  @override
  Widget build(BuildContext context, DateTime date,
      void Function(DateTime datetime)? onTap) {
    return DefaultDayTile(
      date: date,
      onTap: (date) {
       print(date);
       myOnTap(date);
      },
      selectedDayColor: Colors.cyan,
      currentDayBorderColor: Colors.grey,
    );
  }
}
  • Related