一:原生頁面---->跳轉(zhuǎn)flutter頁面(方式1)
1. AndroidManifest.[xml]注冊 FlutterActivity
<activity
android:name="io.flutter.embedding.android.FlutterActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:theme="@style/Theme.AppCompat"
android:windowSoftInputMode="adjustResize" />
2.startActivity(new Intent(MainActivity.this, FlutterActivity.class));(FlutterActivity指向的是flutter系統(tǒng)main.dart頁面)
這種方式原生傳參flutter參照:https://zhuanlan.zhihu.com/p/66081592
二Android跳轉(zhuǎn)到指定的Flutter頁面半缦纭(方式2)
通過路由來跳轉(zhuǎn)峭咒,需要先聲明路由逼泣。MeterialApp中聲明路由:
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
//這種方式不能傳遞參數(shù)赎婚,主要是方便原生調(diào)用
routes: <String, WidgetBuilder>{
"login": (context) => FromPage(),
},
home: MyHomePage(title: "Flutter Demo"),
);
}
}
Android部分通過指定路由跳轉(zhuǎn):
@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='默認(rèn)From'});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(this.title), //在此處页屠,將傳遞的數(shù)據(jù)進(jìn)行引用
),
body: ListView(
children: <Widget>[
ListTile(
title: Text('我是表單'),
),
ListTile(
title: Text('我是表單'),
),
ListTile(
title: Text('我是表單'),
),
],
),
);
}
},
方式三:使用緩存的FlutterEngine跳轉(zhuǎn)到指定的Flutter頁面(解決跳轉(zhuǎn)Flutter頁面的卡頓)
在Application中設(shè)置好要緩存的頁面
public class App extends Application {
FlutterEngine flutterEngine;// 使用緩存的FlutterEngine
@Override
public void onCreate() {
super.onCreate();
// 實例化FlutterEngine
flutterEngine = new FlutterEngine(this);
// 設(shè)置要緩存的頁面
flutterEngine.getNavigationChannel().setInitialRoute("login");//這里login和Dart保持一致
// 開始執(zhí)行Dart代碼以預(yù)熱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();
}
}
原生頁面跳轉(zhuǎn)
@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: <String, WidgetBuilder>{
"login": (context) => UserLoginPage()這里login和上面Application 保持一致
},
// home: MyHomePage(title: "Flutter Demo"),
home: _widgetForRoute(window.defaultRouteName),//根據(jù)傳過來的值打開相應(yīng)頁面
);
}
}
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"),
);
}
}