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