ソースを参照

Handled error states in button when used in action sheet

ashilkn 2 年 前
コミット
78e1c05d93
1 ファイル変更20 行追加8 行削除
  1. 20 8
      lib/ui/components/button_widget.dart

+ 20 - 8
lib/ui/components/button_widget.dart

@@ -11,7 +11,8 @@ import 'package:photos/utils/debouncer.dart';
 enum ExecutionState {
   idle,
   inProgress,
-  successful,
+  error,
+  successful;
 }
 
 enum ButtonSize {
@@ -23,7 +24,8 @@ enum ButtonAction {
   first,
   second,
   third,
-  cancelled;
+  cancelled,
+  error;
 }
 
 typedef FutureVoidCallback = Future<void> Function();
@@ -318,9 +320,10 @@ class _ButtonChildWidgetState extends State<ButtonChildWidget> {
         });
       }),
     );
-    await widget.onTap!
-        .call()
-        .onError((error, stackTrace) => _debouncer.cancelDebounce());
+    await widget.onTap!.call().onError((error, stackTrace) {
+      executionState = ExecutionState.error;
+      _debouncer.cancelDebounce();
+    });
     _debouncer.cancelDebounce();
     // when the time taken by widget.onTap is approximately equal to the debounce
     // time, the callback is getting executed when/after the if condition
@@ -328,13 +331,14 @@ class _ButtonChildWidgetState extends State<ButtonChildWidget> {
     // idle state. This Future is for delaying the execution of the if
     // condition so that the calback in the debouncer finishes execution before.
     await Future.delayed(const Duration(milliseconds: 5));
-    if (widget.buttonAction != null) {
-      Navigator.of(context, rootNavigator: true).pop(widget.buttonAction);
-    }
     if (executionState == ExecutionState.inProgress) {
       setState(() {
         executionState = ExecutionState.successful;
         Future.delayed(const Duration(seconds: 2), () {
+          widget.buttonAction != null
+              ? Navigator.of(context, rootNavigator: true)
+                  .pop(ButtonAction.error)
+              : null;
           if (mounted) {
             setState(() {
               executionState = ExecutionState.idle;
@@ -343,6 +347,14 @@ class _ButtonChildWidgetState extends State<ButtonChildWidget> {
         });
       });
     }
+    if (executionState == ExecutionState.error) {
+      setState(() {
+        widget.buttonAction != null
+            ? Navigator.of(context, rootNavigator: true).pop(ButtonAction.error)
+            : null;
+        executionState = ExecutionState.idle;
+      });
+    }
   }
 
   void _onTapDown(details) {