conch_api 0.0.5
conch_api: ^0.0.5 copied to clipboard
A api define for conch loader.
example/lib/main.dart
import 'dart:io';
import 'package:example/router.dart';
import 'package:flutter/material.dart';
import 'package:conch_api/conch_api.dart';
void main() async {
runApp(MaterialApp(home: MyApp()));
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static BuildContext? context;
@override
Widget build(BuildContext context) {
MyApp.context = context;
return MaterialApp(
title: "Conch示例",
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(
title: "Conch示例",
),
onGenerateRoute: ExampleRoute.onGenerateRoute,
);
}
}
/// 首页
class MyHomePage extends StatefulWidget {
MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String patchInfoDesc = "补丁信息:None";
String patchLoadDesc = "未触发";
@override
void initState() {
super.initState();
_initConchLoader();
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Container(
alignment: Alignment.center,
padding: EdgeInsets.all(16),
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: 200,
child: OutlinedButton(
onPressed: () {
Navigator.pushNamed(context, "image");
},
child: Text('图片'),
),
),
Container(
width: 200,
child: OutlinedButton(
onPressed: () {
Navigator.pushNamed(context, "list");
},
child: Text('列表'),
),
),
Container(
width: 200,
child: OutlinedButton(
onPressed: () {
Navigator.pushNamed(context, "anim");
},
child: Text('动画'),
),
),
Container(
width: 200,
child: OutlinedButton(
onPressed: () {
Navigator.pushNamed(context, "touch");
},
child: Text('拖拽'),
),
),
Container(
width: 200,
child: OutlinedButton(
onPressed: () {
Navigator.pushNamed(context, "loop");
},
child: Text('循环/通信'),
),
),
Container(
margin: EdgeInsets.only(left: 10, top: 100, right: 10),
child: Text(
"$patchInfoDesc",
textDirection: TextDirection.ltr,
),
),
Container(
margin: EdgeInsets.only(left: 10, top: 5, right: 10),
child: Text(
"加载状态:$patchLoadDesc",
textDirection: TextDirection.ltr,
),
),
Container(
width: 200,
margin: EdgeInsets.only(left: 10, top: 5, right: 10),
child: OutlinedButton(
onPressed: () {
ConchLoaderAPI.getPatchLoadResult(preload: true);
},
child: Text('加载补丁'),
),
),
],
),
),
);
}
/// 初始化ConchLoader
void _initConchLoader() {
// 自定义日志输出
ConchLoaderAPI.setCustomLogger((level, tag, message) {
print('[Custom ${level.toString().split('.').last[0]}] $tag: $message');
});
// 初始化参数
final appId = Platform.isAndroid ? "f17bd7de78" : "019c4398e4";
final appKey = Platform.isAndroid ? "e7993b46-6140-4c86-acd6-f90b502a0ca5" : "e57ae316-38ee-47fe-9d21-827115e39e76";
final params = ConchParams(
appId: appId,
appKey: appKey,
moduleName: "conch_module",
deviceId: "12345",
appVersion: "1.0.0",
remoteFirst: true,
debugEnable: true,
);
// 补丁加载结果回调
PatchLoadCallback patchLoadCallback = (patchLoadResult) => _updatePatchLoadResult(patchLoadResult);
ConchLoaderAPI.init(params, patchLoadCallback: patchLoadCallback);
}
/// 更新补丁加载结果
void _updatePatchLoadResult(PatchLoadResult? patchLoadResult) {
setState(() {
if (patchLoadResult == null) {
patchInfoDesc = "补丁信息:NULL";
patchLoadDesc = "NULL";
} else {
final moduleName = patchLoadResult.moduleName;
final md5 = patchLoadResult.md5.isEmpty ? '' : patchLoadResult.md5.substring(0, 8);
final version = patchLoadResult.version;
patchInfoDesc = "模块: $moduleName 版本: $version MD5: $md5";
patchLoadDesc = patchLoadResult.resultCode.name;
}
});
}
}