flutter 权限处理程序警报对话框障碍不允许设置为false

9rbhqvlz  于 2023-03-13  发布在  Flutter
关注(0)|答案(1)|浏览(136)

为什么可以通过点击对话框外部来关闭权限对话框,这样做会导致权限被永久拒绝。如何设置可关闭的错误警报对话框。

ovfsdjhp

ovfsdjhp1#

必须在showDialog中将barrierDismissible设置为false

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(children: [
        ElevatedButton(
            onPressed: () {
              _dialogBuilder(context);
            },
            child: Text('show dialog'))
      ]),
    );
  }
}

Future<void> _dialogBuilder(BuildContext context) {
  return showDialog<void>(
    barrierDismissible: false,
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: const Text('dialog'),
        content: const Text('accept or reject'),
        actions: <Widget>[
          TextButton(
            child: const Text('accept'),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
          TextButton(
            child: const Text('reject'),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
        ],
      );
    },
  );
}

相关问题