Commit 833d1995 authored by Yacumima's avatar Yacumima

本次提交扩展了example的覆盖场景,增加了两个场景:

1.在Flutter页面中需要使用Navigator.push去打开新页面,而按返回键时调用Navigator.pop,这样可以达到不退出当前Activity的效果。

2.在打开新页面时FLutter和Native相互传递页面打开参数的场景。

本次的修改只在Android平台有效,后续iOS跟进,不过做法是一样的。
parent db95bdbb
......@@ -2,6 +2,7 @@ package com.taobao.idlefish.flutterboostexample;
import com.taobao.idlefish.flutterboost.containers.BoostFlutterActivity;
import java.util.HashMap;
import java.util.Map;
import io.flutter.plugin.common.PluginRegistry;
......@@ -14,15 +15,44 @@ public class FlutterPageActivity extends BoostFlutterActivity {
GeneratedPluginRegistrant.registerWith(registry);
}
/**
* 该方法返回当前Activity在Flutter层对应的name,
* 混合栈将会在flutter层根据这个名字,在注册的Route表中查找对应的Widget
*
* 在flutter层有注册函数:
* FlutterBoost.singleton.registerPageBuilders({
* 'first': (pageName, params, _) => FirstRouteWidget(),
* 'second': (pageName, params, _) => SecondRouteWidget(),
* ...
* });
*
* 该方法中返回的就是注册的key:first , second
*
* @return
*/
@Override
public String getContainerName() {
return "flutterPage";
}
/**
* 该方法返回的参数将会传递给上层的flutter对应的Widget
*
* 在flutter层有注册函数:
* FlutterBoost.singleton.registerPageBuilders({
* 'first': (pageName, params, _) => FirstRouteWidget(),
* 'second': (pageName, params, _) => SecondRouteWidget(),
* ...
* });
*
* 该方法返回的参数就会封装成上面的params
*
* @return
*/
@Override
public Map getContainerParams() {
return null;
Map<String,String> params = new HashMap<>();
params.put("aaa","bbb");
return params;
}
}
......@@ -6,6 +6,7 @@ import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import com.taobao.idlefish.flutterboost.Debuger;
import com.taobao.idlefish.flutterboost.FlutterBoostPlugin;
import com.taobao.idlefish.flutterboost.interfaces.IPlatform;
......@@ -38,8 +39,22 @@ public class MyApplication extends FlutterApplication {
return true;
}
/**
* 如果flutter想打开一个本地页面,将会回调这个方法,页面参数将会拼接在url中
*
* 例如:sample://nativePage?aaa=bbb
*
* 参数就是类似 aaa=bbb 这样的键值对
*
* @param context
* @param url
* @param requestCode
* @return
*/
@Override
public boolean startActivity(Context context, String url, int requestCode) {
Debuger.log("startActivity url="+url);
return PageRouter.openPageByUrl(context,url,requestCode);
}
......
......@@ -20,8 +20,15 @@ class _MyAppState extends State<MyApp> {
'first': (pageName, params, _) => FirstRouteWidget(),
'second': (pageName, params, _) => SecondRouteWidget(),
'tab': (pageName, params, _) => TabRouteWidget(),
'flutterPage': (pageName, params, _) => FlutterRouteWidget(),
'flutterFragment': (pageName, params, _) => FragmentRouteWidget(params),
///可以在native层通过 getContainerParams 来传递参数
'flutterPage': (pageName, params, _){
print("flutterPage params:$params");
return FlutterRouteWidget();
},
});
FlutterBoost.handleOnStartPage();
......
......@@ -60,6 +60,10 @@ class TabRouteWidget extends StatelessWidget {
}
class FlutterRouteWidget extends StatelessWidget {
final String message;
FlutterRouteWidget({this.message});
@override
Widget build(BuildContext context) {
return Scaffold(
......@@ -72,7 +76,7 @@ class FlutterRouteWidget extends StatelessWidget {
Container(
margin: const EdgeInsets.only(top: 80.0),
child: Text(
"This is a flutter activity",
message ?? "This is a flutter activity",
style: TextStyle(fontSize: 28.0, color: Colors.blue),
),
alignment: AlignmentDirectional.center,
......@@ -87,8 +91,13 @@ class FlutterRouteWidget extends StatelessWidget {
'open native page',
style: TextStyle(fontSize: 22.0, color: Colors.black),
)),
///后面的参数会在native的IPlatform.startActivity方法回调中拼接到url的query部分。
///例如:sample://nativePage?aaa=bbb
onTap: () =>
FlutterBoost.singleton.openPage("sample://nativePage", {}),
FlutterBoost.singleton.openPage("sample://nativePage", {
"query": {"aaa": "bbb"}
}),
),
InkWell(
child: Container(
......@@ -99,8 +108,27 @@ class FlutterRouteWidget extends StatelessWidget {
'open flutter page',
style: TextStyle(fontSize: 22.0, color: Colors.black),
)),
///后面的参数会在native的IPlatform.startActivity方法回调中拼接到url的query部分。
///例如:sample://nativePage?aaa=bbb
onTap: () =>
FlutterBoost.singleton.openPage("sample://flutterPage", {}),
FlutterBoost.singleton.openPage("sample://flutterPage", {
"query": {"aaa": "bbb"}
}),
),
InkWell(
child: Container(
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
color: Colors.yellow,
child: Text(
'push flutter widget',
style: TextStyle(fontSize: 22.0, color: Colors.black),
)),
onTap: () {
Navigator.push(
context, MaterialPageRoute(builder: (_) => PushWidget()));
},
),
InkWell(
child: Container(
......@@ -192,3 +220,48 @@ class FragmentRouteWidget extends StatelessWidget {
);
}
}
class PushWidget extends StatefulWidget {
@override
_PushWidgetState createState() => _PushWidgetState();
}
class _PushWidgetState extends State<PushWidget> {
VoidCallback _backPressedListenerUnsub;
@override
void initState() {
// TODO: implement initState
super.initState();
}
@override
void didChangeDependencies() {
// TODO: implement didChangeDependencies
super.didChangeDependencies();
if (_backPressedListenerUnsub == null) {
_backPressedListenerUnsub =
BoostContainer.of(context).addBackPressedListener(() {
if (BoostContainer.of(context).onstage &&
ModalRoute.of(context).isCurrent) {
Navigator.pop(context);
}
});
}
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
_backPressedListenerUnsub?.call();
}
@override
Widget build(BuildContext context) {
return FlutterRouteWidget(
message: "pushed Widget",
);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment