Commit 3b157295 authored by 艾娇平's avatar 艾娇平

与原生数据通讯

parent ef69db8d
import 'package:flutter/material.dart';
import 'package:hybrid_stack_manager/hybrid_stack_manager_plugin.dart';
import 'hybrid_stack/hybrid_stack_manager_plugin.dart';
import 'opinion.dart';
......
import 'dart:async';
import 'package:flutter/services.dart';
import '../services/ALNetClient.dart';
import 'dart:convert';
typedef Future<dynamic> MethodHandler(MethodCall call);
class HybridStackManagerPlugin {
static HybridStackManagerPlugin hybridStackManagerPlugin =
new HybridStackManagerPlugin._internal();
MethodChannel _channel;
MethodHandler _handler;
void setMethodCallHandler(MethodHandler hdler) {
_handler = hdler;
_channel.setMethodCallHandler(_handler);
}
HybridStackManagerPlugin._internal() {
_channel = new MethodChannel('hybrid_stack_manager');
}
openUrlFromNative({String url, Map query, Map params, bool animated}) {
_channel.invokeMethod("openUrlFromNative", {
"url": url ?? "",
"query": (query ?? {}),
"params": (params ?? {}),
"animated": animated ?? true
});
}
popCurPage({bool animated = true}) {
_channel.invokeMethod("popCurPage", animated);
}
updateCurFlutterRoute(String curRouteName) {
_channel.invokeMethod("updateCurFlutterRoute", curRouteName ?? "");
}
Future<Map> getMainEntryParams() async {
dynamic info = await _channel.invokeMethod('getMainEntryParams');
return new Future.sync(() => info as Map);
}
// method 0: GET 1:POST
Future<Map> callNativeNetWorking({String url, HttpMethod method,Map parameters}) async {
dynamic info = _channel.invokeMethod('openUrlFromNative', {
"url": url ?? "",
"method": 1 ?? 0,
"parameters": (parameters ?? {}),
});
print('callNativeNetWorking-----${info.toString()}');
return new Future.sync(() => jsonDecode(info.toString()) as Map);
}
}
export 'hybrid_stack_manager.dart';
export 'router_option.dart';
export 'router.dart';
//import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'router_option.dart';
import 'hybrid_stack_manager.dart';
import 'utils.dart';
//import 'dart:io';
//import 'package:image/image.dart' as image;
//import 'dart:math';
typedef Widget FlutterWidgetHandler({RouterOption routeOption, Key key});
class XMaterialPageRoute<T> extends MaterialPageRoute<T> {
final WidgetBuilder builder;
final bool animated;
final GlobalKey boundaryKey;
Duration get transitionDuration {
if (animated == true) return const Duration(milliseconds: 300);
return const Duration(milliseconds: 0);
}
XMaterialPageRoute({
this.builder,
this.animated,
this.boundaryKey,
RouteSettings settings: const RouteSettings(),
}) : super(builder: builder, settings: settings);
}
class Router extends Object {
static final Router singleton = new Router._internal();
List<XMaterialPageRoute> flutterRootPageNameLst = new List();
String currentPageUrl = null;
FlutterWidgetHandler routerWidgetHandler;
GlobalKey globalKeyForRouter;
static Router sharedInstance() {
return singleton;
}
Router._internal(){
setupMethodChannel();
}
void setupMethodChannel(){
HybridStackManagerPlugin.hybridStackManagerPlugin
.setMethodCallHandler((MethodCall methodCall)async{
String method = methodCall.method;
if (method == "openURLFromFlutter") {
Map args = methodCall.arguments;
if (args != null) {
bool animated = (args["animated"] == 1);
Router.sharedInstance().pushPageWithOptionsFromFlutter(
routeOption: new RouterOption(
url: args["url"],
query: args["query"],
params: args["params"]),
animated: animated ?? false);
}
} else if (method == "popToRoot") {
Router.sharedInstance().popToRoot();
} else if (method == "popToRouteNamed") {
Router.sharedInstance().popToRouteNamed(methodCall.arguments);
} else if (method == "popRouteNamed") {
Router.sharedInstance().popRouteNamed(methodCall.arguments);
}
});
}
popToRoot() {
NavigatorState navState = Navigator.of(globalKeyForRouter.currentContext);
List<Route<dynamic>> navHistory = navState.history;
int histLen = navHistory.length;
for (int i = histLen - 1; i >= 1; i--) {
Route route = navHistory.elementAt(i);
navState.removeRoute(route);
}
}
popToRouteNamed(String routeName) {
NavigatorState navState = Navigator.of(globalKeyForRouter.currentContext);
List<Route<dynamic>> navHistory = navState.history;
int histLen = navHistory.length;
for (int i = histLen - 1; i >= 1; i--) {
Route route = navHistory.elementAt(i);
if (!(route is XMaterialPageRoute) ||
((route as XMaterialPageRoute).settings.name != routeName)) {
navState.removeRoute(route);
}
if ((route is XMaterialPageRoute) &&
((route as XMaterialPageRoute).settings.name == routeName)) break;
}
}
popRouteNamed(String routeName) {
NavigatorState navState = Navigator.of(globalKeyForRouter.currentContext);
List<Route<dynamic>> navHistory = navState.history;
int histLen = navHistory.length;
for (int i = histLen - 1; i >= 1; i--) {
Route route = navHistory.elementAt(i);
if ((route is XMaterialPageRoute) &&
((route as XMaterialPageRoute).settings.name == routeName)) {
navState.removeRoute(route);
break;
}
}
}
pushPageWithOptionsFromFlutter({RouterOption routeOption, bool animated}) {
Widget page =
Router.sharedInstance().pageFromOption(routeOption: routeOption);
if (page != null) {
GlobalKey boundaryKey = new GlobalKey();
XMaterialPageRoute pageRoute = new XMaterialPageRoute(
settings: new RouteSettings(name: routeOption.userInfo),
animated: animated,
boundaryKey: boundaryKey,
builder: (BuildContext context) {
return new RepaintBoundary(key:boundaryKey,child: page);
});
Navigator.of(globalKeyForRouter.currentContext).push(pageRoute);
HybridStackManagerPlugin.hybridStackManagerPlugin
.updateCurFlutterRoute(routeOption.userInfo);
} else {
HybridStackManagerPlugin.hybridStackManagerPlugin.openUrlFromNative(
url: routeOption.url,
query: routeOption.query,
params: routeOption.params);
}
NavigatorState navState = Navigator.of(globalKeyForRouter.currentContext);
List<Route<dynamic>> navHistory = navState.history;
}
pushPageWithOptionsFromNative({RouterOption routeOption, bool animated}) {
HybridStackManagerPlugin.hybridStackManagerPlugin.openUrlFromNative(
url: routeOption.url,
query: routeOption.query,
params: routeOption.params,
animated: animated);
}
pageFromOption({RouterOption routeOption, Key key}) {
try {
currentPageUrl = routeOption.url + "?" + converUrl(routeOption.query);
} catch (e) {}
routeOption.userInfo = Utils.generateUniquePageName(routeOption.url);
if (routerWidgetHandler != null)
return routerWidgetHandler(routeOption: routeOption, key: key);
}
static String converUrl(Map query) {
String tmpUrl = "";
if (query != null) {
bool skipfirst = true;
query.forEach((key, value) {
if (skipfirst) {
skipfirst = false;
} else {
tmpUrl = tmpUrl + "&";
}
tmpUrl = tmpUrl + (key + "=" + value.toString());
});
}
return Uri.encodeFull(tmpUrl);
}
}
class RouterOption {
String url;
Map query;
Map params;
String userInfo;
RouterOption({this.url, this.query, this.params});
}
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as path;
import 'package:flutter/rendering.dart';
//import 'package:image/image.dart';
class Utils extends Object {
static int baseId = 100;
static String pageNameSeperatorToken = "_";
static int generatePrimaryPageId() {
return baseId++;
}
static Map parseUniquePageName(String pageName) {
List components = pageName.split(pageNameSeperatorToken);
if (components.length != 2) return null;
return {"name": components[0], "id": components[1]};
}
static String generateUniquePageName(String pageName) {
return (pageName ?? "") +
pageNameSeperatorToken +
generatePrimaryPageId().toString();
}
//
// static Future<Image> getImage(RenderRepaintBoundary object) async {
// ui.Image capture = await object.toImage(pixelRatio: 2.0);
// ByteData data = await capture.toByteData();
// return new Image.fromBytes(capture.width, capture.height, data.buffer.asUint32List());
// }
//
// static Future<File> writeFile(Image outputImage,String fileName) async {
// final Directory directory = await getTemporaryDirectory();
// final File outputFile = new File(path.join(directory.path, fileName));
// List<int> output = encodePng(outputImage);
// return outputFile.writeAsBytes(output);
// }
}
import 'package:flutter/material.dart';
import 'package:hybrid_stack_manager/router.dart';
import 'package:hybrid_stack_manager/hybrid_stack_manager.dart';
import 'package:hybrid_stack_manager/router_option.dart';
import 'hybrid_stack/hybrid_stack_manager_plugin.dart';
import 'app_config.dart';
import 'my_app.dart';
......@@ -13,10 +11,10 @@ import 'my_app.dart';
void main() async {
AppConfig.sharedInstance();
HybridStackManagerPlugin plugin =
HybridStackManagerPlugin.hybridStackManagerPlugin;
Map args = await plugin.getMainEntryParams();
// HybridStackManagerPlugin plugin =
// HybridStackManagerPlugin.hybridStackManagerPlugin;
runApp(new MyApp());
Map args = await HybridStackManagerPlugin.hybridStackManagerPlugin.getMainEntryParams();
if (args != null && args["url"] != null) {
RouterOption routeOption = new RouterOption(
url: args["url"], query: args["query"], params: args["params"]);
......
......@@ -9,26 +9,23 @@
import 'package:flutter/material.dart';
import 'macros/ALColors.dart';
import 'macros/ALDevice.dart';
import 'macros/ALCommon.dart';
import 'package:hybrid_stack_manager/hybrid_stack_manager_plugin.dart';
import 'services/ALNetClient.dart';
import 'hybrid_stack/hybrid_stack_manager_plugin.dart';
class Opinion extends StatelessWidget {
RouterOption routeOption;
Opinion(RouterOption option, {Key key}) : super(key: key) {
routeOption = option;
}
Widget build(BuildContext context) {
// Map m = Utils.parseUniquePageName(routeOption.userInfo);
return MaterialApp(
debugShowCheckedModeBanner: false,
title: '意见与建议',
color: ALColors.Color323232,
theme: ThemeData(
splashColor: Colors.white10
,//水波纹的颜色
splashColor: Colors.white10, //水波纹的颜色
),
home: OpinionPage(title: '意见和建议'),
);
......@@ -60,7 +57,13 @@ class _OpinionPageState extends State<OpinionPage> {
onTap: () {
HybridStackManagerPlugin.hybridStackManagerPlugin.popCurPage();
}),
title: new Text("意见与建议",style: TextStyle(color: ALColors.Color323232,fontSize: 16,fontWeight: FontWeight.bold),),
title: new Text(
"意见与建议",
style: TextStyle(
color: ALColors.Color323232,
fontSize: 16,
fontWeight: FontWeight.bold),
),
),
body: Container(
height: ALDevice.height,
......@@ -96,7 +99,7 @@ class _OpinionPageState extends State<OpinionPage> {
),
Container(
height: 40,
margin: EdgeInsets.only(left: 30, right: 30,top: 30),
margin: EdgeInsets.only(left: 30, right: 30, top: 30),
padding: EdgeInsets.only(bottom: 10),
decoration: BoxDecoration(
border: Border(
......@@ -144,12 +147,21 @@ class _OpinionPageState extends State<OpinionPage> {
}
void confirmClick() {
// 提交信息 && 回到上一级页面
// MethodHandler hdler = MethodHandler(MethodCall('hj',{'d':'d'}));
// HybridStackManagerPlugin.hybridStackManagerPlugin.setMethodCallHandler(hdler);
HybridStackManagerPlugin.hybridStackManagerPlugin.popCurPage();
// HybridStackManagerPlugin.hybridStackManagerPlugin.openUrlFromNative(url: '');
confimSuggestInfo();
print('confirmClick');
}
void confimSuggestInfo() {
Map<String, String> parameters = {'phone': 'dsdsdsd','content':'我是一个建议Flutter提交的','user_id':'123456'};
HybridStackManagerPlugin.hybridStackManagerPlugin.callNativeNetWorking(
url: ALServers.API_ACCOUNT_SUGGESTION,
method: HttpMethod.HTTPMethodPOTS,
parameters: parameters).then((val){
print('confimSuggestInfo---${val}');
});
}
......@@ -14,6 +14,7 @@ import 'dart:io';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:device_info/device_info.dart';
export 'ALServers.dart';
class ALResponse {
const ALResponse(
......
......@@ -8,6 +8,11 @@
import '../macros/ALDevice.dart';
enum HttpMethod {
HTTPMethodGET, // 0
HTTPMethodPOTS // 1
}
// API
class ALServers {
ALServers._();
......@@ -22,7 +27,7 @@ class ALServers {
static const String API_ACCOUNT_REGISTER = '/api/account/register';// 用户注册
static const String API_ACCOUNT_GETVFC = '/api/account/get_vfc';// 获取验证码
static const String API_ACCOUNT_THIRDPART = '/api/account/thirdparty_login'; // 三方账户登录
static const String API_ACCOUNT_SUGGESTION = "/api/v1/suggestion"; // 意见与建议
// HOST
}
......
# Generated by pub
# See https://www.dartlang.org/tools/pub/glossary#lockfile
packages:
archive:
dependency: transitive
description:
name: archive
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.8"
args:
dependency: transitive
description:
name: args
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.5.1"
async:
dependency: transitive
description:
......@@ -43,13 +29,6 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.14.11"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.1"
cookie_jar:
dependency: transitive
description:
......@@ -57,13 +36,6 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.0"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.6"
cupertino_icons:
dependency: "direct main"
description:
......@@ -95,20 +67,6 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
hybrid_stack_manager:
dependency: "direct main"
description:
name: hybrid_stack_manager
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.1.1"
image:
dependency: transitive
description:
name: image
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.7"
json_annotation:
dependency: "direct main"
description:
......@@ -138,19 +96,12 @@ packages:
source: hosted
version: "1.6.2"
path_provider:
dependency: transitive
dependency: "direct main"
description:
name: path_provider
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.4.1"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.1"
version: "0.5.0+1"
quiver:
dependency: transitive
description:
......@@ -226,13 +177,6 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.8"
xml:
dependency: transitive
description:
name: xml
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.3.1"
sdks:
dart: ">=2.0.0 <3.0.0"
flutter: ">=0.3.1 <2.0.0"
flutter: ">=0.1.4 <2.0.0"
......@@ -23,13 +23,13 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
hybrid_stack_manager: 0.1.1
json_annotation: ^2.0.0
# 网络
dio: ^2.0.0
# 数据存储
shared_preferences: ^0.4.2
device_info: ^0.4.0
path_provider: ^0.5.0
dev_dependencies:
......
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