From 23216a74920418e0103d40e876ef999cd420402c Mon Sep 17 00:00:00 2001 From: hegman12 Date: Wed, 30 Dec 2020 16:23:33 +0100 Subject: [PATCH] Enable external widgets to set DatePicker state --- .../ios/Flutter/flutter_export_environment.sh | 10 ++-- example/lib/main.dart | 51 ++++++++++++++++--- example/pubspec.lock | 40 +++++++-------- lib/date_picker_widget.dart | 42 ++++++++++++--- pubspec.lock | 38 +++++++------- 5 files changed, 123 insertions(+), 58 deletions(-) diff --git a/example/ios/Flutter/flutter_export_environment.sh b/example/ios/Flutter/flutter_export_environment.sh index 19dd560..adcef7a 100755 --- a/example/ios/Flutter/flutter_export_environment.sh +++ b/example/ios/Flutter/flutter_export_environment.sh @@ -1,12 +1,12 @@ #!/bin/sh # This is a generated file; do not edit or check into version control. -export "FLUTTER_ROOT=/Users/vivek/development/flutter" -export "FLUTTER_APPLICATION_PATH=/Users/vivek/StudioProjects/DatePickerTimelineFlutter/example" -export "FLUTTER_TARGET=lib/main.dart" +export "FLUTTER_ROOT=D:\flutter" +export "FLUTTER_APPLICATION_PATH=D:\gcloud_projects\flutterapp\datetime\DatePickerTimelineFlutter\example" +export "FLUTTER_TARGET=lib\main.dart" export "FLUTTER_BUILD_DIR=build" -export "SYMROOT=${SOURCE_ROOT}/../build/ios" +export "SYMROOT=${SOURCE_ROOT}/../build\ios" export "OTHER_LDFLAGS=$(inherited) -framework Flutter" -export "FLUTTER_FRAMEWORK_DIR=/Users/vivek/development/flutter/bin/cache/artifacts/engine/ios" +export "FLUTTER_FRAMEWORK_DIR=D:\flutter\bin\cache\artifacts\engine\ios" export "FLUTTER_BUILD_NAME=1.0.0" export "FLUTTER_BUILD_NUMBER=1" export "DART_OBFUSCATION=false" diff --git a/example/lib/main.dart b/example/lib/main.dart index a478f44..c67505a 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -29,7 +29,6 @@ class _MyHomePageState extends State { DateTime _selectedValue = DateTime.now(); - @override void initState() { super.initState(); @@ -38,12 +37,12 @@ class _MyHomePageState extends State { @override Widget build(BuildContext context) { return Scaffold( - floatingActionButton: FloatingActionButton( - child: Icon(Icons.replay), - onPressed: () { - _controller.animateToSelection(); - }, - ), + floatingActionButton: FloatingActionButton( + child: Icon(Icons.replay), + onPressed: () { + _controller.animateToSelection(); + }, + ), appBar: AppBar( title: Text(widget.title), ), @@ -83,6 +82,44 @@ class _MyHomePageState extends State { }, ), ), + Padding( + padding: const EdgeInsets.all(40.0), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + RaisedButton( + color: Colors.lightBlueAccent, + onPressed: () { + //Make sure within valid date range of DatePicker. + //Skipping the check here as its example + DateTime _currentDate = _controller.getCurrentDate(); + _controller.setCurrentDate( + _currentDate.add(Duration(days: 1))); + _controller.animateToSelection(); + }, + child: Text( + "Set Next day", + style: TextStyle(fontSize: 15), + ), + ), + RaisedButton( + color: Colors.lightBlueAccent, + onPressed: () { + //Make sure within valid date range of DatePicker. + //Skipping the check here as its example + DateTime _currentDate = _controller.getCurrentDate(); + _controller.setCurrentDate( + _currentDate.subtract(Duration(days: 1))); + _controller.animateToSelection(); + }, + child: Text( + "Set Previous day", + style: TextStyle(fontSize: 15), + ), + ), + ], + ), + ) ], ), )); diff --git a/example/pubspec.lock b/example/pubspec.lock index 14bb50a..f7b695b 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -7,42 +7,42 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.4.2" + version: "2.5.0-nullsafety.1" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" characters: dependency: transitive description: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.0.0" + version: "1.1.0-nullsafety.3" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.1.3" + version: "1.2.0-nullsafety.1" clock: dependency: transitive description: name: clock url: "https://pub.dartlang.org" source: hosted - version: "1.0.1" + version: "1.1.0-nullsafety.1" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.14.13" + version: "1.15.0-nullsafety.3" cupertino_icons: dependency: "direct main" description: @@ -56,14 +56,14 @@ packages: path: ".." relative: true source: path - version: "1.2.0" + version: "1.2.1" fake_async: dependency: transitive description: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0-nullsafety.1" flutter: dependency: "direct main" description: flutter @@ -87,21 +87,21 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.8" + version: "0.12.10-nullsafety.1" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.8" + version: "1.3.0-nullsafety.3" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.0-nullsafety.1" sky_engine: dependency: transitive description: flutter @@ -113,55 +113,55 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.0-nullsafety.2" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.5" + version: "1.10.0-nullsafety.1" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.5" + version: "1.1.0-nullsafety.1" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0-nullsafety.1" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.17" + version: "0.2.19-nullsafety.2" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.0-nullsafety.3" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" + version: "2.1.0-nullsafety.3" sdks: - dart: ">=2.9.0-14.0.dev <3.0.0" + dart: ">=2.10.0-110 <2.11.0" diff --git a/lib/date_picker_widget.dart b/lib/date_picker_widget.dart index 0eccf67..43b9446 100644 --- a/lib/date_picker_widget.dart +++ b/lib/date_picker_widget.dart @@ -142,6 +142,12 @@ class _DatePickerState extends State { } } + void setCurrentDate(DateTime date) { + setState(() { + _currentDate = date; + }); + } + @override Widget build(BuildContext context) { return Container( @@ -191,13 +197,19 @@ class _DatePickerState extends State { date: date, monthTextStyle: isDeactivated ? deactivatedMonthStyle - : isSelected ? selectedMonthStyle : widget.monthTextStyle, + : isSelected + ? selectedMonthStyle + : widget.monthTextStyle, dateTextStyle: isDeactivated ? deactivatedDateStyle - : isSelected ? selectedDateStyle : widget.dateTextStyle, + : isSelected + ? selectedDateStyle + : widget.dateTextStyle, dayTextStyle: isDeactivated ? deactivatedDayStyle - : isSelected ? selectedDayStyle : widget.dayTextStyle, + : isSelected + ? selectedDayStyle + : widget.dayTextStyle, width: widget.width, locale: widget.locale, selectionColor: @@ -207,12 +219,10 @@ class _DatePickerState extends State { if (isDeactivated) return; // A date is selected - if (widget.onDateChange != null ) { + if (widget.onDateChange != null) { widget.onDateChange(selectedDate); + setCurrentDate(selectedDate); } - setState(() { - _currentDate = selectedDate; - }); }, ); }, @@ -236,6 +246,24 @@ class DatePickerController { _datePickerState = state; } + void setCurrentDate(DateTime date) { + assert(_datePickerState != null, + 'DatePickerController is not attached to any DatePicker View.'); + + //Allows external widgets to set date of DatePicker. + //Propersolution would be to implement standard state management. + _datePickerState.setCurrentDate(date); + } + + DateTime getCurrentDate() { + assert(_datePickerState != null, + 'DatePickerController is not attached to any DatePicker View.'); + + //Allows external widgets to get date of DatePicker + //More propersolution would be to implement standard state management. + return _datePickerState._currentDate; + } + void jumpToSelection() { assert(_datePickerState != null, 'DatePickerController is not attached to any DatePicker View.'); diff --git a/pubspec.lock b/pubspec.lock index 0f6823a..8f49bbf 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -7,49 +7,49 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.4.2" + version: "2.5.0-nullsafety.1" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" characters: dependency: transitive description: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.0.0" + version: "1.1.0-nullsafety.3" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.1.3" + version: "1.2.0-nullsafety.1" clock: dependency: transitive description: name: clock url: "https://pub.dartlang.org" source: hosted - version: "1.0.1" + version: "1.1.0-nullsafety.1" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.14.13" + version: "1.15.0-nullsafety.3" fake_async: dependency: transitive description: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0-nullsafety.1" flutter: dependency: "direct main" description: flutter @@ -73,21 +73,21 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.8" + version: "0.12.10-nullsafety.1" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.8" + version: "1.3.0-nullsafety.3" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.0-nullsafety.1" sky_engine: dependency: transitive description: flutter @@ -99,55 +99,55 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.0-nullsafety.2" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.5" + version: "1.10.0-nullsafety.1" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.5" + version: "1.1.0-nullsafety.1" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0-nullsafety.1" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.17" + version: "0.2.19-nullsafety.2" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.0-nullsafety.3" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" + version: "2.1.0-nullsafety.3" sdks: - dart: ">=2.9.0-14.0.dev <3.0.0" + dart: ">=2.10.0-110 <2.11.0"