Anandh Krishnan
Anandh Krishnan

Reputation: 6022

How to pass data from SystemAlertDialog into StatefulWidget Class in Flutter?

In this class I created a System alert dialog using the system_alert_window: ^1.2.1 library. It's working fine, the thing is after showing the dialog I want to get return response from the system alert dialog. Any help will be highly assist me as I trapped on this issue long time. Thanks in advance.

  1. How to get return response from system alert dialog ?
  2. There is a button named Click the button When the button gets pressed I want to know that information in the MyApp() class

pubspec.yaml

   system_alert_window: ^1.2.1

AndroidManifest.xml

 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
 <uses-permission android:name="android.permission.FOREGROUND_SERVICE " />
 <uses-permission android:name="android.permission.WAKE_LOCK" />

Main.dart

import 'package:flutter/material.dart';
import 'my_app.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

@pragma('vm:entry-point')
void callBack(String tag) {
  WidgetsFlutterBinding.ensureInitialized();
  print(tag);
  switch (tag) {
    case "btn_click":
      print("OnClick event of $tag");
      // need to notify to tha class that simple_button has been pressed
      break;
  }
}

my_app.dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:system_alert_window/system_alert_window.dart';
import 'main.dart';

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  SystemWindowPrefMode prefMode = SystemWindowPrefMode.OVERLAY;

  @override
  void initState() {
    super.initState();
    _requestPermissions();
    SystemAlertWindow.registerOnClickListener(callBack);
  }

  Future<void> _requestPermissions() async {
    await SystemAlertWindow.requestPermissions(prefMode: prefMode);
  }

  void _showOverlayWindow() {
    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: "Click the button",
                fontSize: 10,
                textColor: Colors.black45),
            tag: "btn_click"),
        buttonPosition: ButtonPosition.TRAILING);
    SystemAlertWindow.showSystemWindow(
        height: 80,
        header: header,
        margin: SystemWindowMargin(left: 8, right: 8, top: 200, bottom: 0),
        gravity: SystemWindowGravity.TOP,
        prefMode: prefMode,
        backgroundColor: Colors.black12,
        isDisableClicks: false);
  }

  @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>[
              Padding(
                padding: const EdgeInsets.symmetric(vertical: 8.0),
                child: MaterialButton(
                  onPressed: _showOverlayWindow,
                  textColor: Colors.white,
                  child: Text("Show system alert window"),
                  color: Colors.deepOrange,
                  padding: const EdgeInsets.symmetric(vertical: 8.0),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Upvotes: 2

Views: 258

Answers (0)

Related Questions