Browse Source

添加widget工厂

tags/0.0.1
Weller 4 years ago
parent
commit
03250d6409
18 changed files with 148 additions and 197 deletions
  1. +1
    -8
      android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNativable.java
  2. +35
    -1
      example/android/app/src/main/java/cn/zhios/zhiying_comm_example/MainActivity.java
  3. +18
    -7
      example/lib/main.dart
  4. +0
    -6
      ios/Classes/ZhiyingCommNative.h
  5. +2
    -7
      ios/Classes/ZhiyingCommNative.m
  6. +1
    -14
      ios/Classes/ZhiyingCommNativeProtocol.h
  7. +0
    -6
      ios/Classes/ZhiyingCommNativeResult.h
  8. +0
    -6
      ios/Classes/ZhiyingCommNativeResult.m
  9. +0
    -20
      lib/native/native_router.dart
  10. +7
    -4
      lib/pages/empty_page.dart
  11. +0
    -9
      lib/util/enum_util.dart
  12. +10
    -75
      lib/util/native_util.dart
  13. +0
    -2
      lib/util/navigator_observer.dart
  14. +17
    -16
      lib/util/net_util.dart
  15. +0
    -8
      lib/util/page_factory.dart
  16. +0
    -8
      lib/util/router_util.dart
  17. +56
    -0
      lib/util/widget_factory.dart
  18. +1
    -0
      lib/zhiying_comm.dart

+ 1
- 8
android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNativable.java View File

@@ -1,11 +1,4 @@
/*
* @Author: your name
* @Date: 2020-06-16 15:37:18
* @LastEditTime: 2020-06-19 17:13:52
* @LastEditors: your name
* @Description: In User Settings Edit
* @FilePath: /hairuyi_flutter_comm/android/src/main/kotlin/com/fnuoos/hairuyi_flutter_comm/HairuyiFlutterCommNativable.java
*/

package cn.zhios.zhiying_comm;

import java.util.Map;


+ 35
- 1
example/android/app/src/main/java/cn/zhios/zhiying_comm_example/MainActivity.java View File

@@ -1,13 +1,47 @@
package cn.zhios.zhiying_comm_example;

import cn.zhios.zhiying_comm.ZhiyingFlutterCommHandler;
import cn.zhios.zhiying_comm.ZhiyingFlutterCommNativable;
import cn.zhios.zhiying_comm.ZhiyingFlutterCommNative;
import cn.zhios.zhiying_comm.ZhiyingFlutterCommNativeResult;

import java.util.HashMap;
import java.util.Map;

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
public class MainActivity extends FlutterActivity implements ZhiyingFlutterCommNativable {
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
ZhiyingFlutterCommNative.getInstance().regist((ZhiyingFlutterCommNativable) this);
}

boolean localDebug = false;

@Override
public void openPage(Map params) {

}

@Override
public void openNativePage(Map params) {

}

@Override
public Map getSetting() {
Map map = new HashMap();
map.put("domain", "http://120.76.175.204:8989");
return map;
}

@Override
public void invokeMethod(String method, Map<String, Object> params, ZhiyingFlutterCommHandler handler) {
return ;
}

}

+ 18
- 7
example/lib/main.dart View File

@@ -21,13 +21,24 @@ class _MyAppState extends State<MyApp> {
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('智莺-基础库'),
),
body: Center(
child: Text('123'),
),
),
appBar: AppBar(
title: const Text('智莺-基础库'),
),
body: Wrap(
direction: Axis.horizontal,
spacing: 5,
children: <Widget>[
RaisedButton(
onPressed: () {
NetUtil.post('/siteapi/v1/ucenter/login/', params: {
'username': 'xiangguohui',
'password': 'fnuo123com'
});
},
child: Text('登录请求'),
)
],
)),
);
}
}

+ 0
- 6
ios/Classes/ZhiyingCommNative.h View File

@@ -1,9 +1,3 @@
//
// ZhiyingCommNative.h
// hairuyi_flutter_comm
//
// Created by Weller on 2020/5/16.
//

#import <Foundation/Foundation.h>
#import "ZhiyingCommNativeProtocol.h"


+ 2
- 7
ios/Classes/ZhiyingCommNative.m View File

@@ -1,9 +1,4 @@
//
// ZhiyingCommNative.m
// hairuyi_flutter_comm
//
// Created by Weller on 2020/5/16.
//

#import <Flutter/Flutter.h>
#import "ZhiyingCommNative.h"
#import "ZhiyingCommNativeResult.h"
@@ -15,7 +10,7 @@ static id<ZhiyingCommNativeProtocol> _delegate;

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
FlutterMethodChannel* channel = [FlutterMethodChannel
methodChannelWithName:@"hairuyi_flutter_comm://method"
methodChannelWithName:@"zhiying_comm://method"
binaryMessenger:[registrar messenger]];
ZhiyingCommNative* instance = [[ZhiyingCommNative alloc] init];
[registrar addMethodCallDelegate:instance channel:channel];


+ 1
- 14
ios/Classes/ZhiyingCommNativeProtocol.h View File

@@ -1,17 +1,4 @@
/*
* @Author: your name
* @Date: 2020-06-16 15:37:18
* @LastEditTime: 2020-06-20 11:28:22
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /hairuyi_flutter_comm/ios/Classes/HairuyiFlutterCommNativeProtocol.h
*/
//
// ZhiyingCommNativeProtocol.h
// hairuyi_flutter_comm
//
// Created by Weller on 2020/5/16.
//


#import "ZhiyingCommNativeResult.h"



+ 0
- 6
ios/Classes/ZhiyingCommNativeResult.h View File

@@ -1,9 +1,3 @@
//
// ZhiyingCommNativeResult.h
// hairuyi_flutter_comm
//
// Created by Weller on 2020/5/16.
//

#import <Foundation/Foundation.h>



+ 0
- 6
ios/Classes/ZhiyingCommNativeResult.m View File

@@ -1,9 +1,3 @@
//
// ZhiyingCommNativeResult.m
// hairuyi_flutter_comm
//
// Created by Weller on 2020/5/16.
//

#import "ZhiyingCommNativeResult.h"



+ 0
- 20
lib/native/native_router.dart View File

@@ -1,12 +1,3 @@
/*
* @Author: your name
* @Date: 2020-06-19 14:59:26
* @LastEditTime: 2020-06-24 16:37:35
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /hairuyi_flutter_comm/lib/native/native_router.dart
*/

// 重要!!!!!
// 重要!!!!!
// 重要!!!!!
@@ -20,15 +11,4 @@

enum NativeRouter {
openModifyPayPassword, // 打开修改支付密码
openVerifyByAlipay, // 打开支付宝实名认证
openSettingAlipay, // 打开支付宝绑定页面
openContacts, // 打开人脉
openLiveAnchor, //打开主播开播页面
openLiveWatch, //打开观看直播页面
openLiveRecord, // 打开会看(录播)
openWeChatShare, // 分享到微信
openSharePop, //打开分享弹窗
openSearchKeyword, // 关键词搜索
openWithdrawal, // 打开提现页面
showIntegralPop, // 展示获得积分弹窗
}

+ 7
- 4
lib/pages/empty_page.dart View File

@@ -1,10 +1,13 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class EmptyPage extends StatelessWidget {
@override
Widget build(BuildContext context) {

return Center(child: Text('Page has no regist'),);
return Scaffold(
body: Center(
child: Text('页面未注册'),
),
);
}
}
}

+ 0
- 9
lib/util/enum_util.dart View File

@@ -1,12 +1,3 @@
/*
* @Author: your name
* @Date: 2020-06-19 15:09:28
* @LastEditTime: 2020-06-19 15:09:38
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /hairuyi_flutter_comm/lib/utils/enum_util.dart
*/

///枚举类型转string
String enumToString(o) => o.toString().split('.').last;



+ 10
- 75
lib/util/native_util.dart View File

@@ -1,15 +1,7 @@
/*
* 与原生app进行交互
* @Author: fnuoos
* @Date: 2020-05-15 11:53:06
* @Last Modified by: mikey.zhaopeng
* @Last Modified time: 2020-05-15 11:53:58
*/

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:zhiying_comm/pages/empty_page.dart';

class NativeUtil {
// 工厂模式
@@ -20,7 +12,7 @@ class NativeUtil {
MethodChannel _methodChannel;
NativeUtil._internal() {
// 初始化
_methodChannel = MethodChannel('hairuyi_flutter_comm://method');
_methodChannel = MethodChannel('zhiying_comm://method');
}
static NativeUtil _getInstance() {
if (_instance == null) {
@@ -52,16 +44,14 @@ class NativeUtil {
}

// 公共跳转方法
static Future openPage(BuildContext context, Map params) {
// if (params.containsKey('is_to_flutter') &&
// params['is_to_flutter'].toString() == '1') {
// BasePageModel model = BasePageModel.fromJson(params);
// return Navigator.of(context).push(MaterialPageRoute(
// builder: (BuildContext context) =>
// PageFactory.create(model.SkipUIIdentifier, model)));
// }
return NativeUtil._invokeChannel('openPage',
params: new Map<String, dynamic>.from(params));
static Future openPage(BuildContext context, Map<String, dynamic> params) {
return Navigator.of(context)
.push(MaterialPageRoute(builder: (BuildContext context) {
if (!params.containsKey('SkipUIIdentifier')) {
return EmptyPage();
}
return PageFactory.create(params['SkipUIIdentifier'], params);
}));
}

//跳转原生普通页面,非公共跳转
@@ -78,65 +68,10 @@ class NativeUtil {
NativeUtil._invokeChannel('openGoodsDetail', params: params);
}

static Future<String> getToken() async {
Map<String, dynamic> data = await NativeUtil._invokeChannel('getToken');
if (data.containsKey('token')) {
return data['token'];
}
return '';
}

static Future<Map<String, dynamic>> getSetting() async {
return NativeUtil._invokeChannel('getSetting');
}

// 打开支付宝支付
static Future<bool> payByAlipay(String payCode) async {
Map params = {"code": payCode};
Map<String, dynamic> result = await NativeUtil._invokeChannel('openAliPay',
params: new Map<String, dynamic>.from(params));
if (result.containsKey('msg')) {
Fluttertoast.showToast(
msg: result['msg'],
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
);
}
if (result.containsKey('success') && result['success'] == '1') {
return true;
}
return false;
}

// 打开微信支付
static Future<bool> payByWechat(Map params) async {
Map<String, dynamic> result = await NativeUtil._invokeChannel(
'openWechatPay',
params: new Map<String, dynamic>.from(params));

if (result.containsKey('msg')) {
Fluttertoast.showToast(
msg: result['msg'],
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
);
}
if (result.containsKey('success') && result['success'] == '1') {
return true;
}
return false;
}

static Future<Map<String, dynamic>> onPagePop(bool isTop) async {
NativeUtil._invokeChannel('pageDidPop',
params: new Map<String, dynamic>.from({'isTop': isTop}));
}

static Future<Map<String, dynamic>> onPagePush(bool isTop) async {
NativeUtil._invokeChannel('pageDidPush',
params: new Map<String, dynamic>.from({'isTop': isTop}));
}

static Future<Map<String, dynamic>> invokeMethod(
String method, Map<String, dynamic> params) {
Map<String, dynamic> request = {"method": method, "params": params};


+ 0
- 2
lib/util/navigator_observer.dart View File

@@ -21,13 +21,11 @@ class FNNavigatorObserver extends NavigatorObserver {
void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
notifyHide(previousRoute);
notifyShow(route);
NativeUtil.onPagePush(previousRoute == null ? true : previousRoute.isFirst);
}

void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
notifyHide(route);
notifyShow(previousRoute);
NativeUtil.onPagePush(previousRoute == null ? true : previousRoute.isFirst);
}

void didRemove(Route<dynamic> route, Route<dynamic> previousRoute) {


+ 17
- 16
lib/util/net_util.dart View File

@@ -31,7 +31,6 @@ class NetUtil {
if (_dio == null) {
var setting = await NativeUtil.getSetting();
String domain = setting['domain']; //'http://www.hairuyi.com/';
String version = setting['version']; //'42';
_config(domain, apiVersion: _apiVersion);
}
return _dio;
@@ -45,9 +44,10 @@ class NetUtil {
baseUrl: baseUrl,
connectTimeout: 15000,
receiveTimeout: 15000,
contentType: Headers.formUrlEncodedContentType,
contentType: Headers.jsonContentType,
followRedirects: true));
_dio.interceptors.add(_NetInterceptors());
_dio.interceptors.add(LogInterceptor());

_apiVersion = apiVersion;
const bool inProduction = const bool.fromEnvironment("dart.vm.product");
@@ -93,7 +93,7 @@ class NetUtil {
int height = window.physicalSize.height.floor();
params["resolutionRatio"] = "$width*$height";

String token = await NativeUtil.getToken();
String token = '';
if (token != null &&
token != '' &&
(!params.containsKey('token') || params['token'] == '')) {
@@ -144,13 +144,13 @@ class NetUtil {
return "$timestamp";
}

@Deprecated('方法弃用,建议使用 NetUtil.requet')
static Future<dynamic> post(String path,
{Map<String, dynamic> params}) async {
if (params == null) {
params = {};
}
Map<String, dynamic> sign = await signParams(params);
// Map<String, dynamic> sign = await signParams(params);
Map<String, dynamic> sign = params;
Response response;
try {
Dio dio = await NetUtil.getInstance().dio;
@@ -163,6 +163,7 @@ class NetUtil {
}

if (response.statusCode != 200) {
// 请求错误
const bool inProduction = const bool.fromEnvironment("dart.vm.product");
if (!inProduction) {
Fluttertoast.showToast(msg: response.statusMessage);
@@ -171,18 +172,18 @@ class NetUtil {
return {};
}

var result = jsonDecode(response.data);
var result = response.data;

if (result['success'] == 0 || result['success'] == '0') {
print('error: ' + result['msg']);
Fluttertoast.showToast(
msg: result['msg'],
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
);
return {};
}
return result['data'];
// if (result['success'] == 0 || result['success'] == '0') {
// print('error: ' + result['msg']);
// Fluttertoast.showToast(
// msg: result['msg'],
// toastLength: Toast.LENGTH_SHORT,
// gravity: ToastGravity.BOTTOM,
// );
// return {};
// }
return result;
}

static Future<dynamic> post2(String path,


+ 0
- 8
lib/util/page_factory.dart View File

@@ -1,11 +1,3 @@
/*
* @Author: Weller
* @Date: 2020-05-16 17:26:40
* @LastEditTime: 2020-06-16 15:40:43
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /hairuyi_flutter_comm/lib/router/page_factory.dart
*/
import 'package:flutter/material.dart';

import 'package:zhiying_comm/zhiying_comm.dart';


+ 0
- 8
lib/util/router_util.dart View File

@@ -1,11 +1,3 @@
/*
* @Author: your name
* @Date: 2020-05-16 17:50:31
* @LastEditTime: 2020-06-17 18:27:00
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /hairuyi_flutter_comm/lib/router/router_util.dart
*/
import 'package:flutter/material.dart';
import 'package:zhiying_comm/zhiying_comm.dart';



+ 56
- 0
lib/util/widget_factory.dart View File

@@ -0,0 +1,56 @@
import 'package:flutter/material.dart';

import 'package:zhiying_comm/zhiying_comm.dart';

typedef Widget CreateWidget(Map<String, dynamic> model);

/* Widget生成工厂, 用于生成flutter端组件 */
class WidgetFactory {
static Map<String, CreateWidget> widgetCreater = Map();

/**
* @description: 注册组件
* @param {name} 组件名,唯一
* @return:
*/
static void regist(String name, CreateWidget creater) {
if (widgetCreater.containsKey(name)) {
return;
}

widgetCreater[name] = creater;
}

/**
* @description: 创建组件
* @param {String} 组件注册的名字
* @return:
*/
static Widget create(String name, Map<String, dynamic> model) {
if (widgetCreater.containsKey(name)) {
return widgetCreater[name](model);
}
const bool inProduction = const bool.fromEnvironment("dart.vm.product");
if (!inProduction) {
return Container();
} else {
return Container(
width: double.infinity,
height: 100,
color: Colors.redAccent,
child: Center(
child: Text('当前组件尚未注册'),
),
);
}
}

/**
* @description: 是否注册组件
* @param {bool}
* @return:
*/
static bool hasRegisted(String name) {
return widgetCreater.containsKey(name);
}
}

+ 1
- 0
lib/zhiying_comm.dart View File

@@ -9,6 +9,7 @@ export 'util/native_util.dart';
export 'util/navigator_observer.dart';
export 'util/net_util.dart';
export 'util/page_factory.dart';
export 'util/widget_factory.dart';
export 'util/router_util.dart';

export 'util/enum_util.dart';


Loading…
Cancel
Save