mop-flutter-sdk/example/lib/main.dart

327 lines
11 KiB
Dart
Raw Permalink Normal View History

2021-10-10 23:50:23 +08:00
// ignore_for_file: prefer_const_constructors
2020-02-27 17:21:45 +08:00
import 'package:flutter/material.dart';
2023-04-22 10:49:55 +08:00
import 'package:flutter/services.dart';
2023-04-07 21:40:02 +08:00
import 'package:mop/api.dart';
2020-02-27 17:21:45 +08:00
import 'dart:async';
import 'package:mop/mop.dart';
2024-08-21 09:26:56 +08:00
import 'package:mop_example/test_page.dart';
2020-02-27 17:21:45 +08:00
void main() => runApp(MyApp());
2023-04-22 10:49:55 +08:00
const toAppMessageChannel = MethodChannel("com.message.flutter_to_app");
2020-02-27 17:21:45 +08:00
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
2024-08-21 09:26:56 +08:00
Future.delayed(const Duration(seconds: 3), () {
init();
});
2020-02-27 17:21:45 +08:00
}
// Platform messages are asynchronous, so we initialize in an async method.
2020-02-28 12:20:44 +08:00
Future<void> init() async {
2021-12-28 20:26:23 +08:00
2023-04-07 21:40:02 +08:00
//多服务器配置
2021-12-28 20:26:23 +08:00
FinStoreConfig storeConfigA = FinStoreConfig(
2023-04-07 21:40:02 +08:00
"22LyZEib0gLTQdU3MUauAfJ/xujwNfM6OvvEqQyH4igA",
"703b9026be3d6bc5",
2021-12-28 20:26:23 +08:00
"https://api.finclip.com",
2023-04-07 21:40:02 +08:00
cryptType: "SM",
2021-12-28 20:26:23 +08:00
);
FinStoreConfig storeConfigB = FinStoreConfig(
2023-04-07 21:40:02 +08:00
"22LyZEib0gLTQdU3MUauAfJ/xujwNfM6OvvEqQyH4igA",
"703b9026be3d6bc5",
"https://finchat-mop-b.finogeeks.club"
2021-12-28 20:26:23 +08:00
);
List<FinStoreConfig> storeConfigs = [storeConfigA];
2023-04-07 21:40:02 +08:00
Config config = Config(storeConfigs);
config.language = LanguageType.English;
2023-04-10 11:23:11 +08:00
config.baseLoadingViewClass = "LoadingView";
2024-08-21 09:26:56 +08:00
config.userId = "18607180143";
config.channel = "finclip";
config.phone = "1234567890";
2023-06-11 13:59:21 +08:00
config.appletDebugMode = BOOLState.BOOLStateTrue;
2023-04-07 21:40:02 +08:00
UIConfig uiconfig = UIConfig();
2021-12-28 20:26:23 +08:00
uiconfig.isAlwaysShowBackInDefaultNavigationBar = false;
uiconfig.isClearNavigationBarNavButtonBackground = false;
uiconfig.isHideFeedbackAndComplaints = true;
uiconfig.isHideBackHome = true;
uiconfig.isHideForwardMenu = true;
2021-12-28 20:26:23 +08:00
CapsuleConfig capsuleConfig = CapsuleConfig();
2023-04-07 21:40:02 +08:00
// capsuleConfig.capsuleBgLightColor = 0x33ff00ee;
capsuleConfig.capsuleCornerRadius = 16;
// capsuleConfig.capsuleRightMargin = 25;
2021-12-28 20:26:23 +08:00
uiconfig.capsuleConfig = capsuleConfig;
uiconfig.appletText = "applet";
2023-04-10 12:20:26 +08:00
uiconfig.loadingLayoutCls = "com.finogeeks.mop_example.CustomLoadingPage";
2023-06-14 19:06:10 +08:00
uiconfig.autoAdaptDarkMode = true;
2021-12-28 20:26:23 +08:00
2023-04-07 21:40:02 +08:00
// if (Platform.isIOS) {
// final res = await Mop.instance.initialize(
// '22LyZEib0gLTQdU3MUauATBwgfnTCJjdr7FCnywmAEM=', 'bdfd76cae24d4313',
// apiServer: 'https://api.finclip.com',
// apiPrefix: '/api/v1/mop',
// uiConfig: uiconfig,
// finStoreConfigs: storeConfigs);
// print(res);
// } else if (Platform.isAndroid) {
// final res = await Mop.instance.initialize(
// '22LyZEib0gLTQdU3MUauATBwgfnTCJjdr7FCnywmAEM=', 'bdfd76cae24d4313',
// apiServer: 'https://api.finclip.com', apiPrefix: '/api/v1/mop');
// print(res);
// }
final res = await Mop.instance.initSDK(config, uiConfig: uiconfig);
print(res);
Mop.instance.registerAppletHandler(MyAppletHandler());
2024-06-17 09:54:16 +08:00
Mop.instance.registerExtensionApi('getUserProfile', getUserProfile);
2023-07-05 17:30:57 +08:00
2023-12-06 13:57:15 +08:00
Mop.instance.registerExtensionApi('pushNativePage', pushNativePage);
2020-02-27 17:21:45 +08:00
if (!mounted) return;
}
2023-12-06 13:57:15 +08:00
Future<Map<String, dynamic>> pushNativePage(dynamic params) async {
print(params);
Map<String, dynamic> result = {
"userInfo":{
"nickName" : "haley",
"avatarUrl" : "https://www.finclip.com",
"gender" : 1,
"country" : "China",
"province" : "Guangdong",
"city" : "shenzhen",
}
};
return Future.value(result);
}
2023-07-05 17:30:57 +08:00
Future<Map<String, dynamic>> getUserProfile(dynamic params) async {
Map<String, dynamic> result = {
"userInfo":{
"nickName" : "haley",
"avatarUrl" : "https://www.finclip.com",
"gender" : 1,
"country" : "China",
"province" : "Guangdong",
"city" : "shenzhen",
}
};
return Future.value(result);
2023-07-05 17:30:57 +08:00
}
2021-12-28 20:26:23 +08:00
Widget _buildAppletItem(
String appletId, String itemName, VoidCallback tapAction) {
return GestureDetector(
onTap: tapAction,
child: Container(
padding: EdgeInsets.only(left: 7, right: 7),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(5)),
gradient: LinearGradient(
colors: const [Color(0xFF12767e), Color(0xFF0dabb8)],
stops: const [0.0, 1.0],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
child: Center(
child: Text(
itemName,
style: TextStyle(color: Colors.white),
),
),
),
);
}
2024-08-21 09:26:56 +08:00
Widget _buildAppletWidget(String appletId, String appletName, int index, Map<String, String>? startParams) {
2021-12-28 20:26:23 +08:00
return Container(
margin: EdgeInsets.only(left: 20, top: 30, right: 20),
child: Column(
children: [
Text(
appletName,
style: TextStyle(
fontSize: 18, fontWeight: FontWeight.w500, color: Colors.red),
),
SizedBox(
height: 10,
),
Container(
height: 100,
child: GridView.count(
2024-08-21 09:26:56 +08:00
crossAxisCount: 4,
2021-12-28 20:26:23 +08:00
childAspectRatio: 2,
2024-08-21 09:26:56 +08:00
crossAxisSpacing: 10,
2021-12-28 20:26:23 +08:00
// physics: NeverScrollableScrollPhysics(),
children: [
2024-08-21 09:26:56 +08:00
_buildAppletItem(appletId, "open", () {
2023-06-09 16:43:26 +08:00
TranstionStyle style = TranstionStyle.TranstionStyleUp;
2024-08-21 09:26:56 +08:00
FCReLaunchMode mode = FCReLaunchMode.PARAMS_EXIST;
if (index == 1) {
mode = FCReLaunchMode.ONLY_PARAMS_DIFF;
2023-06-09 16:43:26 +08:00
style = TranstionStyle.TranstionStylePush;
2024-08-21 09:26:56 +08:00
} else if (index == 2) {
mode = FCReLaunchMode.ALWAYS;
} else if (index == 3) {
mode = FCReLaunchMode.NEVER;
2023-06-09 16:43:26 +08:00
}
2024-08-21 09:26:56 +08:00
RemoteAppletRequest request = RemoteAppletRequest(
apiServer: 'https://api.finclip.com',
appletId: appletId,
transitionStyle: style,
reLaunchMode: mode,
startParams: startParams);
2023-06-09 16:43:26 +08:00
Mop.instance.startApplet(request);
2023-06-09 16:43:26 +08:00
// Mop.instance.qrcodeOpenApplet('https://api.finclip.com/api/v1/mop/runtime/applet/-f-MGYzN2Q1YTYzMmI2MWIyZg--');
2021-12-28 20:26:23 +08:00
}),
2024-08-21 09:26:56 +08:00
_buildAppletItem(appletId, "finish", () {
2021-12-28 20:26:23 +08:00
Mop.instance.finishRunningApplet(appletId, true);
}),
2024-08-21 09:26:56 +08:00
_buildAppletItem(appletId, "remove", () {
2021-12-28 20:26:23 +08:00
Mop.instance.removeUsedApplet(appletId);
}),
2024-08-21 09:26:56 +08:00
_buildAppletItem(appletId, "finishAll", () {
Mop.instance.clearApplets();
}),
2021-12-28 20:26:23 +08:00
],
),
)
],
),
);
}
2020-04-22 21:51:50 +08:00
// 5e637a18cbfae4000170fa7a
2020-02-27 17:21:45 +08:00
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
2020-02-28 12:20:44 +08:00
title: const Text('凡泰极客小程序 Flutter 插件'),
2024-08-21 09:26:56 +08:00
actions: <Widget>[
Builder(
builder: (context) {
return IconButton(
icon: Icon(Icons.more_horiz),
tooltip: 'More',
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => TestPage()),
);
},
);
},
),
],
2020-02-27 17:21:45 +08:00
),
2021-12-28 20:26:23 +08:00
body: Column(
children: <Widget>[
2024-08-21 09:26:56 +08:00
_buildAppletWidget("5facb3a52dcbff00017469bd", "画图小程序", 0, {'query':'ramdom='+context.hashCode.toString()}),
_buildAppletWidget("5f72e3559a6a7900019b5baa", "官方小程序", 1, {'query':'key=value'}),
_buildAppletWidget("5f17f457297b540001e06ebb", "api测试小程序", 2, null),
_buildAppletWidget("61386f6484dd160001d3e1ab", "测试小程序", 3, {'query':'ramdom='+context.hashCode.toString()}),
2023-06-16 18:17:23 +08:00
2021-12-28 20:26:23 +08:00
],
2020-02-28 12:20:44 +08:00
),
2020-02-27 17:21:45 +08:00
),
);
}
}
2023-04-07 21:40:02 +08:00
class MyAppletHandler extends AppletHandler {
@override
Future<void> appletDidOpen(String appId) async {
print("appletDidOpen appId:$appId");
}
@override
bool customCapsuleMoreButtonClick(String appId) {
2023-04-22 10:49:55 +08:00
print("customCapsuleMoreButtonClick---");
// toAppMessageChannel.invokeMethod("showCustomMoreView", {"appId": appId});
return false;
2023-04-07 21:40:02 +08:00
}
@override
void forwardApplet(Map<String, dynamic> appletInfo) {
print("forwardApplet ---");
}
@override
2023-12-06 13:57:15 +08:00
Future<List<CustomMenu>> getCustomMenus(String appId) {
2023-06-15 17:21:22 +08:00
CustomMenu menu1 = CustomMenu('WXTest', 'https://img1.baidu.com/it/u=2878938773,1765835171&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500', '百度图标', 'common');
2023-06-14 19:06:10 +08:00
menu1.darkImage = 'https://img95.699pic.com/xsj/14/46/mh.jpg%21/fw/700/watermark/url/L3hzai93YXRlcl9kZXRhaWwyLnBuZw/align/southeast';
CustomMenu menu2 = CustomMenu('CustomMenu2', 'minipro_list_collect', '工程图标', 'common');
menu2.darkImage = 'minipro_list_service';
List<CustomMenu> customMenus = [
2023-06-14 19:06:10 +08:00
menu1,
menu2,
2023-06-11 13:59:21 +08:00
CustomMenu('MyFriends', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSpvugSNLs9R7iopz_noeotAelvgzYj-74iCg&usqp=CAU', '谷歌图标', 'common'),
2023-06-09 16:43:26 +08:00
// CustomMenu('WXShareAPPFriends', 'https://img1.baidu.com/it/u=2878938773,1765835171&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500', '微信好朋友', 'common'),
// CustomMenu('WXShareAPPMoments', 'https://img2.baidu.com/it/u=3113705544,436318069&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500', '微信朋友圈', 'common'),
// CustomMenu('WXShareAPPFriends', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSpvugSNLs9R7iopz_noeotAelvgzYj-74iCg&usqp=CAU', '微信好朋友', 'common'),
// CustomMenu('WXShareAPPMoments', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR7cO4KB4e5-Ugdcq4pIyWunliH7LZRZzguKQ&usqp=CAU', '微信朋友圈', 'common'),
];
2023-12-06 13:57:15 +08:00
return Future.value(customMenus);
2023-04-07 21:40:02 +08:00
}
@override
Future<void> getMobileNumber(Function(dynamic params) callback) {
2023-04-07 21:40:02 +08:00
// TODO: implement getMobileNumber
Map<String, dynamic> result = {"phone": '18607180143',"other":'abc123'};
print('getMobileNumber:' + result.toString());
callback(result);
return Future.value(null);
2023-04-07 21:40:02 +08:00
}
@override
Future<Map<String, dynamic>> getUserInfo() {
// TODO: implement getUserInfo
Map<String, dynamic> result = {
"userInfo":{
"nickName" : "haley",
"avatarUrl" : "https://www.finclip.com",
"gender" : 1,
"country" : "China",
"province" : "Guangdong",
"city" : "shenzhen",
}
};
return Future.value(result);
2023-04-07 21:40:02 +08:00
}
2023-12-06 13:57:15 +08:00
// @override
// Future<Map<String, dynamic>> getUserInfo() {
// // TODO: implement getUserInfo
// throw UnimplementedError();
// }
2023-04-07 21:40:02 +08:00
@override
Future<void> onCustomMenuClick(String appId, String path, String menuId, String appInfo) {
// TODO: implement onCustomMenuClick
throw UnimplementedError();
}
}