【问题标题】:How to display pop up message in any screen in flutter?如何在颤动的任何屏幕上显示弹出消息?
【发布时间】:2021-08-17 07:49:48
【问题描述】:

我需要在任何屏幕上显示一个带有消息的弹出窗口。它可以是教程/介绍屏幕、登录或主屏幕。基本上,弹出窗口会指示该应用程序是否有新的可下载版本。现在这没有任何条件。所以我应该能够在每次启动应用程序时向用户显示弹出消息。

同样是通过在 iOS 中的 didFinishLaunchingWithOptions 中添加弹出消息来实现的。我如何在颤振中做同样的事情?

【问题讨论】:

    标签: ios flutter popup appdelegate


    【解决方案1】:

    弹出消息是通过flutter AlertDialog 小部件实现的。因此,如果您想在每次应用启动时提醒用户,您应该在应用启动的第一个屏幕中显示此小部件。这是文档中的一个示例:

    // Flutter code sample for AlertDialog
    //
    // This demo shows a [TextButton] which when pressed, calls [showDialog]. When called, this method
    // displays a Material dialog above the current contents of the app and returns
    // a [Future] that completes when the dialog is dismissed.
    
    import 'package:flutter/material.dart';
    
    void main() => runApp(const MyApp());
    
    /// This is the main application widget.
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      static const String _title = 'Flutter Code Sample';
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: _title,
          home: Scaffold(
            appBar: AppBar(title: const Text(_title)),
            body: const Center(
              child: MyStatelessWidget(),
            ),
          ),
        );
      }
    }
    
    /// This is the stateless widget that the main application instantiates.
    class MyStatelessWidget extends StatelessWidget {
      const MyStatelessWidget({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return TextButton(
          onPressed: () => showDialog<String>(
            context: context,
            builder: (BuildContext context) => AlertDialog(
              title: const Text('AlertDialog Title'),
              content: const Text('AlertDialog description'),
              actions: <Widget>[
                TextButton(
                  onPressed: () => Navigator.pop(context, 'Cancel'),
                  child: const Text('Cancel'),
                ),
                TextButton(
                  onPressed: () => Navigator.pop(context, 'OK'),
                  child: const Text('OK'),
                ),
              ],
            ),
          ),
          child: const Text('Show Dialog'),
        );
      }
    }
    

    更多信息请访问here

    【讨论】:

    • 感谢您的回答!但我想在任何地方显示登录/注册/主屏幕中的弹出窗口......我的意思是当应用程序在不同条件下启动时用户获得的屏幕......您共享的代码需要一个上下文。我应该从每个屏幕上调用它吗?在 iOS 中它非常简单......
    猜你喜欢
    • 2012-05-28
    • 2020-03-31
    • 2021-11-25
    • 2015-12-12
    • 2021-11-26
    • 2019-08-13
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多