简体   繁体   中英

how to detect appbar back and footer back button in flutter?

how to detect appbar back and footer back button in flutter?

number 01=>

How to detect AppBar Back Button in flutter? 在此处输入图像描述

number 02=>

how to detected android phone footer back button detect in flutter? 在此处输入图像描述 I am trying to catch/detect my below source code. my trying source code:

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

在此处输入图像描述

If you want to detect when the user clicked the Back button on the phone and when clicked the appBar Button, then you only have to add the WillScope to the page and leading on the AppBar with the type of button that you want and the logic that you want, like below:

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");
            }),
          ),
        ));
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM