我如何才能在Flutter中将应用程序从后台带到前景

时间:2019-07-04 21:37:27

标签: flutter

我有一个正在管理音频通话的应用程序。调用添加项并且应用程序在后台运行时,我需要将应用程序置于前台状态。我尝试使用Navigator.push,但没有任何结果。

1 个答案:

答案 0 :(得分:1)

您可以使用软件包bringtoforeground。就其版本而言,它还处于早期阶段,但是可以正常工作。

iOS

但这仅适用于android ,您必须记住,背景上的iOS应用已被破坏。你可以读这个吗 see details here

Android

因此,此实现仅适用于Android。

此程序包的最好之处在于,您可以将它与Firebase Cloud Messaging(FCM)或其他任何程序一起使用。

这是他们的示例,Bringtoforeground.bringAppToForeground();这是您用于将应用程序置于前台的代码。

import 'dart:async';

import 'package:bringtoforeground/bringtoforeground.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(MyApp());

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

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // 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 {
      Timer.periodic(Duration(seconds: 10), (t) {
        Bringtoforeground.bringAppToForeground(); //This is the only thing that matters
      });
    } 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;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('Running on: $_platformVersion\n'),
        ),
      ),
    );
  }
}