How to receive a list of parameters in a property correctly?

Asked

Viewed 75 times

1

I’m using this widgets library: https://pub.dev/packages/flutter_rounded_date_picker

The library makes it possible to disable certain dates within a calendar. Originally in the original documented code, canceled dates only allowed to be added as of the current date. These dates are passed by parameters of a list.

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

I managed to pass specific dates to the list

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

I believe the goal of blocking already scheduled dates from an API is evident. The problem is that when I turn this list into a variable or a function, the application is compiled, but the widget does not load, either as local or global list.

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

Among the attempts to insert these parameters (or disable the days in the calendar), were:

  • Add item to list using foreach
  • Create global function that returns the list
  • I tried to insert different types of data into the list
  • Unable to pass the parameters to the property, I tried using the native Dart API calendar using the selectableDayPredicate function. I used this topic: https://stackoverflow.com/questions/53647938/how-to-disable-list-of-specific-date-on-flutter . But I couldn’t implement the solution.
  • Search for date disabled in documentation

In attempts involving code, the widget was not loaded on the screen, and I cannot pass a data list as parameter. This appears to be a problem of synchronous and asynchronous functions, but I do not know how to work with these concepts in flutter. I am aware that there are two problems here: The technical, which is the passing of parameters in list form, and the business rule that is disabling dates of a calendar. Even receiving the solution of the business rule (maybe in the comments), I would like to be able to solve the technical problem so that others do not go through this problem.

Final code:

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(),
      ),
    );
  }
}

1 answer

-1


In fact, your code problem is in function processaDias() - it is with build error. Fixing this problem by migrating its method to an asynchronous function (note async at the end of the method) and initiating result as a list of strings, your code works even with dates disabled.

Its corrected function:

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

Your code running:

Código rodando

Browser other questions tagged

You are not signed in. Login or sign up in order to post.