fixed AnimatedDialogPicker inability to close

This commit is contained in:
Vadim 2023-05-16 10:16:12 +02:00
parent 1a7fc2e251
commit 017c7da88a

View file

@ -3,8 +3,9 @@ import 'package:flutter/material.dart';
import 'package:lightmeter/screens/metering/components/shared/readings_container/components/animated_dialog_picker/components/animated_dialog/widget_dialog_animated.dart'; import 'package:lightmeter/screens/metering/components/shared/readings_container/components/animated_dialog_picker/components/animated_dialog/widget_dialog_animated.dart';
import 'package:lightmeter/screens/metering/components/shared/readings_container/components/animated_dialog_picker/components/dialog_picker/widget_picker_dialog.dart'; import 'package:lightmeter/screens/metering/components/shared/readings_container/components/animated_dialog_picker/components/dialog_picker/widget_picker_dialog.dart';
class AnimatedDialogPicker<T> extends StatelessWidget { // Has to be stateful, so that [GlobalKey] is not recreated.
final _key = GlobalKey<AnimatedDialogState>(); // Otherwise use will no be able to close the dialog after EV value has changed.
class AnimatedDialogPicker<T> extends StatefulWidget {
final IconData icon; final IconData icon;
final String title; final String title;
final String? subtitle; final String? subtitle;
@ -15,7 +16,7 @@ class AnimatedDialogPicker<T> extends StatelessWidget {
final ValueChanged<T> onChanged; final ValueChanged<T> onChanged;
final Widget closedChild; final Widget closedChild;
AnimatedDialogPicker({ const AnimatedDialogPicker({
required this.icon, required this.icon,
required this.title, required this.title,
this.subtitle, this.subtitle,
@ -28,24 +29,31 @@ class AnimatedDialogPicker<T> extends StatelessWidget {
super.key, super.key,
}); });
@override
State<AnimatedDialogPicker<T>> createState() => _AnimatedDialogPickerState<T>();
}
class _AnimatedDialogPickerState<T> extends State<AnimatedDialogPicker<T>> {
final _key = GlobalKey<AnimatedDialogState>();
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return AnimatedDialog( return AnimatedDialog(
key: _key, key: _key,
closedChild: closedChild, closedChild: widget.closedChild,
openedChild: DialogPicker<T>( openedChild: DialogPicker<T>(
icon: icon, icon: widget.icon,
title: title, title: widget.title,
subtitle: subtitle, subtitle: widget.subtitle,
initialValue: selectedValue, initialValue: widget.selectedValue,
values: values, values: widget.values,
itemTitleBuilder: itemTitleBuilder, itemTitleBuilder: widget.itemTitleBuilder,
itemTrailingBuilder: itemTrailingBuilder, itemTrailingBuilder: widget.itemTrailingBuilder,
onCancel: () { onCancel: () {
_key.currentState?.close(); _key.currentState?.close();
}, },
onSelect: (value) { onSelect: (value) {
_key.currentState?.close().then((_) => onChanged(value)); _key.currentState?.close().then((_) => widget.onChanged(value));
}, },
), ),
); );