fix red screen of death caused by overlay not disposing

This commit is contained in:
Brighton van den End 2022-11-21 14:53:07 +01:00
parent 66e0090597
commit a3fcfabb01
4 changed files with 134 additions and 104 deletions

View file

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

View file

@ -7,8 +7,9 @@ import 'package:flutter_date_time_picker/flutter_date_time_picker.dart';
class DateTimePickerTheme {
/// 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({
this.paginationSize = 25,
this.weekDateBoxSize = 35,
this.monthDateBoxSize = 45,
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.
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.showWeekDays = true,
this.dateTimeConstraint = const DateTimeConstraint(),
this.onNextPageButtonChild,
this.onPreviousPageButtonChild,
this.onNextPageButtonBuilder,
this.onPreviousPageButtonBuilder,
}) : assert(child != null || buttonBuilder != null);
/// 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.
final DateTimeConstraint dateTimeConstraint;
/// a [Widget] that determents the icon of the button for going to the next page
final Widget? onNextPageButtonChild;
/// a [Function] that determents the icon of the button for going to the next page
final Widget Function(void Function()? onPressed)? onNextPageButtonBuilder;
/// a [Widget] that determents the icon of the button for going to the previous page
final Widget? onPreviousPageButtonChild;
/// a [Function] that determents the icon of the button for going to the previous page
final Widget Function(void Function()? onPressed)?
onPreviousPageButtonBuilder;
@override
State<OverlayDateTimePicker> createState() => _OverlayDateTimePickerState();
@ -144,14 +145,15 @@ class _OverlayDateTimePickerState extends State<OverlayDateTimePicker> {
@override
void dispose() {
if (_overlay.mounted) _overlay.remove();
_overlay.dispose();
_overlayState?.dispose();
_overlayState = null;
_dateTimePickerController.dispose();
super.dispose();
}
void _toggleOverlay() {
if (mounted) {
if (mounted && (_overlayState?.mounted ?? false)) {
setState(() {
if (!_isShown) {
_overlayState?.insert(_overlay);
@ -232,8 +234,8 @@ class _OverlayDateTimePickerState extends State<OverlayDateTimePicker> {
onNextDate: nextDate,
onPreviousDate: previousDate,
dateTimeConstraint: widget.dateTimeConstraint,
onNextPageButtonChild: widget.onNextPageButtonChild,
onPreviousPageButtonChild: widget.onPreviousPageButtonChild,
onNextPageButtonChild: widget.onNextPageButtonBuilder,
onPreviousPageButtonChild: widget.onPreviousPageButtonBuilder,
),
),
),

View file

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