簡體   English   中英

Flutter:當你離開屏幕或按下返回時,你如何擺脫一個小部件?

[英]Flutter: How do you get rid of a widget when you leave the screen or press back?

我正在創建一個簡單的聊天應用程序,並添加了一些身份驗證驗證。 在登錄或注冊期間,當我從 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,
    );
  }
}

而不是使用只是為了顯示錯誤/警告消息控件一個定制的,我建議使用類似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),
      ),
    )


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM