当前位置:   article > 正文

Flutter开发(二十八):Flutter_1.12与Android混合开发_flutterengine 预热

flutterengine 预热

1. Android 页面跳转到 Flutter 页面两种方式及传值

上一节看到无论跳转还是嵌入,速度都要一秒多,肉眼可见的慢,没有原生快很多。为了优化这个问题,flutter 提供了 FlutterEngine 来进行预热/缓存,需要跳转的时候直接找到对应 id 进行跳转即可。

1. Android 页面跳转到 Flutter 页面两种方式及传值

前面一节说了两种方式:

第一种作为 fragment 嵌入到原生页面中;

第二种是作为一个新的页面 activity 跳转进入;

下面讲的是用两种方法跳转到 flutter 然后传值。先看效果图:

 对比上一节,跳转的速度快了很多,因为用到了 FlutterEngine ,它的简单使用:设置路由和传参,使用dart executor来预热,给它一个id并且缓存;

原生 Android 项目下具体代码如下:

  1. import androidx.appcompat.app.AppCompatActivity;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.widget.Button;
  5. import android.widget.FrameLayout;
  6. import io.flutter.embedding.android.FlutterActivity;
  7. import io.flutter.embedding.android.FlutterFragment;
  8. import io.flutter.embedding.engine.FlutterEngine;
  9. import io.flutter.embedding.engine.FlutterEngineCache;
  10. import io.flutter.embedding.engine.dart.DartExecutor;
  11. public class MainActivity extends AppCompatActivity {
  12. Button tv_test,btn_test1;
  13. FrameLayout frameFlutter;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. setFlutterEngine();
  19. tv_test = findViewById(R.id.btn_test);
  20. btn_test1 = findViewById(R.id.btn_test1);
  21. frameFlutter = findViewById(R.id.frameFlutter);
  22. tv_test.setOnClickListener(new View.OnClickListener() {
  23. @Override
  24. public void onClick(View v) {
  25. //跳转activity
  26. startActivity(
  27. FlutterActivity
  28. .withCachedEngine("my_engine_id")
  29. .build(MainActivity.this)
  30. );
  31. }
  32. });
  33. btn_test1.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View v) {
  36. //fragment嵌入
  37. FlutterFragment flutterFragment = FlutterFragment.withCachedEngine("my_engine_id")
  38. .build();
  39. getSupportFragmentManager()
  40. .beginTransaction()
  41. .add(R.id.frameFlutter, flutterFragment)
  42. .commit();
  43. }
  44. });
  45. }
  46. private void setFlutterEngine() {
  47. // 初始化FlutterEngine.
  48. FlutterEngine flutterEngine = new FlutterEngine(this);
  49. //设置传参
  50. flutterEngine.getNavigationChannel().setInitialRoute("route1?{\"name\":\"" + "张三" + "\"}");
  51. // 使用dart executor来预热FlutterEngine.
  52. flutterEngine.getDartExecutor().executeDartEntrypoint(
  53. DartExecutor.DartEntrypoint.createDefault()
  54. );
  55. // 缓存FlutterEngine,并且给一个id,方便后面获取
  56. FlutterEngineCache
  57. .getInstance()
  58. .put("my_engine_id", flutterEngine);
  59. }
  60. }

flutter module 中 main.dart ,取出对应路由,并且拿到传来的参数并且显示出来,具体代码:

  1. import 'dart:ui';
  2. import 'package:flutter/material.dart';
  3. import 'dart:convert';
  4. void main() => runApp(const MyApp());
  5. class MyApp extends StatelessWidget {
  6. const MyApp({Key? key}) : super(key: key);
  7. @override
  8. Widget build(BuildContext context) {
  9. return MaterialApp(
  10. title: 'Flutter Demo',
  11. theme: ThemeData(
  12. primarySwatch: Colors.blue,
  13. ),
  14. home: const MyHomePage(title: 'Flutter Demo Home Page'),
  15. );
  16. }
  17. }
  18. class MyHomePage extends StatefulWidget {
  19. const MyHomePage({Key? key, required this.title}) : super(key: key);
  20. final String title;
  21. @override
  22. State<MyHomePage> createState() => _MyHomePageState();
  23. }
  24. class _MyHomePageState extends State<MyHomePage> {
  25. @override
  26. Widget build(BuildContext context) {
  27. return Scaffold(
  28. appBar: AppBar(
  29. title: Text(widget.title),
  30. ),
  31. body: _widgetForRoute(),// This trailing comma makes auto-formatting nicer for build methods.
  32. );
  33. }
  34. }
  35. Widget _widgetForRoute() {
  36. String route = _getRouteName(window.defaultRouteName);
  37. Map<String, dynamic> params = _getParamsStr(window.defaultRouteName);
  38. switch (route) {
  39. case 'route1':
  40. return Center(
  41. child: Text(params['name']),
  42. );
  43. default:
  44. return
  45. Center(
  46. child: Text(params['name']),
  47. );
  48. }
  49. }
  50. // 获取路由名称
  51. String _getRouteName(String s) {
  52. if (s.indexOf('?') == -1) {
  53. return s;
  54. } else {
  55. return s.substring(0, s.indexOf('?'));
  56. }
  57. }
  58. // 获取参数
  59. Map<String, dynamic> _getParamsStr(String s) {
  60. if (s.indexOf('?') == -1) {
  61. return Map();
  62. } else {
  63. return json.decode(s.substring(s.indexOf('?') + 1));
  64. }
  65. }

这样就完成了跳转以及传参。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号