首页 > 解决方案 > 如何使用 add2calendar 包制作动态 Flutter 按钮?

问题描述

颤振新手。我有这个按钮,按下是在设备上启动日历。我想让这个按钮动态化,因为我的 model/events.dart 列表中有 20 多个日期,并且我想启动具有特定日期的日历,例如 DateTime('${events.date}') 可能吗?有没有另一种解决方案?Vs Code 错误告诉我应该创建一个吸气剂来解决这个问题。想法?

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",
);

标签: flutterdynamiccalendarwidget

解决方案


您可以使用List<Events>

在你的 events.dart

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

在您的代码中:

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)
        },
      )
  ]
)

推荐阅读