Flutter v1.0.0
原生跳到Flutter页面路由的问题,官方文档也没什么详细教程,由于Flutter没有方法可以传递参数,所以现在只能依靠initialRoute的String来传递参数。

  • 原生跳Flutter页面路由传参
    可以在initialRoute添加参数,例如:demo?{“id”:112233},
    然后Flutter端 runApp(_widgetForRoute(window.defaultRouteName)),window.defaultRouteName可以拿到这个字符串(demo?{“id”:112233}),再路由并解析参数就可以了。

话不多说,直接代码:
main.dart

import 'package:flutter/material.dart';
import 'dart:ui' as ui;
import './demo.dart';

void main() {
//  debugPaintSizeEnabled=true;
  runApp(new MaterialApp(
    theme: new ThemeData(
      primarySwatch: Colors.blue,
    ),
    home: _widgetForRoute(ui.window.defaultRouteName),
  ));
}

Widget _widgetForRoute(String s) {
  print("pageName=" + _getPageName(s) + ",ParamJson=" + _getPageParamJsonStr(s));
  switch (_getPageName(s)) {
    case "demo":
      return new Demo();
  }
  return Home();
}

String _getPageName(String s) {
  if (s.indexOf("?") == -1) {
    return s;
  } else {
    return s.substring(0, s.indexOf("?"));
  }
}

String _getPageParamJsonStr(String s) {
  if (s.indexOf("?") == -1) {
    return "{}";
  } else {
    return s.substring(s.indexOf("?") + 1);
  }
}

Android端FlutterMainActivity

public class FlutterMainActivity extends FlutterActivity {

  private String routeStr = "";

  @Override
  public FlutterView createFlutterView(Context context) {
    WindowManager.LayoutParams matchParent = new WindowManager.LayoutParams(-1, -1);
    FlutterNativeView nativeView = this.createFlutterNativeView();
    FlutterView flutterView = new FlutterView(FlutterMainActivity.this, (AttributeSet) null, nativeView);
    flutterView.setInitialRoute(routeStr);
    flutterView.setLayoutParams(matchParent);
    this.setContentView(flutterView);
    return flutterView;
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    routeStr = "demo?{"id":112233}";
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
  }
}

运行结果:

frc 25b9801d5865f6c15481a00bcb97e25a - Flutter交互-原生跳Flutter页面路由传参
flutter

Flutter交互-原生跳Flutter页面路由传参