flutter和Android原生页面交互

一:原生页面---->跳转flutter页面(方式1)

参考:点击跳转

1. AndroidManifest.[xml]注册 FlutterActivity

 

2.startActivity(new Intent(MainActivity.this, FlutterActivity.class));(FlutterActivity指向的是flutter系统main.dart页面)

这种方式原生传参flutter参照:https://zhuanlan.zhihu.com/p/66081592

二Android跳转到指定的Flutter页面啊(方式2)

通过路由来跳转,需要先声明路由。MeterialApp中声明路由:

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
 //这种方式不能传递参数,主要是方便原生调用
      routes: {
        "login": (context) => FromPage(),
       
      },
      home: MyHomePage(title: "Flutter Demo"),
    );
  }
}
Android部分通过指定路由跳转:

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn2:
                startActivity(FlutterActivity
                        .withNewEngine()
                        .initialRoute("login")
//                        .initialRoute("old_password")
//                        .initialRoute("code_password")
                        .build(MainActivity.this));
                break;
        }
    }

class FromPage extends StatelessWidget{
  String title;

//dart:
  FromPage({this.title='默认From'});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(this.title),  //在此处,将传递的数据进行引用
      ),
      body: ListView(
        children: [
          ListTile(
            title: Text('我是表单'),
          ),
          ListTile(
            title: Text('我是表单'),
          ),
       
          ListTile(
            title: Text('我是表单'),
          ),

        ],
      ),
    );
  }

},

方式三:使用缓存的FlutterEngine跳转到指定的Flutter页面(解决跳转Flutter页面的卡顿)

在Application中设置好要缓存的页面

public class App extends Application {
    FlutterEngine flutterEngine;// 使用缓存的FlutterEngine
    @Override
    public void onCreate() {
        super.onCreate();
        // 实例化FlutterEngine
        flutterEngine = new FlutterEngine(this);
        // 设置要缓存的页面
        flutterEngine.getNavigationChannel().setInitialRoute("login");//这里login和Dart保持一致
        // 开始执行Dart代码以预热FlutterEngine
        flutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault());
        // 缓存FlutterActivity要使用的FlutterEngine
        FlutterEngineCache.getInstance().put("my_engine_id", flutterEngine);
    }

    @Override
    public void onTerminate() {
        //销毁flutter引擎
        flutterEngine.destroy();
        super.onTerminate();
    }

}
原生页面跳转
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn3:
                startActivity(FlutterActivity
                        .withCachedEngine("my_engine_id")
                        .build(MainActivity.this));
                break;
        }
    }

**Dart页面**
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_module/user_login_page.dart';
import 'package:flutter_module/user_update_password_page_for_code.dart';
import 'package:flutter_module/user_update_password_page_for_old.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      routes: {
        "login": (context) => UserLoginPage()这里login和上面Application 保持一致
      },
      // home: MyHomePage(title: "Flutter Demo"),
      home: _widgetForRoute(window.defaultRouteName),//根据传过来的值打开相应页面
    );
  }
}

Widget _widgetForRoute(String route) {
  switch (route) {
    case 'login':
      return Center(
        child: UserLoginPage(),
      );
    case 'old_password':
      return  Center(
          UserUpdatePasswordForOldPage()
      );
    case 'code_password':
      return Center(
          UserUpdatePasswordForCodePage()
      );
    default:
      return Center(
        child: MyHomePage(title: "123456"),
      );
  }
}

你可能感兴趣的:(flutter和Android原生页面交互)