美文网首页
flutter_android系统返回到主界面

flutter_android系统返回到主界面

作者: ChaosHeart | 来源:发表于2020-12-08 14:47 被阅读0次

    1.问题:

    怎么做到android系统返回到主界面,不退出App呢?

    2.解决:

    1.android里MainActivity类的代码:

    package com.flutter.study_app;
    
    import android.os.Bundle;
    import io.flutter.app.FlutterActivity;
    import io.flutter.plugin.common.MethodChannel;
    import io.flutter.plugins.GeneratedPluginRegistrant;
    
    public class MainActivity extends FlutterActivity {
        //通讯名称,回到手机桌面
        private final String chanel = "android/back/desktop";
        //返回手机桌面事件
        static final String eventBackDesktop = "backDesktop";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            GeneratedPluginRegistrant.registerWith(this);
    
            initBackTop();
        }
    
        //注册返回到手机桌面事件
        private void initBackTop() {
            new MethodChannel(getFlutterView(), chanel).setMethodCallHandler(
                    (methodCall, result) -> {
                        if (methodCall.method.equals(eventBackDesktop)) {
                            moveTaskToBack(false);
                            result.success(true);
                        }
                    }
            );
        }
    }
    
    

    3. flutter代码

    import 'package:flutter/services.dart';
    import 'package:flutter/material.dart';
    
    class AndroidBackTop {
      ///通讯名称,回到手机桌面
      static const String chanel = "android/back/desktop";
    
      //返回手机桌面事件
      static const String eventBackDesktop = "backDesktop";
    
      ///设置回退到手机桌面
      static Future<bool> backDesktop() async {
        final platform = MethodChannel(chanel);
        try {
          await platform.invokeMethod(eventBackDesktop);
        } on PlatformException catch (e) {
          debugPrint(e.toString());
        }
        return Future.value(false);
      }
    }
    
    

    3.使用示例

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    import 'package:study_app/views/back_desktop/utils/android_back_desktop_util.dart';
    
    class BackDesktop extends StatefulWidget {
      final String title;
    
      BackDesktop({Key key, this.title}) : super(key: key);
    
      @override
      _BackDesktopState createState() => new _BackDesktopState();
    }
    
    class _BackDesktopState extends State<BackDesktop> {
      int _count = 0;
    
      void _changeCount() {
        setState(() {
          _count++;
        });
      }
    
      void _openPage1() {}
    
      @override
      void dispose() {
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        debugPrint('xxxx');
        // TODO: implement build
        return WillPopScope(
            onWillPop: AndroidBackTop.backDesktop, //重点:页面将要消失时,调用原生的返回桌面方法
            child: new Scaffold(
              appBar: new AppBar(
                title: new Text(widget.title),
              ),
              body: new Center(
                  child: new Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  new Text("点击按钮改变数字"),
                  new Text("数字" + _count.toString(), style: Theme.of(context).textTheme.display1),
                  new FlatButton(
                    onPressed: _openPage1,
                    child: Text('打开page1'),
                    color: Theme.of(context).textSelectionColor,
                    highlightColor: Color.fromRGBO(0, 0, 255, .5),
                  )
                ],
              )),
              floatingActionButton: new FloatingActionButton(
                onPressed: _changeCount,
                tooltip: '点我改变数字',
                child: new Icon(Icons.add),
                mini: true,
              ),
              floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
            ));
      }
    }
    

    参考:
    https://blog.csdn.net/qq_30043669/article/details/84955550

    相关文章

      网友评论

          本文标题:flutter_android系统返回到主界面

          本文链接:https://www.haomeiwen.com/subject/xwofgktx.html