赞
踩
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" />
public class MainActivity extends FlutterActivity {}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
// 注意activity继承的应该是AppCompatActivity
startActivity(new Intent(MainActivity.this, FlutterActivity.class));
break;
}
}
通过路由来跳转,需要先声明路由。MeterialApp中声明路由:
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(), "old_password": (context) => UserUpdatePasswordForOldPage(), "code_password": (context) => UserUpdatePasswordForCodePage(), "login": (context) => UserLoginPage(), }, 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;
}
}
在Application实例化一个FlutterEngine:
public class App extends Application { FlutterEngine flutterEngine;// 使用缓存的FlutterEngine @Override public void onCreate() { super.onCreate(); // 实例化FlutterEngine flutterEngine = new FlutterEngine(this); // 开始执行Dart代码以预热FlutterEngine flutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault()); // 缓存FlutterActivity要使用的FlutterEngine FlutterEngineCache.getInstance().put("my_engine_id", flutterEngine); } @Override public void onTerminate() { //销毁flutterEngine 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;
}
}
在Application中设置好要缓存的页面
public class App extends Application { FlutterEngine flutterEngine;// 使用缓存的FlutterEngine @Override public void onCreate() { super.onCreate(); // 实例化FlutterEngine flutterEngine = new FlutterEngine(this); // 设置要缓存的页面 flutterEngine.getNavigationChannel().setInitialRoute("login"); // 开始执行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(); } }
Flutter入口文件:
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(), "old_password": (context) => UserUpdatePasswordForOldPage(), "code_password": (context) => UserUpdatePasswordForCodePage(), "login": (context) => UserLoginPage(), }, // 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"), ); } }
继承FlutterActivity的类:
public class FlutterAppActivity extends FlutterActivity { public final static String INIT_PARAMS = "initParams"; private String initParams; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); initParams = getIntent().getStringExtra(INIT_PARAMS); } @Override public String getInitialRoute() { return initParams == null ? super.getInitialRoute() : initParams; } public static void start(Context context, String initParams) { Intent intent = new Intent(context, FlutterAppActivity.class); intent.putExtra(INIT_PARAMS, initParams); context.startActivity(intent); } }
使用:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn4:
FlutterAppActivity.start(MainActivity.this, "login");
break;
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。