Merge pull request #10 from Iconica-Development/hotfix/global-key-duplicate-when-switching-screen

fix red screen of death caused by global keys
This commit is contained in:
FlutterJorian 2022-11-22 09:59:11 +01:00 committed by GitHub
commit b816b4a8ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 134 additions and 104 deletions

View file

@ -20,12 +20,7 @@ class MyApp extends StatelessWidget {
theme: ThemeData( theme: ThemeData(
primarySwatch: Colors.blue, primarySwatch: Colors.blue,
), ),
home: Scaffold( home: const DatePickerDemo(),
appBar: AppBar(
title: const Text('Demo'),
),
body: const DatePickerDemo(),
),
); );
} }
} }
@ -58,9 +53,15 @@ class DatePickerDemo extends StatelessWidget {
barColor: Colors.black, barColor: Colors.black,
barOpacity: 1, barOpacity: 1,
), ),
paginationSize: 50,
); );
return Stack( return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: true,
title: const Text('Demo'),
),
body: Stack(
children: [ children: [
Center( Center(
child: Column( child: Column(
@ -104,8 +105,14 @@ class DatePickerDemo extends StatelessWidget {
), ),
), ),
), ),
onNextPageButtonChild: const Icon(Icons.add), onNextPageButtonBuilder: (onPressed) {
onPreviousPageButtonChild: const Icon(Icons.minimize), return IconButton(
onPressed: onPressed, icon: const Icon(Icons.add));
},
onPreviousPageButtonBuilder: (onPressed) {
return IconButton(
onPressed: onPressed, icon: const Icon(Icons.minimize));
},
) )
], ],
), ),
@ -138,6 +145,7 @@ class DatePickerDemo extends StatelessWidget {
markedDates: [DateTime(2022, 9, 6)], markedDates: [DateTime(2022, 9, 6)],
) )
], ],
),
); );
} }
} }

View file

@ -7,8 +7,9 @@ import 'package:flutter_date_time_picker/flutter_date_time_picker.dart';
class DateTimePickerTheme { class DateTimePickerTheme {
/// The [DateTimePickerTheme] to style [DragDownDateTimePicker] in. Define a custom shape for the dates and specifically style /// The [DateTimePickerTheme] to style [DragDownDateTimePicker] in. Define a custom shape for the dates and specifically style
/// a basic, hightlighted, selected and disabled date. /// a basic, highlighted, selected and disabled date.
const DateTimePickerTheme({ const DateTimePickerTheme({
this.paginationSize = 25,
this.weekDateBoxSize = 35, this.weekDateBoxSize = 35,
this.monthDateBoxSize = 45, this.monthDateBoxSize = 45,
this.markedIndicatorColor, this.markedIndicatorColor,
@ -75,4 +76,7 @@ class DateTimePickerTheme {
/// The position where the week view changes to month view and the other way around. Enter a value between 0 and 1 that's between the weekViewSize and the monthViewSize. /// The position where the week view changes to month view and the other way around. Enter a value between 0 and 1 that's between the weekViewSize and the monthViewSize.
final double weekMonthTriggerSize; final double weekMonthTriggerSize;
/// The size of the buttons for navigation the different pages
final double paginationSize;
} }

View file

@ -29,8 +29,8 @@ class OverlayDateTimePicker extends StatefulWidget {
this.closeOnSelectDate = true, this.closeOnSelectDate = true,
this.showWeekDays = true, this.showWeekDays = true,
this.dateTimeConstraint = const DateTimeConstraint(), this.dateTimeConstraint = const DateTimeConstraint(),
this.onNextPageButtonChild, this.onNextPageButtonBuilder,
this.onPreviousPageButtonChild, this.onPreviousPageButtonBuilder,
}) : assert(child != null || buttonBuilder != null); }) : assert(child != null || buttonBuilder != null);
/// The child contained by the DatePicker. /// The child contained by the DatePicker.
@ -84,11 +84,12 @@ class OverlayDateTimePicker extends StatefulWidget {
/// a [DateTimeConstraint] that dictates the constraints of the dates that can be picked. /// a [DateTimeConstraint] that dictates the constraints of the dates that can be picked.
final DateTimeConstraint dateTimeConstraint; final DateTimeConstraint dateTimeConstraint;
/// a [Widget] that determents the icon of the button for going to the next page /// a [Function] that determents the icon of the button for going to the next page
final Widget? onNextPageButtonChild; final Widget Function(void Function()? onPressed)? onNextPageButtonBuilder;
/// a [Widget] that determents the icon of the button for going to the previous page /// a [Function] that determents the icon of the button for going to the previous page
final Widget? onPreviousPageButtonChild; final Widget Function(void Function()? onPressed)?
onPreviousPageButtonBuilder;
@override @override
State<OverlayDateTimePicker> createState() => _OverlayDateTimePickerState(); State<OverlayDateTimePicker> createState() => _OverlayDateTimePickerState();
@ -144,14 +145,15 @@ class _OverlayDateTimePickerState extends State<OverlayDateTimePicker> {
@override @override
void dispose() { void dispose() {
if (_overlay.mounted) _overlay.remove();
_overlay.dispose(); _overlay.dispose();
_overlayState?.dispose(); _overlayState = null;
_dateTimePickerController.dispose(); _dateTimePickerController.dispose();
super.dispose(); super.dispose();
} }
void _toggleOverlay() { void _toggleOverlay() {
if (mounted) { if (mounted && (_overlayState?.mounted ?? false)) {
setState(() { setState(() {
if (!_isShown) { if (!_isShown) {
_overlayState?.insert(_overlay); _overlayState?.insert(_overlay);
@ -232,8 +234,8 @@ class _OverlayDateTimePickerState extends State<OverlayDateTimePicker> {
onNextDate: nextDate, onNextDate: nextDate,
onPreviousDate: previousDate, onPreviousDate: previousDate,
dateTimeConstraint: widget.dateTimeConstraint, dateTimeConstraint: widget.dateTimeConstraint,
onNextPageButtonChild: widget.onNextPageButtonChild, onNextPageButtonChild: widget.onNextPageButtonBuilder,
onPreviousPageButtonChild: widget.onPreviousPageButtonChild, onPreviousPageButtonChild: widget.onPreviousPageButtonBuilder,
), ),
), ),
), ),

View file

@ -32,8 +32,8 @@ class OverlayDateTimeContent extends StatefulWidget {
final bool showWeekDays; final bool showWeekDays;
final DateTimeConstraint dateTimeConstraint; final DateTimeConstraint dateTimeConstraint;
final Widget? onNextPageButtonChild; final Widget Function(void Function()? onPressed)? onNextPageButtonChild;
final Widget? onPreviousPageButtonChild; final Widget Function(void Function()? onPressed)? onPreviousPageButtonChild;
final void Function() onNextDate; final void Function() onNextDate;
final void Function() onPreviousDate; final void Function() onPreviousDate;
@ -77,13 +77,20 @@ class _OverlayDateTimeContentState extends State<OverlayDateTimeContent> {
Row( Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [ children: [
IconButton( (widget.onPreviousPageButtonChild != null)
onPressed: (widget.dateTimeConstraint.inMonthRange(previousDate)) ? widget.onPreviousPageButtonChild!(
(widget.dateTimeConstraint.inMonthRange(previousDate))
? _goToPreviousPage ? _goToPreviousPage
: null, : null,
icon: widget.onPreviousPageButtonChild ?? )
const Icon(Icons.arrow_circle_left_outlined), : IconButton(
onPressed:
(widget.dateTimeConstraint.inMonthRange(previousDate))
? _goToPreviousPage
: null,
icon: const Icon(Icons.arrow_circle_left_outlined),
color: widget.theme.barTheme.barColor, color: widget.theme.barTheme.barColor,
iconSize: widget.theme.paginationSize,
), ),
Text( Text(
DateFormat.yMMMM().format( DateFormat.yMMMM().format(
@ -91,13 +98,20 @@ class _OverlayDateTimeContentState extends State<OverlayDateTimeContent> {
), ),
style: widget.theme.baseTheme.textStyle, style: widget.theme.baseTheme.textStyle,
), ),
IconButton( (widget.onNextPageButtonChild != null)
onPressed: (widget.dateTimeConstraint.inMonthRange(nextDate)) ? widget.onNextPageButtonChild!(
(widget.dateTimeConstraint.inMonthRange(nextDate))
? _goToNextPage ? _goToNextPage
: null, : null,
icon: widget.onNextPageButtonChild ?? )
const Icon(Icons.arrow_circle_right_outlined), : IconButton(
onPressed:
(widget.dateTimeConstraint.inMonthRange(nextDate))
? _goToNextPage
: null,
icon: const Icon(Icons.arrow_circle_right_outlined),
color: widget.theme.barTheme.barColor, color: widget.theme.barTheme.barColor,
iconSize: widget.theme.paginationSize,
), ),
], ],
), ),
@ -158,6 +172,7 @@ class _OverlayDateTimeContentState extends State<OverlayDateTimeContent> {
} }
void _goToNextPage() async { void _goToNextPage() async {
if (!mounted) return;
setState(() { setState(() {
usesButtons = true; usesButtons = true;
}); });
@ -169,6 +184,7 @@ class _OverlayDateTimeContentState extends State<OverlayDateTimeContent> {
} }
void _goToPreviousPage() async { void _goToPreviousPage() async {
if (!mounted) return;
setState(() { setState(() {
usesButtons = true; usesButtons = true;
}); });