flutter中的onBackPressed()

在安卓開發中有時候我們需要監聽用戶的返回鍵點擊事件,在裏面根據要求做判斷來決定是返回還是彈窗等其他操作.就像這樣:

override fun onBackPressed() {
       if (allowBack){
           super.onBackPressed()
       }else{
           do something ...
       }
    }

最近學習flutter,做了一個看圖的APP:fPix,在做下載目錄選擇功能的時候用到了《flutter版的文件管理器》的代碼,其中需要攔截返回鍵事件做判斷以回到上級目錄,開始沒有發現作者代碼中的一些邏輯,導致我按返回鍵直接退出了APP,開始是以爲需要實現onBackPressed()類似的邏輯來判斷.經過百度發現可以添加RawKeyboardListener實現按鍵事件的監聽:

class _DirSelectorState extends State<DirSelector> {
  
  FocusNode _backNode = new FocusNode();

  @override
  Widget build(BuildContext context) {
    FocusScope.of(context).requestFocus(_backNode);
    return Scaffold(
          appBar: AppBar(
            title: Text(
              parentDir?.path == sDCardDir
                  ? 'SD Card'
                  : parentDir.path.substring(parentDir.parent.path.length + 1),
              style: TextStyle(color: Colors.white),
            ),
            elevation: 0.4,
            centerTitle: true,
              //使用RawKeyboardListener進行包裹.
            leading: new RawKeyboardListener(
                focusNode: _backNode ,
                onKey: (event) {
                  if (event.runtimeType.toString() == 'RawKeyDownEvent'&&event.data is RawKeyEventDataAndroid) {
                    RawKeyEventDataAndroid data = event.data;
                    print('code=${data.keyCode}');
                      //獲取到keyCode進行判斷,4就是返回鍵
                    if(data.keyCode==4){
                        //執行相應的邏輯,回到上層或者pop
                      if (parentDir.path != sDCardDir) {
                        initDirectory(parentDir.parent.path);
                        jumpToPosition(false);
                      } else {
                        Navigator.pop(context);
                      }
                      return true;
                    }
                  }
                  return false;
                },
                child: IconButton(
                    icon: Icon(
                      Icons.chevron_left,
                      color: Colors.white,
                    ),
                    onPressed: () {
                      if (parentDir.path != sDCardDir) {
                        initDirectory(parentDir.parent.path);
                        jumpToPosition(false);
                      } else {
                        Navigator.pop(context);
                      }
                    })),
          ),
          backgroundColor: Color(0xfff3f3f3),
          body: Scrollbar(
              .......
            ),
          ));
  }
}

這樣的可以實現監聽和判斷的,但是做完了發現還是一按返回鍵就退出了,後來才發現這位作者通過其他方式做了判斷和監聽,並調用了SystemNavigator.pop();導致APP退出了,他的實現方法是添加WillPopScope,感覺比添加RawKeyboardListener方便很多.

class _DirSelectorState extends State<DirSelector> {
  
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
    //按返回鍵時這裏會被調用
      onWillPop: () {
        if (parentDir.path != sDCardDir) {
          initDirectory(parentDir.parent.path);
          jumpToPosition(false);
        } else {
        //pop當前的widget
          Navigator.pop(context);
        }
      },
      child: Scaffold(
          appBar: AppBar(
            title: Text(
              parentDir?.path == sDCardDir
                  ? 'SD Card'
                  : parentDir.path.substring(parentDir.parent.path.length + 1),
              style: TextStyle(color: Colors.white),
            ),
            elevation: 0.4,
            centerTitle: true,
            leading: IconButton(
                    icon: Icon(
                      Icons.chevron_left,
                      color: Colors.white,
                    ),
                    onPressed: () {
                      if (parentDir.path != sDCardDir) {
                        initDirectory(parentDir.parent.path);
                        jumpToPosition(false);
                      } else {
                        Navigator.pop(context);
                      }
                    })),
          ),
          backgroundColor: Color(0xfff3f3f3),
          body: Scrollbar(
            ......
    );
  }
}

這就是我現在瞭解的2種flutter中實現onBackPressed() 的方法,筆記備查.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章