Flutter Dismissible小部件,带有confirmDismiss和showAlertDialog停止应用程序

问题描述 投票:0回答:1

[当我尝试在showAlertDialog的Dismissible小部件上使用confirmDismiss时,它会弹出警报,但也会引发异常并停止应用程序。如果我尝试在异常后继续运行,则正常。

Maybe similar to this question我已经尝试使用showAlertDialog来分隔函数和不同类型的调用导航,但是没有一个可以解决它。

            Dismissible(
              confirmDismiss: (direction) {
                return showDialog(
                  context: context,
                  builder: (context) {
                    return CupertinoAlertDialog(
                      title: Text('Delete'),
                      content: Text('Delete'),
                      actions: <Widget>[
                        FlatButton(
                          onPressed: () {
                            // Navigator.pop(context, false);
                            Navigator.of(
                              context,
                              // rootNavigator: true,
                            ).pop(false);
                          },
                          child: Text('No'),
                        ),
                        FlatButton(
                          onPressed: () {
                            // Navigator.pop(context, true);
                            Navigator.of(
                              context,
                              // rootNavigator: true,
                            ).pop(true);
                          },
                          child: Text('Yes'),
                        ),
                      ],
                    );
                  },
                );
              },
              key: Key(UniqueKey().toString()),
              direction: DismissDirection.endToStart,
              onDismissed: (direction) {
                //TODO DELETE
                Scaffold.of(context).showSnackBar(
                  SnackBar(
                    backgroundColor: Theme.of(context).accentColor,
                    content: Text(
                      'test',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: 20.0,
                        color: Colors.white,
                      ),
                    ),
                  ),
                );
              },
              background: Container(
                padding: const EdgeInsets.only(right: 20.0),
                // alignment: AlignmentDirectional.centerEnd,
                alignment: Alignment.centerRight,
                color: Theme.of(context).accentColor,
                child: Icon(
                  Icons.delete,
                  size: 32.0,
                  color: Theme.of(context).primaryColor,
                ),
              ),
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 20.0),
                child: CartCard(),
              ),
            ),

这是例外

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: 'package:flutter/src/animation/animation_controller.dart': Failed assertion: line 484 pos 7: '_ticker != null': AnimationController.reverse() called after AnimationController.dispose()
AnimationController methods should not be used after calling dispose.
#0      _AssertionError._doThrowNew  (dart:core-patch/errors_patch.dart:40:39)
#1      _AssertionError._throwNew  (dart:core-patch/errors_patch.dart:36:5)
#2      AnimationController.reverse 
package:flutter/…/animation/animation_controller.dart:484
#3      _DismissibleState._handleDismissStatusChanged 
package:flutter/…/widgets/dismissible.dart:449
<asynchronous suspension>
#4      AnimationLocalStatusListenersMixin.notifyStatusListeners 
package:flutter/…/animation/listener_helpers.dart:193
#5      AnimationController._checkStatusChanged 
package:flutter/…/animation/animation_controller.dart:753
#6      AnimationController._tick (package:flutter/src/animation/animation_contr<…>
flutter flutter-layout flutter-dependencies flutter-animation
1个回答
0
投票

您的问题不在于解雇或确认解雇方法。错误警告很明显:您的问题出在动画控制器上。

显然,在代码中的某处,您正在处理它-很有可能在:

 @override dispose 

尝试将其删除,看看是否可行。

确定,请尝试此

        Dismissible(
          confirmDismiss: (direction) {
            return showDialog(
              context: context,
              builder: (context) {
                return CupertinoAlertDialog(
                  title: Text('Delete'),
                  content: Text('Delete'),
                  actions: <Widget>[
                    FlatButton(
                      onPressed: () {
                        // Navigator.pop(context, false);
                        Navigator.of(
                          context,
                          // rootNavigator: true,
                        ).pop(false);
                      },
                      child: Text('No'),
                    ),
                    FlatButton(
                      onPressed: () {
                        // Navigator.pop(context, true);
                        Navigator.of(
                          context,
                          // rootNavigator: true,
                        ).pop(true);
                      },
                      child: Text('Yes'),
                    ),
                  ],
                );
              },
            );
          },
          key: UniqueKey(),
          direction: DismissDirection.endToStart,
          onDismissed: (direction) {
            //TODO DELETE
            Scaffold.of(context).showSnackBar(
              SnackBar(
                backgroundColor: Theme.of(context).accentColor,
                content: Text(
                  'test',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 20.0,
                    color: Colors.white,
                  ),
                ),
              ),
            );
          },
          background: Container(
            padding: const EdgeInsets.only(right: 20.0),
            // alignment: AlignmentDirectional.centerEnd,
            alignment: Alignment.centerRight,
            color: Theme.of(context).accentColor,
            child: Icon(
              Icons.delete,
              size: 32.0,
              color: Theme.of(context).primaryColor,
            ),
          ),
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 20.0),
            child: CartCard(),
          ),
        ),
© www.soinside.com 2019 - 2024. All rights reserved.