Commit 0a915d6f authored by xster's avatar xster Committed by GitHub

let the year picker start at the current date instead of the first date (#11042)

parent d9ca775f
...@@ -553,6 +553,16 @@ class YearPicker extends StatefulWidget { ...@@ -553,6 +553,16 @@ class YearPicker extends StatefulWidget {
class _YearPickerState extends State<YearPicker> { class _YearPickerState extends State<YearPicker> {
static const double _itemExtent = 50.0; static const double _itemExtent = 50.0;
ScrollController scrollController;
@override
void initState() {
super.initState();
scrollController = new ScrollController(
// Move the initial scroll position to the currently selected date's year.
initialScrollOffset: (widget.selectedDate.year - widget.firstDate.year) * _itemExtent,
);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
...@@ -560,6 +570,7 @@ class _YearPickerState extends State<YearPicker> { ...@@ -560,6 +570,7 @@ class _YearPickerState extends State<YearPicker> {
final ThemeData themeData = Theme.of(context); final ThemeData themeData = Theme.of(context);
final TextStyle style = themeData.textTheme.body1; final TextStyle style = themeData.textTheme.body1;
return new ListView.builder( return new ListView.builder(
controller: scrollController,
itemExtent: _itemExtent, itemExtent: _itemExtent,
itemCount: widget.lastDate.year - widget.firstDate.year + 1, itemCount: widget.lastDate.year - widget.firstDate.year + 1,
itemBuilder: (BuildContext context, int index) { itemBuilder: (BuildContext context, int index) {
......
...@@ -186,9 +186,9 @@ void main() { ...@@ -186,9 +186,9 @@ void main() {
await preparePicker(tester, (Future<DateTime> date) async { await preparePicker(tester, (Future<DateTime> date) async {
await tester.tap(find.text('2016')); await tester.tap(find.text('2016'));
await tester.pump(); await tester.pump();
await tester.tap(find.text('2006')); await tester.tap(find.text('2018'));
await tester.tap(find.text('OK')); await tester.tap(find.text('OK'));
expect(await date, equals(new DateTime(2006, DateTime.JANUARY, 15))); expect(await date, equals(new DateTime(2018, DateTime.JANUARY, 15)));
}); });
}); });
...@@ -196,14 +196,25 @@ void main() { ...@@ -196,14 +196,25 @@ void main() {
await preparePicker(tester, (Future<DateTime> date) async { await preparePicker(tester, (Future<DateTime> date) async {
await tester.tap(find.text('2016')); await tester.tap(find.text('2016'));
await tester.pump(); await tester.pump();
await tester.tap(find.text('2005')); await tester.tap(find.text('2017'));
await tester.pump(); await tester.pump();
final String dayLabel = new DateFormat('E, MMM\u00a0d').format(new DateTime(2005, DateTime.JANUARY, 15)); final String dayLabel = new DateFormat('E, MMM\u00a0d').format(new DateTime(2017, DateTime.JANUARY, 15));
await tester.tap(find.text(dayLabel)); await tester.tap(find.text(dayLabel));
await tester.pump(); await tester.pump();
await tester.tap(find.text('19')); await tester.tap(find.text('19'));
await tester.tap(find.text('OK')); await tester.tap(find.text('OK'));
expect(await date, equals(new DateTime(2005, DateTime.JANUARY, 19))); expect(await date, equals(new DateTime(2017, DateTime.JANUARY, 19)));
});
});
testWidgets('Current year is initially visible in year picker', (WidgetTester tester) async {
initialDate = new DateTime(2000);
firstDate = new DateTime(1900);
lastDate = new DateTime(2100);
await preparePicker(tester, (Future<DateTime> date) async {
await tester.tap(find.text('2000'));
await tester.pump();
expect(find.text('2000'), findsNWidgets(2));
}); });
}); });
...@@ -215,7 +226,7 @@ void main() { ...@@ -215,7 +226,7 @@ void main() {
await tester.tap(find.text('10')); // Earlier than firstDate. Should be ignored. await tester.tap(find.text('10')); // Earlier than firstDate. Should be ignored.
await tester.tap(find.text('20')); // Later than lastDate. Should be ignored. await tester.tap(find.text('20')); // Later than lastDate. Should be ignored.
await tester.tap(find.text('OK')); await tester.tap(find.text('OK'));
// We should still be on the inital date. // We should still be on the initial date.
expect(await date, equals(initialDate)); expect(await date, equals(initialDate));
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment