Search code examples
flutterdynamiccalendarwidget

How to make a dynamic Flutter button with add2calendar package?


New to Flutter. I have this button and on pressed is launching the calendar on device. I want to make this button dynamic as i have more than 20 dates in my model/events.dart list and i want to launch the calendar with the specific date like DateTime('${events.date}') maybe? Is there another solution for this? Vs Code errors is telling me that i should create a getter to solve this. Thoughts?

import 'package:flutter/material.dart';
import 'package:add_2_calendar/add_2_calendar.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';
import '../model/events.dart';

class Calendar extends StatefulWidget {
  @override
  _CalendarState createState() => _CalendarState();
}

class _CalendarState extends State<Calendar> {
  final GlobalKey<ScaffoldState> scaffoldState = GlobalKey();

  @override
  Widget build(BuildContext context) {
    Event event = Event(
      title: '',
      description: '',
      location: '',
      startDate: DateTime.now(),
      endDate: DateTime.now(),
      allDay: false,
    );

    return Row(
      children: <Widget>[
        IconButton(
          icon: Icon(
            FontAwesomeIcons.calendarAlt,
            size: 30.0,
            color: Colors.green,
          ),
          onPressed: () {
            Add2Calendar.addEvent2Cal(event);
          },
        ),
      ],
    );
  }
}
import 'package:flutter/foundation.dart';

class Events {
  final String imagePath,
      title,
      description,
      location,
      duration,
      punchLine1,
      punchLine2,
      address,
      shareB,
      phone,
      fb,
      maps,
      guide,
      date;
  final List categoryIds, galleryImages;

  Events({
    @required this.imagePath,
    @required this.title,
    @required this.description,
    @required this.location,
    @required this.duration,
    @required this.punchLine1,
    @required this.punchLine2,
    @required this.categoryIds,
    @required this.galleryImages,
    @required this.address,
    @required this.shareB,
    @required this.phone,
    @required this.fb,
    @required this.maps,
    @required this.guide,
    @required this.date,
  });
}

final ponteRagogna = Events(
  imagePath: "assets/locations/ponte1.jpg",
  title: "Osteria al Ponte dal Lupo",
  description: "",
  location: "Ragogna",
  duration: "",
  punchLine1: "Osteria",
  punchLine2: " al Ponte dal Lupo",
  address: "Via al Ponte 6, Ragogna",
  maps: "46.184476,12.96572912",
  phone: "+393501073003",
  fb: "https://www.facebook.com/pages/category/Restaurant/Osteria-al-ponte-dal-lupo-2354276691519510/",
  galleryImages: [
    "assets/locations/ponte0.jpg",
    "assets/locations/ponte1.jpg",
    "assets/locations/ponte2.jpg",
  ],
  categoryIds: [
    2,
  ],
  shareB:
      "https://www.tripadvisor.it/Restaurant_Review-g666616-d3964038-Reviews-Osteria_al_Ponte-Cison_Di_Valmarino_Province_of_Treviso_Veneto.html",
  guide: "",
  date: "24-09-2020",
);

Solution

  • You can use a List<Events> :

    In your events.dart

    final events = [
      ponteRagogna,
      new Events(...)
    ];
    

    In your code :

    Row(
      children: [
        for (var staticEvent in events)
          IconButton(
            onPressed: () {
             final event = Event(
               title: '',
               description: '',
               location: '',
               startDate: DateTime(staticEvent.date),
               endDate: DateTime(staticEvent.date),
               allDay: false,
             );
             Add2Calendar.addEvent2Cal(event)
            },
          )
      ]
    )