简体   繁体   中英

Flutter onWillPop never called

I've read several questions relatod to this topic but any of the solutions have worked for me. This is my code. Probably thera are some other problems or improvements in the code but I'm pretty new in Flutter. But anyway, the code works.

class Calendario1 extends StatelessWidget {
  final List listaini;
  Calendario1(this.listaini);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "ATGapp",
      home:Cal1(listaini: listaini),
    );
  }
}

class Cal1 extends StatefulWidget {
  final List listaini;
  Cal1({Key? key,required this.listaini}) : super(key: key);

  @override
  ///
  _Cal1State createState() => _Cal1State();
}

class _Cal1State extends State<Cal1> {
  @override
  void initState() {
    getImage(path1);
    super.initState();
  }

  String url_1 = '';

  getImage(String path1) async {
    //String url='';
    final ref1 = FirebaseStorage.instance.ref().child(path1);
    var url1 = await ref1.getDownloadURL();
    setState(() => url_1 = url1);
  }

  final FirebaseStorage storage =
      FirebaseStorage.instance;
  String path1 = 'fondos/mons.jpeg';

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: ()async{
        print('Bck button pressed');
        return false;
      },
      child: Scaffold(
          body: Column(...//and so on

I also tried to wrap the MaterialApp widget with de WillPopScope but the result is the same, never called. Thank you in advance

The reason you face this issue is because you are not push to new page so when you pop its call it. In order to call WillPopScope, you should do this, for example load page A from MaterialApp and then navigate to page B then in page B if you push back button WillPopScope should call.

I found teh solution restructuring the app. I had every page in a different dart file and onwillpop had never called. Now, I have all my pages in one file structured by routes in MaterialApp and I'm able to cacth de android back button event.

OnWillPop Registers a callback to veto attempts by the user to dismiss the enclosing ModalRoute. Whenever the back button is pressed, you will get a callback at onWillPop, which returns a Future. If the Future returns true, the screen is popped

example:

    import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}

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

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  bool shouldPop = true;
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        return shouldPop;
      },
      child: Scaffold(
        appBar: AppBar(
          title: const Text('Flutter WillPopScope demo'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              OutlinedButton(
                child: const Text('Push'),
                onPressed: () {
                  Navigator.of(context).push<void>(
                    MaterialPageRoute<void>(
                      builder: (BuildContext context) {
                        return const MyStatefulWidget();
                      },
                    ),
                  );
                },
              ),
              OutlinedButton(
                child: Text('shouldPop: $shouldPop'),
                onPressed: () {
                  setState(
                    () {
                      shouldPop = !shouldPop;
                    },
                  );
                },
              ),
              const Text('Push to a new screen, then tap on shouldPop '
                  'button to toggle its value. Press the back '
                  'button in the appBar to check its behavior '
                  'for different values of shouldPop'),
            ],
          ),
        ),
      ),
    );
  }
}

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