android 扑:返回按钮按下关闭我的应用程序,但应用程序栏回按工作正常

8yoxcaq7  于 12个月前  发布在  Android
关注(0)|答案(1)|浏览(119)

在我的代码中,硬件/底部导航回按立即关闭应用程序,而不会触发WillPopScope,但appBar回按工作正常。我希望应用程序返回到上一页或至少触发WillPopScope。我尝试了一个新的Flutter创建的应用程序和它的工作正常,但在这里,它甚至没有触发断点。下面是我的main.dart代码:

import 'package:flutter/material.dart';
import 'package:flutter_architecture/new_page.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

// This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // TRY THIS: Try running your application with "flutter run". You'll see
        // the application has a blue toolbar. Then, without quitting the app,
        // try changing the seedColor in the colorScheme below to Colors.green
        // and then invoke "hot reload" (save your changes or press the "hot
        // reload" button in a Flutter-supported IDE, or press "r" if you used
        // the command line to start the app).
        //
        // Notice that the counter didn't reset back to zero; the application
        // state is not lost during the reload. To reset the state, use hot
        // restart instead.
        //
        // This works for code too, not just values: Most code changes can be
        // tested with just a hot reload.
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  void _navigateToNewPage() {
    Navigator.of(context).push(MaterialPageRoute(builder: (context) {
      return NewPage();
    }));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline6,
            ),
            ElevatedButton(
              onPressed: _navigateToNewPage,
              child: Text('Go to New Page'),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
but5z9lq

but5z9lq1#

您可以使用

Navigator.of(context).pop(true)

或者按照这个例子:

class _OverrideBackButtonDemoState extends State<OverrideBackButtonDemo> {
        Future<bool> _onWillPop() async {
          return (await showDialog(
                context: context,
                builder: (context) => AlertDialog(
                  title: new Text('Are you sure?'),
                  content: new Text('Do you want to exit an App'),
                  actions: <Widget>[
                    TextButton(
                      onPressed: () => Navigator.of(context).pop(false), //<-- SEE HERE
                      child: new Text('No'),
                    ),
                    TextButton(
                      onPressed: () => Navigator.of(context).pop(true), // <-- SEE HERE
                      child: new Text('Yes'),
                    ),
                  ],
                ),
              )) ??
              false;
        }
          @override
          Widget build(BuildContext context) {
            return WillPopScope(
              onWillPop: _onWillPop,
              child: Scaffold(
                appBar: ...
                body: Center(
                  child: Column(
                    children: [
                      SizedBox(
                        height: 50,
                      ),
                      Text(
                        'Home Screen',
                        style: TextStyle(fontSize: 50),
                      ),
                    ],
                  ),
                ),
              ),
            );
          }
        }

相关问题