如何正确接收属性中的参数列表?

时间:2021-03-20 12:24:15

标签: flutter date datetime datepicker

我正在使用这个小部件库:https://pub.dev/packages/flutter_rounded_date_picker

该库可以禁用日历中的某些日期。最初在原始文档代码中,取消的日期只允许在当前日期临近时添加。这些日期通过列表中的参数传递。

listDateDisabled: [
                          DateTime.now (). Subtract (Duration (days: 2)),
                          DateTime.now (). Subtract (Duration (days: 4)),
                          DateTime.now (). Subtract (Duration (days: 6)),
                          DateTime.now (). Subtract (Duration (days: 8)),
                          DateTime.now (). Subtract (Duration (days: 10)),
                          DateTime.now (). Add (Duration (days: 2)),
                          DateTime.now (). Add (Duration (days: 4)),
                          DateTime.now (). Add (Duration (days: 6)),
                          DateTime.now (). Add (Duration (days: 8)),
                          DateTime.now (). Add (Duration (days: 10)),
                        ]);

我能够将特定日期传递给列表

listDateDisabled: [
      DateTime.parse ("2021-03-10"),
      DateTime.parse ("2021-03-11"),
      DateTime.parse ("2021-03-10"),
    ],

我相信阻止来自 API 的已安排日期的目标是显而易见的。问题是,当我将此列表转换为变量或函数时,会编译应用程序,但不会加载小部件,无论是作为本地列表还是全局列表。

var disableDays = [
      DateTime.parse ("2021-03-10"),
      DateTime.parse ("2021-03-11"),
      DateTime.parse ("2021-03-12"),
    ];
...
...
listDateDisabled: disableDays;

输入这些参数(或禁用日历中的日期)的尝试包括:

  • 使用 forEach 将项目添加到列表
  • 创建返回列表的全局函数
  • 我尝试在列表中插入不同类型的数据
  • 在无法将参数传递给属性的情况下,我尝试使用 selectableDayPredicate 函数使用 dart API 的本机日历。我使用了这个主题:How to disable list of specific date on flutter。但我无法实施该解决方案。
  • 文档中禁用了按日期搜索

在涉及代码的尝试中,小部件未加载到屏幕上,我无法将数据列表作为参数传递。这看起来是同步和异步函数的问题,但是我不知道如何在flutter中处理这些概念。我知道这里有两个问题:技术问题,即以列表形式传递参数,以及业务规则,即禁用日历中的日期。即使收到业务规则解决方案(可能在评论中),我也希望能够解决技术问题,以免其他人遇到此问题。

最终代码:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rounded_date_picker/flutter_rounded_date_picker.dart';
//import 'package:flutter_rounded_date_picker/src/material_rounded_date_picker_style.dart';
//import 'package:flutter_rounded_date_picker/src/material_rounded_year_picker_style.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  DateTime dateTime;
  Duration duration;

  @override
  void initState() {
    dateTime = DateTime.now();
    duration = Duration(minutes: 10);
    super.initState();
  }

  Future<List> processaDias() {
    List result = "nada";
    return result;
  };

  @override
  Widget build(BuildContext context) {
    Widget _buildBody() {
      return Column(
        children: <Widget>[
          Container(
            padding: const EdgeInsets.all(16),
            child: Center(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Text(
                    "Date Time selected",
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontSize: 20,
                      color: Colors.grey[600],
                    ),
                  ),
                  Text(
                    "$dateTime",
                    style: const TextStyle(fontSize: 20),
                  ),
                  Text(
                    "Duration Selected",
                    textAlign: TextAlign.center,
                    style: TextStyle(fontSize: 20, color: Colors.grey[600]),
                  ),
                  Text(
                    "$duration",
                    style: const TextStyle(fontSize: 20),
                  ),
                ],
              ),
            ),
          ),
          Expanded(
            child: ListView(
              padding: const EdgeInsets.only(bottom: 50),
              children: <Widget>[
                //const SizedBox(height: 16),

                const SizedBox(height: 12),
                const SizedBox(height: 12),
                FloatingActionButton.extended(
                  onPressed: () async {
                    DateTime newDateTime = await showRoundedDatePicker(
                        context: context,
                        theme: ThemeData(primarySwatch: Colors.blue),
                        imageHeader: AssetImage(
                          "assets/images/calendar_header_rainy.jpg",
                        ),
                        fontFamily: "Mali",
                        description:
                            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                        listDateDisabled: [
      DateTime.parse("2021-03-10"),
      DateTime.parse("2021-03-11"),
      DateTime.parse("2021-03-12"),
    ],
                        );
                    if (newDateTime != null) {
                      setState(() => dateTime = newDateTime);
                    }
                  },
                  label: const Text("Rounded Calendar and Custom Font"),
                ),

                //const SizedBox(height: 12),
              ],
            ),
          ),
        ],
      );
    }

    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('Rounded Date Picker'),
      ),
      body: Container(
        padding: EdgeInsets.symmetric(horizontal: 32),
        child: _buildBody(),
      ),
    );
  }
}

0 个答案:

没有答案
相关问题