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