【问题标题】:Flutter: How do you get rid of a widget when you leave the screen or press back?Flutter:当你离开屏幕或按下返回时,你如何摆脱一个小部件?
【发布时间】:2020-12-14 08:53:50
【问题描述】:

我正在创建一个简单的聊天应用程序,并添加了一些身份验证验证。在登录或注册期间,当我从 firebase 收到错误消息时,会弹出一个小部件显示错误。当我离开屏幕时,如何确保这个小部件消失?现在,如果我按返回,它会进入欢迎屏幕,当我返回登录屏幕时,错误仍然存​​在。这是小部件-

class ErrorMessage extends StatefulWidget {
  @override
  _ErrorMessageState createState() => _ErrorMessageState();
}

class _ErrorMessageState extends State<ErrorMessage> {
  @override
  Widget build(BuildContext context) {
    if (error != null) {
      return Container(
        width: double.infinity,
        color: Colors.amberAccent,
        padding: EdgeInsets.all(8),
        child: Row(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.only(right: 8),
              child: Icon(Icons.error_outline),
            ),
            Expanded(
              child: Text(error, maxLines: 3),
            ),
            Padding(
              padding: EdgeInsets.only(left: 8),
              child: IconButton(
                icon: Icon(Icons.close),
                onPressed: () {
                  setState(() {
                    error = null;
                  });
                },
              ),
            ),
          ],
        ),
      );
    }
    return SizedBox(
      height: 0,
      width: 0,
    );
  }
}

【问题讨论】:

    标签: android-studio flutter dart


    【解决方案1】:

    我建议不要使用仅用于显示错误/警告消息的定制小部件,而是使用 awesome_dialog 之类的东西,它已经具有 autoHide 功能。

    例如,这个会在 4 秒后自行关闭:

    AwesomeDialog(
        context: context,
        animType: AnimType.LEFTSLIDE,
        dismissOnTouchOutside: true,
        dismissOnBackKeyPress: true,
        headerAnimationLoop: true,
        dialogType: DialogType.WARNING,
        autoHide: Duration(seconds: 4),  
        body: Container(
          child: Center(
             child: Text(errorMessage, maxLines: 3),
          ),
        )
    
    
    

    【讨论】:

      猜你喜欢
      • 2022-11-11
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 2019-07-14
      相关资源
      最近更新 更多