flutter - Flutter Create Dropdown 月/年选择器
问题描述
我想在下图中创建月份-年份下拉选择器,我需要它位于应用栏内。我正在使用颤振。是否有任何控件,或者我必须自己编写。以及如何开始?谢谢。
解决方案
我一直在寻找和你一样的东西。你提供的截图给了我灵感,我写了:)我把它放在页面上,但你可以把它bottom
放在AppBar
. 如果任何人需要它 - 享受它,但以它为例并完成它以确保安全和稳定。
这是一个完整Scaffold
小部件的代码(我的一个页面)。
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class Time extends StatefulWidget {
@override
_TimeState createState() => _TimeState();
}
class _TimeState extends State<Time> with SingleTickerProviderStateMixin {
bool pickerIsExpanded = false;
int _pickerYear = DateTime.now().year;
DateTime _selectedMonth = DateTime(
DateTime.now().year,
DateTime.now().month,
1,
);
dynamic _pickerOpen = false;
void switchPicker() {
setState(() {
_pickerOpen ^= true;
});
}
List<Widget> generateRowOfMonths(from, to) {
List<Widget> months = [];
for (int i = from; i <= to; i++) {
DateTime dateTime = DateTime(_pickerYear, i, 1);
final backgroundColor = dateTime.isAtSameMomentAs(_selectedMonth)
? Theme.of(context).accentColor
: Colors.transparent;
months.add(
AnimatedSwitcher(
duration: kThemeChangeDuration,
transitionBuilder: (Widget child, Animation<double> animation) {
return FadeTransition(
opacity: animation,
child: child,
);
},
child: TextButton(
key: ValueKey(backgroundColor),
onPressed: () {
setState(() {
_selectedMonth = dateTime;
});
},
style: TextButton.styleFrom(
backgroundColor: backgroundColor,
shape: CircleBorder(),
),
child: Text(
DateFormat('MMM').format(dateTime),
),
),
),
);
}
return months;
}
List<Widget> generateMonths() {
return [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: generateRowOfMonths(1, 6),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: generateRowOfMonths(7, 12),
),
];
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisSize: MainAxisSize.min,
children: [
Material(
color: Theme.of(context).cardColor,
child: AnimatedSize(
curve: Curves.easeInOut,
vsync: this,
duration: Duration(milliseconds: 300),
child: Container(
height: _pickerOpen ? null : 0.0,
child: Column(
children: [
Row(
children: [
IconButton(
onPressed: () {
setState(() {
_pickerYear = _pickerYear - 1;
});
},
icon: Icon(Icons.navigate_before_rounded),
),
Expanded(
child: Center(
child: Text(
_pickerYear.toString(),
style: TextStyle(fontWeight: FontWeight.bold),
),
),
),
IconButton(
onPressed: () {
setState(() {
_pickerYear = _pickerYear + 1;
});
},
icon: Icon(Icons.navigate_next_rounded),
),
],
),
...generateMonths(),
SizedBox(
height: 10.0,
),
],
),
),
),
),
SizedBox(
height: 20.0,
),
Text(DateFormat.yMMMM().format(_selectedMonth)),
ElevatedButton(
onPressed: switchPicker,
child: Text(
'Select date',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
),
],
),
);
}
}
推荐阅读
- typescript - Set-ExecutionPolicy -ExecutionPolicy RemoteSigned 的安全风险
- r - 如何根据R中的列中存在多个值来复制行
- c - C中整数或长数据类型中数字的位置
- javascript - 如何在 knex.js 中的“join”的“on”条件下使用“and”
- javascript - 如何在 Protractor 中处理跨浏览器测试的日志记录?
- python - Pip 拒绝在 Anaconda 环境中安装
- javascript - 如何仅显示对象中需要的值对?
- react-native - 为什么 SectionList onEndReached 不起作用?
- mysql - MySQL借方和贷方计算余额的问题
- kotlin - 另一个片段中的 Recyclerview 绑定