wal*_*eir 2 datepicker flutter
我想在下图中创建月年下拉选择器,并且需要将其放在应用程序栏中。我正在使用颤振。有没有什么控件,或者我必须自己写。以及如何开始?谢谢。

我一直在寻找和你一样的东西。你提供的屏幕截图启发了我,我写了它:)我把它放在页面上,但你可以把它放在 的属性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,
),
),
),
],
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8568 次 |
| 最近记录: |