selectableDayPredicate

selectableDayPredicate

我正在使用以下代码启用某些日期(在本示例中为6月24日和6月25日),以便能够从DatePicker中进行选择。

   final DateTime picked = await showDatePicker(
            context: context,
            initialDate: DateTime(2020, 6, 24),
            firstDate: DateTime(2020, 6, 24),
            lastDate: DateTime(2101),
          selectableDayPredicate: (DateTime val) =>
          val == DateTime(2020, 6, 24) || val == DateTime(2020, 6, 25) ? true : false,
        );
请注意,val == DateTime(2020, 6, 24) || val == DateTime(2020, 6, 25)是硬编码的。我有日期列表,如何将其传递给selectableDayPredicate?我尝试了Anoop建议的以下方法,但是它不起作用。
List<DateTime> _dates = List();
_dates.add(DateTime(2020, 6, 24));
_dates.add(DateTime(2020, 6, 25));

 final DateTime picked = await showDatePicker(
        context: context,
        initialDate: DateTime(2020, 6, 24),
        firstDate: DateTime(2020, 6, 24),
        lastDate: DateTime(2101),
      selectableDayPredicate: (DateTime val) =>
          _dates.contains(_dates),
    );
我得到了错误:'selectableDayPredicate == null || selectableDayPredicate(initialDate)': Provided initialDate 2020-06-24 00:00:00.000 must satisfy provided selectableDayPredicate.

最佳答案

您可以在下面复制粘贴运行完整代码
你可以改变

_dates.contains(_dates)
_dates.contains(val)
工作演示
flutter - Flutter-如何将日期列表传递给showDatePicker中的selectableDayPredicate?-LMLPHP
完整的代码
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  List<DateTime> _dates = List();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _dates.add(DateTime(2020, 6, 24));
    _dates.add(DateTime(2020, 6, 25));
  }

  void _incrementCounter() async {
    final DateTime picked = await showDatePicker(
      context: context,
      initialDate: DateTime(2020, 6, 24),
      firstDate: DateTime(2020, 6, 24),
      lastDate: DateTime(2101),
      selectableDayPredicate: (DateTime val) => _dates.contains(val),
    );

    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

10-04 14:31