作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用以下代码启用某些日期(在本示例中为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)
工作演示
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),
),
);
}
}
关于flutter - Flutter-如何将日期列表传递给showDatePicker中的selectableDayPredicate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62547383/
我是一名优秀的程序员,十分优秀!