【问题标题】:how to draw over other apps in flutter?如何在颤动中绘制其他应用程序?
【发布时间】:2020-09-26 05:23:12
【问题描述】:

我想即使我的 Flutter 应用关闭也显示警告框或弹出。和这张图差不多

【问题讨论】:

    标签: flutter flutter-layout flutter-dependencies


    【解决方案1】:

    你可以查看这个插件system_alert_window

    一个 Flutter 插件,用于在所有其他应用程序以及回调事件上显示 Truecaller 之类的叠加窗口。

    【讨论】:

    • 不,这不是我想要的。这显示通知
    • @WrongHack 它显示的是覆盖窗口,而不是通知。
    • @WrongHack 太长,无法在此处发布。因此,将其发布为答案
    • 它显示了覆盖窗口,但单击覆盖上的按钮时我无法进入应用程序。
    • @Vamsi 如何在窗口中显示图标并通过点击图标返回应用程序?
    【解决方案2】:

    将此添加到您的 pubspec 文件中

    dependencies:
      system_alert_window: ^0.3.2+1
    

    运行

    flutter pub get
    

    导入你的飞镖代码

    import 'package:system_alert_window/system_alert_window.dart';
    

    示例代码取自here

    import 'package:flutter/material.dart';
    import 'dart:async';
    
    import 'package:flutter/services.dart';
    import 'package:system_alert_window/system_alert_window.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      String _platformVersion = 'Unknown';
      bool _isShowingWindow = false;
      bool _isUpdatedWindow = false;
    
      @override
      void initState() {
        super.initState();
        _initPlatformState();
        _checkPermissions();
        SystemAlertWindow.registerOnClickListener(callBack);
      }
    
      // Platform messages are asynchronous, so we initialize in an async method.
      Future<void> _initPlatformState() async {
        String platformVersion;
        // Platform messages may fail, so we use a try/catch PlatformException.
        try {
          platformVersion = await SystemAlertWindow.platformVersion;
        } on PlatformException {
          platformVersion = 'Failed to get platform version.';
        }
    
        // If the widget was removed from the tree while the asynchronous platform
        // message was in flight, we want to discard the reply rather than calling
        // setState to update our non-existent appearance.
        if (!mounted) return;
    
        setState(() {
          _platformVersion = platformVersion;
        });
      }
    
      Future<void> _checkPermissions() async {
        await SystemAlertWindow.checkPermissions;
      }
    
      void _showOverlayWindow() {
        if (!_isShowingWindow) {
          SystemWindowHeader header = SystemWindowHeader(
              title: SystemWindowText(text: "Incoming Call", fontSize: 10, textColor: Colors.black45),
              padding: SystemWindowPadding.setSymmetricPadding(12, 12),
              subTitle: SystemWindowText(text: "9898989899", fontSize: 14, fontWeight: FontWeight.BOLD, textColor: Colors.black87),
              decoration: SystemWindowDecoration(startColor: Colors.grey[100]),
              button: SystemWindowButton(text: SystemWindowText(text: "Personal", fontSize: 10, textColor: Colors.black45), tag: "personal_btn"),
              buttonPosition: ButtonPosition.TRAILING);
          SystemWindowBody body = SystemWindowBody(
            rows: [
              EachRow(
                columns: [
                  EachColumn(
                    text: SystemWindowText(text: "Some body", fontSize: 12, textColor: Colors.black45),
                  ),
                ],
                gravity: ContentGravity.CENTER,
              ),
              EachRow(columns: [
                EachColumn(
                    text: SystemWindowText(text: "Long data of the body", fontSize: 12, textColor: Colors.black87, fontWeight: FontWeight.BOLD),
                    padding: SystemWindowPadding.setSymmetricPadding(6, 8),
                    decoration: SystemWindowDecoration(startColor: Colors.black12, borderRadius: 25.0),
                    margin: SystemWindowMargin(top: 4)),
              ], gravity: ContentGravity.CENTER),
              EachRow(
                columns: [
                  EachColumn(
                    text: SystemWindowText(text: "Notes", fontSize: 10, textColor: Colors.black45),
                  ),
                ],
                gravity: ContentGravity.LEFT,
                margin: SystemWindowMargin(top: 8),
              ),
              EachRow(
                columns: [
                  EachColumn(
                    text: SystemWindowText(text: "Some random notes.", fontSize: 13, textColor: Colors.black54, fontWeight: FontWeight.BOLD),
                  ),
                ],
                gravity: ContentGravity.LEFT,
              ),
            ],
            padding: SystemWindowPadding(left: 16, right: 16, bottom: 12, top: 12),
          );
          SystemWindowFooter footer = SystemWindowFooter(
              buttons: [
                SystemWindowButton(
                  text: SystemWindowText(text: "Simple button", fontSize: 12, textColor: Color.fromRGBO(250, 139, 97, 1)),
                  tag: "simple_button",
                  padding: SystemWindowPadding(left: 10, right: 10, bottom: 10, top: 10),
                  width: 0,
                  height: SystemWindowButton.WRAP_CONTENT,
                  decoration: SystemWindowDecoration(startColor: Colors.white, endColor: Colors.white, borderWidth: 0, borderRadius: 0.0),
                ),
                SystemWindowButton(
                  text: SystemWindowText(text: "Focus button", fontSize: 12, textColor: Colors.white),
                  tag: "focus_button",
                  width: 0,
                  padding: SystemWindowPadding(left: 10, right: 10, bottom: 10, top: 10),
                  height: SystemWindowButton.WRAP_CONTENT,
                  decoration: SystemWindowDecoration(
                      startColor: Color.fromRGBO(250, 139, 97, 1), endColor: Color.fromRGBO(247, 28, 88, 1), borderWidth: 0, borderRadius: 30.0),
                )
              ],
              padding: SystemWindowPadding(left: 16, right: 16, bottom: 12),
              decoration: SystemWindowDecoration(startColor: Colors.white),
              buttonsPosition: ButtonPosition.CENTER);
          SystemAlertWindow.showSystemWindow(
              height: 230,
              header: header,
              body: body,
              footer: footer,
              margin: SystemWindowMargin(left: 8, right: 8, top: 200, bottom: 0),
              gravity: SystemWindowGravity.TOP);
          setState(() {
            _isShowingWindow = true;
          });
        } else if (!_isUpdatedWindow) {
          SystemWindowHeader header = SystemWindowHeader(
              title: SystemWindowText(text: "Outgoing Call", fontSize: 10, textColor: Colors.black45),
              padding: SystemWindowPadding.setSymmetricPadding(12, 12),
              subTitle: SystemWindowText(text: "8989898989", fontSize: 14, fontWeight: FontWeight.BOLD, textColor: Colors.black87),
              decoration: SystemWindowDecoration(startColor: Colors.grey[100]),
              button: SystemWindowButton(text: SystemWindowText(text: "Personal", fontSize: 10, textColor: Colors.black45), tag: "personal_btn"),
              buttonPosition: ButtonPosition.TRAILING);
          SystemWindowBody body = SystemWindowBody(
            rows: [
              EachRow(
                columns: [
                  EachColumn(
                    text: SystemWindowText(text: "Updated body", fontSize: 12, textColor: Colors.black45),
                  ),
                ],
                gravity: ContentGravity.CENTER,
              ),
              EachRow(columns: [
                EachColumn(
                    text: SystemWindowText(text: "Updated long data of the body", fontSize: 12, textColor: Colors.black87, fontWeight: FontWeight.BOLD),
                    padding: SystemWindowPadding.setSymmetricPadding(6, 8),
                    decoration: SystemWindowDecoration(startColor: Colors.black12, borderRadius: 25.0),
                    margin: SystemWindowMargin(top: 4)),
              ], gravity: ContentGravity.CENTER),
              EachRow(
                columns: [
                  EachColumn(
                    text: SystemWindowText(text: "Notes", fontSize: 10, textColor: Colors.black45),
                  ),
                ],
                gravity: ContentGravity.LEFT,
                margin: SystemWindowMargin(top: 8),
              ),
              EachRow(
                columns: [
                  EachColumn(
                    text: SystemWindowText(text: "Updated random notes.", fontSize: 13, textColor: Colors.black54, fontWeight: FontWeight.BOLD),
                  ),
                ],
                gravity: ContentGravity.LEFT,
              ),
            ],
            padding: SystemWindowPadding(left: 16, right: 16, bottom: 12, top: 12),
          );
          SystemWindowFooter footer = SystemWindowFooter(
              buttons: [
                SystemWindowButton(
                  text: SystemWindowText(text: "Updated Simple button", fontSize: 12, textColor: Color.fromRGBO(250, 139, 97, 1)),
                  tag: "updated_simple_button",
                  padding: SystemWindowPadding(left: 10, right: 10, bottom: 10, top: 10),
                  width: 0,
                  height: SystemWindowButton.WRAP_CONTENT,
                  decoration: SystemWindowDecoration(startColor: Colors.white, endColor: Colors.white, borderWidth: 0, borderRadius: 0.0),
                ),
                SystemWindowButton(
                  text: SystemWindowText(text: "Focus button", fontSize: 12, textColor: Colors.white),
                  tag: "focus_button",
                  width: 0,
                  padding: SystemWindowPadding(left: 10, right: 10, bottom: 10, top: 10),
                  height: SystemWindowButton.WRAP_CONTENT,
                  decoration: SystemWindowDecoration(
                      startColor: Color.fromRGBO(250, 139, 97, 1), endColor: Color.fromRGBO(247, 28, 88, 1), borderWidth: 0, borderRadius: 30.0),
                )
              ],
              padding: SystemWindowPadding(left: 16, right: 16, bottom: 12),
              decoration: SystemWindowDecoration(startColor: Colors.white),
              buttonsPosition: ButtonPosition.CENTER);
          SystemAlertWindow.updateSystemWindow(
              height: 230,
              header: header,
              body: body,
              footer: footer,
              margin: SystemWindowMargin(left: 8, right: 8, top: 200, bottom: 0),
              gravity: SystemWindowGravity.TOP);
          setState(() {
            _isUpdatedWindow = true;
          });
        } else {
          setState(() {
            _isShowingWindow = false;
            _isUpdatedWindow = false;
          });
          SystemAlertWindow.closeSystemWindow();
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: const Text('System Alert Window Example App'),
            ),
            body: Center(
              child: Column(
                children: <Widget>[
                  Text('Running on: $_platformVersion\n'),
                  Padding(
                    padding: const EdgeInsets.symmetric(vertical: 8.0),
                    child: MaterialButton(
                      onPressed: _showOverlayWindow,
                      textColor: Colors.white,
                      child: !_isShowingWindow
                          ? Text("Show system alert window")
                          : !_isUpdatedWindow ? Text("Update system alert window") : Text("Close system alert window"),
                      color: Colors.deepOrange,
                      padding: const EdgeInsets.symmetric(vertical: 8.0),
                    ),
                  )
                ],
              ),
            ),
          ),
        );
      }
    }
    
    ///
    /// Whenever a button is clicked, this method will be invoked with a tag (As tag is unique for every button, it helps in identifying the button).
    /// You can check for the tag value and perform the relevant action for the button click
    ///
    void callBack(String tag) {
      print(tag);
      switch (tag) {
        case "simple_button":
        case "updated_simple_button":
          SystemAlertWindow.closeSystemWindow();
          break;
        case "focus_button":
          print("Focus button has been called");
          break;
        default:
          print("OnClick event of $tag");
      }
    }
    

    【讨论】:

    • @WrongHack 是的。我是那个插件的发布者。
    • onClick在这个包中是如何工作的,假设我想点击simple_button,然后想做一些动作或调用任何方法,那么我该怎么做。
    猜你喜欢
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    相关资源
    最近更新 更多