簡體   English   中英

如何檢測 flutter 中的 appbar 后退和頁腳后退按鈕?

[英]how to detect appbar back and footer back button in flutter?

如何檢測 flutter 中的 appbar 后退和頁腳后退按鈕?

數字 01=>

如何檢測 flutter 中的 AppBar 后退按鈕? 在此處輸入圖像描述

數字 02=>

如何在 flutter 中檢測到 android 手機頁腳后退按鈕? 在此處輸入圖像描述 我正在嘗試捕獲/檢測我的以下源代碼。 我嘗試的源代碼:

WillPopScope(
      onWillPop: (){
       print("detect back button");
      },
)

在此處輸入圖像描述

如果您想檢測用戶何時單擊手機上的后退按鈕以及何時單擊 appBar 按鈕,那么您只需將 WillScope 添加到頁面並在 AppBar 上引導您想要的按鈕類型和邏輯你想要,如下所示:

class Example extends StatefulWidget {
  const Example({Key key}) : super(key: key);

  @override
  _ExampleState createState() => _ExampleState();
}

class _ExampleState extends State<Example> {
  Future<bool> _onWillPop() async {
    print("detect back button");
    return true;
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop:_onWillPop,
        child: Scaffold(
          appBar: AppBar(
            leading: IconButton(icon: Icon(Icons.arrow_back), onPressed: (){
              print("AppBar back button");
            }),
          ),
        ));
  }
}

暫無
暫無

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

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