Commit c7fe899c authored by yangwu.jia's avatar yangwu.jia

1.9 demo优化

parent 3c3abf9f
......@@ -31,6 +31,7 @@ import io.flutter.embedding.engine.FlutterShellArgs;
import io.flutter.plugin.platform.PlatformPlugin;
import io.flutter.view.FlutterMain;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
......@@ -71,7 +72,7 @@ public class NewBoostFlutterActivity extends Activity
private final Class<? extends NewBoostFlutterActivity> activityClass;
private String backgroundMode = DEFAULT_BACKGROUND_MODE;
private String url = "";
private HashMap params = new HashMap();
private Map params = new HashMap();
......@@ -88,7 +89,7 @@ public class NewBoostFlutterActivity extends Activity
}
public NewEngineIntentBuilder params (@NonNull HashMap params) {
public NewEngineIntentBuilder params (@NonNull Map params) {
this.params = params;
return this;
}
......@@ -102,15 +103,30 @@ public class NewBoostFlutterActivity extends Activity
public Intent build(@NonNull Context context) {
SerializableMap serializableMap=new SerializableMap();
serializableMap.setMap(params);
return new Intent(context, activityClass)
.putExtra(EXTRA_BACKGROUND_MODE, backgroundMode)
.putExtra(EXTRA_DESTROY_ENGINE_WITH_ACTIVITY, false)
.putExtra(EXTRA_URL, url)
.putExtra(EXTRA_PARAMS, (HashMap)params);
.putExtra(EXTRA_PARAMS, serializableMap);
}
}
public static class SerializableMap implements Serializable {
private Map<String,Object> map;
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
}
private FlutterActivityAndFragmentDelegate delegate;
......@@ -476,7 +492,8 @@ public class NewBoostFlutterActivity extends Activity
public Map getContainerUrlParams() {
if (getIntent().hasExtra(EXTRA_PARAMS)) {
return (Map) getIntent().getSerializableExtra(EXTRA_PARAMS);
SerializableMap serializableMap= (SerializableMap)getIntent().getSerializableExtra(EXTRA_PARAMS);
return serializableMap.getMap();
}
Map<String,String> params = new HashMap<>();
......
......@@ -115,7 +115,7 @@ public class NewFlutterFragment extends Fragment implements FlutterActivityAndFr
private FlutterView.TransparencyMode transparencyMode = FlutterView.TransparencyMode.transparent;
private boolean shouldAttachEngineToActivity = true;
private String url = "";
private HashMap params = new HashMap();
private Map params = new HashMap();
/**
* Constructs a {@code NewEngineFragmentBuilder} that is configured to construct an instance of
* {@code NewFlutterFragment}.
......@@ -162,7 +162,7 @@ public class NewFlutterFragment extends Fragment implements FlutterActivityAndFr
}
public NewEngineFragmentBuilder params (@NonNull HashMap params) {
public NewEngineFragmentBuilder params (@NonNull Map params) {
this.params = params;
return this;
}
......@@ -188,11 +188,17 @@ public class NewFlutterFragment extends Fragment implements FlutterActivityAndFr
if (null != shellArgs) {
args.putStringArray(ARG_FLUTTER_INITIALIZATION_ARGS, shellArgs.toArray());
}
NewBoostFlutterActivity.SerializableMap serializableMap=new NewBoostFlutterActivity.SerializableMap();
serializableMap.setMap(params);
args.putString(EXTRA_URL, url);
args.putSerializable(EXTRA_PARAMS, (HashMap)params);
args.putSerializable(EXTRA_PARAMS, serializableMap);
args.putString(ARG_FLUTTERVIEW_RENDER_MODE, renderMode != null ? renderMode.name() : FlutterView.RenderMode.surface.name());
args.putString(ARG_FLUTTERVIEW_TRANSPARENCY_MODE, transparencyMode != null ? transparencyMode.name() : FlutterView.TransparencyMode.transparent.name());
args.putBoolean(ARG_DESTROY_ENGINE_WITH_FRAGMENT, true);
return args;
}
......@@ -532,7 +538,10 @@ public class NewFlutterFragment extends Fragment implements FlutterActivityAndFr
@Override
public Map getContainerUrlParams() {
return (HashMap) getArguments().getSerializable(EXTRA_PARAMS);
NewBoostFlutterActivity.SerializableMap serializableMap= (NewBoostFlutterActivity.SerializableMap) getArguments().getSerializable(EXTRA_PARAMS);
return serializableMap.getMap();
}
/**
......
......@@ -45,6 +45,8 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
@Override
public void onClick(View v) {
Map params = new HashMap();
params.put("test1","v_test1");
params.put("test2","v_test2");
//Add some params if needed.
if (v == mOpenNative) {
PageRouter.openPageByUrl(this, PageRouter.NATIVE_PAGE_URL , params);
......
......@@ -33,6 +33,9 @@ public class NativePageActivity extends AppCompatActivity implements View.OnClic
@Override
public void onClick(View v) {
Map params = new HashMap();
params.put("test1","v_test1");
params.put("test2","v_test2");
if (v == mOpenNative) {
PageRouter.openPageByUrl(this, PageRouter.NATIVE_PAGE_URL,params);
} else if (v == mOpenFlutter) {
......
......@@ -3,6 +3,7 @@ package com.taobao.idlefish.flutterboostexample;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import com.idlefish.flutterboost.containers.NewBoostFlutterActivity;
import java.util.HashMap;
......@@ -10,24 +11,34 @@ import java.util.Map;
public class PageRouter {
public final static Map<String, String> pageName = new HashMap<String, String>() {{
put("sample://flutterPage1", "flutterPage1");
put("sample://flutterPage2", "flutterPage2");
put("sample://flutterPage3", "flutterPage3");
put("sample://flutterMain", "flutterMain");
}};
public static final String NATIVE_PAGE_URL = "sample://nativePage";
public static final String FLUTTER_PAGE_URL = "sample://flutterPage";
public static final String FLUTTER_PAGE_URL = "sample://flutterMain";
public static final String FLUTTER_FRAGMENT_PAGE_URL = "sample://flutterFragmentPage";
public static boolean openPageByUrl(Context context, String url,Map params) {
return openPageByUrl(context, url,params, 0);
public static boolean openPageByUrl(Context context, String url, Map params) {
return openPageByUrl(context, url, params, 0);
}
public static boolean openPageByUrl(Context context, String url, Map params, int requestCode) {
try {
if (url.startsWith(FLUTTER_PAGE_URL)) {
HashMap p=new HashMap();
Intent intent= NewBoostFlutterActivity.withNewEngine().url("flutterPage").params(p)
String path = url.split("\\?")[0];
Log.i("openPageByUrl",path);
try {
if (pageName.containsKey(path)) {
Intent intent = NewBoostFlutterActivity.withNewEngine().url(pageName.get(path)).params(params)
.backgroundMode(NewBoostFlutterActivity.BackgroundMode.opaque).build(context);
context.startActivity( intent);
return true;
context.startActivity(intent);
} else if (url.startsWith(FLUTTER_FRAGMENT_PAGE_URL)) {
context.startActivity(new Intent(context, FlutterFragmentPageActivity.class));
return true;
......@@ -40,5 +51,6 @@ public class PageRouter {
} catch (Throwable t) {
return false;
}
return false;
}
}
......@@ -17,16 +17,15 @@ class _MyAppState extends State<MyApp> {
super.initState();
FlutterBoost.singleton.registerPageBuilders({
'first': (pageName, params, _) => FirstRouteWidget(),
'second': (pageName, params, _) => SecondRouteWidget(),
'tab': (pageName, params, _) => TabRouteWidget(),
'flutterPage1': (pageName, params, _) => FirstRouteWidget(),
'flutterPage2': (pageName, params, _) => SecondRouteWidget(),
'flutterPage3': (pageName, params, _) => TabRouteWidget(),
'flutterFragment': (pageName, params, _) => FragmentRouteWidget(params),
///可以在native层通过 getContainerParams 来传递参数
'flutterPage': (pageName, params, _) {
'flutterMain': (pageName, params, _) {
print("flutterPage params:$params");
return FlutterRouteWidget();
return FlutterRouteWidget(params:params);
},
});
}
......
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
......@@ -13,8 +14,9 @@ class FirstRouteWidget extends StatelessWidget {
child: Text('Open second route'),
onPressed: () {
print("open second page!");
FlutterBoost.singleton.open("second").then((Map value) {
print("call me when page is finished. did recieve second route result $value");
FlutterBoost.singleton.open("sample://flutterPage2").then((Map value) {
print(
"call me when page is finished. did recieve second route result $value");
});
},
),
......@@ -57,7 +59,7 @@ class TabRouteWidget extends StatelessWidget {
body: Center(
child: RaisedButton(
onPressed: () {
FlutterBoost.singleton.open("second");
FlutterBoost.singleton.open("sample://flutterPage2");
},
child: Text('Open second route'),
),
......@@ -66,90 +68,170 @@ class TabRouteWidget extends StatelessWidget {
}
}
class FlutterRouteWidget extends StatelessWidget {
class FlutterRouteWidget extends StatefulWidget {
FlutterRouteWidget({this.params,this.message});
final Map params;
final String message;
FlutterRouteWidget({this.message});
@override
_FlutterRouteWidgetState createState() => _FlutterRouteWidgetState();
}
class _FlutterRouteWidgetState extends State<FlutterRouteWidget> {
final TextEditingController _usernameController = TextEditingController();
@override
Widget build(BuildContext context) {
final String message=widget.message;
return Scaffold(
appBar: AppBar(
title: Text('flutter_boost_example'),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
margin: const EdgeInsets.only(top: 80.0),
child: Text(
message ?? "This is a flutter activity",
style: TextStyle(fontSize: 28.0, color: Colors.blue),
),
alignment: AlignmentDirectional.center,
),
Expanded(child: Container()),
InkWell(
child: Container(
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
color: Colors.yellow,
child: Text(
'open native page',
style: TextStyle(fontSize: 22.0, color: Colors.black),
)),
body: SingleChildScrollView(
child:Container(
margin: const EdgeInsets.all(24.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
margin: const EdgeInsets.only(top: 10.0,bottom: 20.0),
child: Text(
message ?? "This is a flutter activity \n params:${widget.params}",
style: TextStyle(fontSize: 28.0, color: Colors.blue),
),
alignment: AlignmentDirectional.center,
),
// Expanded(child: Container()),
const CupertinoTextField(
prefix: Icon(
CupertinoIcons.person_solid,
color: CupertinoColors.lightBackgroundGray,
size: 28.0,
),
padding: EdgeInsets.symmetric(horizontal: 6.0, vertical: 12.0),
clearButtonMode: OverlayVisibilityMode.editing,
textCapitalization: TextCapitalization.words,
autocorrect: false,
decoration: BoxDecoration(
border: Border(bottom: BorderSide(width: 0.0, color: CupertinoColors.inactiveGray)),
),
placeholder: 'Name',
),
InkWell(
child: Container(
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
color: Colors.yellow,
child: Text(
'open native page',
style: TextStyle(fontSize: 22.0, color: Colors.black),
)),
///后面的参数会在native的IPlatform.startActivity方法回调中拼接到url的query部分。
///例如:sample://nativePage?aaa=bbb
onTap: () =>
FlutterBoost.singleton.open("sample://nativePage", urlParams: {
"query": {"aaa": "bbb"}
}),
),
InkWell(
child: Container(
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
color: Colors.yellow,
child: Text(
'open flutter page',
style: TextStyle(fontSize: 22.0, color: Colors.black),
)),
///后面的参数会在native的IPlatform.startActivity方法回调中拼接到url的query部分。
///例如:sample://nativePage?aaa=bbb
onTap: () => FlutterBoost.singleton
.open("sample://nativePage", urlParams: {
"query": {"aaa": "bbb"}
}),
),
InkWell(
child: Container(
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
color: Colors.yellow,
child: Text(
'open flutterPage1',
style: TextStyle(fontSize: 22.0, color: Colors.black),
)),
///后面的参数会在native的IPlatform.startActivity方法回调中拼接到url的query部分。
///例如:sample://nativePage?aaa=bbb
onTap: () =>
FlutterBoost.singleton.open("sample://flutterPage", urlParams: {
"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(
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
color: Colors.yellow,
child: Text(
'open flutter fragment page',
style: TextStyle(fontSize: 22.0, color: Colors.black),
)),
onTap: () =>
FlutterBoost.singleton.open("sample://flutterFragmentPage"),
),
],
///后面的参数会在native的IPlatform.startActivity方法回调中拼接到url的query部分。
///例如:sample://nativePage?aaa=bbb
onTap: () => FlutterBoost.singleton
.open("sample://flutterPage1", urlParams: {
"query": {"aaa": "bbb"}
}),
),
InkWell(
child: Container(
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
color: Colors.yellow,
child: Text(
'open flutterPage2',
style: TextStyle(fontSize: 22.0, color: Colors.black),
)),
///后面的参数会在native的IPlatform.startActivity方法回调中拼接到url的query部分。
///例如:sample://nativePage?aaa=bbb
onTap: () => FlutterBoost.singleton
.open("sample://flutterPage2", urlParams: {
"query": {"aaa": "bbb"}
}),
),
InkWell(
child: Container(
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
color: Colors.yellow,
child: Text(
'open flutterPage3',
style: TextStyle(fontSize: 22.0, color: Colors.black),
)),
///后面的参数会在native的IPlatform.startActivity方法回调中拼接到url的query部分。
///例如:sample://nativePage?aaa=bbb
onTap: () => FlutterBoost.singleton
.open("sample://flutterPage3", urlParams: {
"query": {"aaa": "bbb"}
}),
),
InkWell(
child: Container(
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
color: Colors.yellow,
child: Text(
'open flutter page',
style: TextStyle(fontSize: 22.0, color: Colors.black),
)),
///后面的参数会在native的IPlatform.startActivity方法回调中拼接到url的query部分。
///例如:sample://nativePage?aaa=bbb
onTap: () => FlutterBoost.singleton
.open("sample://flutterPage", urlParams: {
"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(
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
color: Colors.yellow,
child: Text(
'open flutter fragment page',
style: TextStyle(fontSize: 22.0, color: Colors.black),
)),
onTap: () => FlutterBoost.singleton
.open("sample://flutterFragmentPage"),
),
],
),
),
),
);
}
......
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