【问题标题】:Make a Flutter alertdialog when the app open应用打开时制作 Flutter alertdialog
【发布时间】:2020-03-05 21:37:14
【问题描述】:

所以我希望我的 Flutter 应用程序在应用程序打开后立即生成警报对话框,并在我摇晃手机时使其崩溃我该怎么做?

【问题讨论】:

    标签: flutter shake flutter-alertdialog


    【解决方案1】:

    我不会给你确切的解决方案,但我会尽力指导你。

    showDialog 函数可让您打开模式对话框。

    您可以在 StatefulWidgetState 中使用 initState 方法在开始时调用 showDialog(当您的页面首次构建时)。

    有一个plugin 用于检测手机震动。

    当你检测到抖动时,你应该检查对话框是否打开(可能存储一些标志),如果它打开,调用Navigator.of(context).pop();关闭它。

    UPD:好的,解决方案来了:

    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      ShakeDetector detector;
      bool dialogOpened = true;
    
      @override
      void initState() {
        WidgetsBinding.instance.addPostFrameCallback((_) {
          showDialog(
            context: context,
            builder: (cxt) => AlertDialog(content: Text('shake to close')),
          ).then((_) => dialogOpened = false);
        });
    
        detector = ShakeDetector.autoStart(
          onPhoneShake: () {
            if (dialogOpened) {
              Navigator.of(context).pop();
            }
    
            detector?.stopListening();
            detector = null;
          }
        );
    
        super.initState();
      }
    
      @override
      void dispose() {
        detector?.stopListening();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {...}
    }
    

    还将shake_event: ^0.0.4 添加到 pubspec.yaml 中的依赖项中。

    【讨论】:

    • 我不明白为什么你不给我解决我的问题的确切解决方案我知道你提到的大部分组件,在我在这里问之前尝试了很多,但没有正确的主题所以我来这里寻求解决方案
    • @MohamedEldereny 因为你的问题看起来你根本没有尝试过任何东西,这就是它被否决的原因。虽然你想要实现的并不难。
    • @MohamedEldereny 好的,解决方案来了。如果对您有帮助,请点赞/接受答案。
    猜你喜欢
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 2021-11-19
    • 1970-01-01
    • 2020-09-13
    相关资源
    最近更新 更多