155 lines
4.8 KiB
Dart
155 lines
4.8 KiB
Dart
import 'dart:async';
|
||
|
||
import 'package:app_upgrade_plugin/app_upgrade_plugin.dart';
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter/services.dart';
|
||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||
|
||
void main() {
|
||
// 确保Flutter绑定已初始化
|
||
WidgetsFlutterBinding.ensureInitialized();
|
||
|
||
// 插件现在会自动配置证书验证:
|
||
// - Debug模式:自动绕过证书验证
|
||
// - Release模式:严格证书验证
|
||
|
||
// 如需强制绕过证书(不推荐),可以使用:
|
||
// AppUpgradePlugin.ignoreCertificate = true;
|
||
|
||
// 或手动配置HTTP设置:
|
||
// AppUpgradePlugin().configureHttp(HttpConfig.unsafe);
|
||
|
||
// 延迟插件配置到Flutter完全初始化后
|
||
|
||
runApp(const MyApp());
|
||
}
|
||
|
||
class MyApp extends StatelessWidget {
|
||
const MyApp({super.key});
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return MaterialApp(
|
||
// 这里是国际化支持,确保添加flutter_localizations依赖
|
||
supportedLocales: const [Locale('zh', 'CN')],
|
||
localizationsDelegates: const [
|
||
GlobalMaterialLocalizations.delegate,
|
||
GlobalWidgetsLocalizations.delegate,
|
||
GlobalCupertinoLocalizations.delegate,
|
||
],
|
||
home: const HomePage(),
|
||
);
|
||
}
|
||
}
|
||
|
||
class HomePage extends StatefulWidget {
|
||
const HomePage({super.key});
|
||
|
||
@override
|
||
State<HomePage> createState() => _HomePageState();
|
||
}
|
||
|
||
class _HomePageState extends State<HomePage> {
|
||
String _platformVersion = 'Unknown';
|
||
final _appUpgradePlugin = AppUpgradePlugin();
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
initPlatformState();
|
||
// Use addPostFrameCallback to ensure the context is valid and mounted
|
||
// after the first frame has been rendered.
|
||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||
if (mounted) {
|
||
_testNetworkFunctionality();
|
||
}
|
||
});
|
||
}
|
||
|
||
Future<void> _testNetworkFunctionality() async {
|
||
await AppUpgradeSimple.instance.checkUpdate(
|
||
context: context,
|
||
future: () async {
|
||
// 模拟获取服务器版本信息
|
||
// 实际使用时,您应该调用您的API,并返回 AppUpgradeVersion 对象
|
||
// final response = await myApi.checkVersion();
|
||
// return AppUpgradeVersion(...);
|
||
|
||
// 这里为了演示,我们手动构造一个版本信息
|
||
return AppUpgradeVersion(
|
||
versionName: '1.0.1',
|
||
versionBuildNumber: 11,
|
||
isForce: true,
|
||
updateContent: '修复了一些Bug\n优化了用户体验',
|
||
downloadUrl: 'https://example.com/app.apk',
|
||
supportedMethods: [AppUpgradeMethod.browser, AppUpgradeMethod.inApp, AppUpgradeMethod.market],
|
||
);
|
||
},
|
||
showNoUpdateToast: true,
|
||
autoDownload: false,
|
||
autoInstall: true,
|
||
);
|
||
debugPrint('=== 网络功能测试完成 ===');
|
||
}
|
||
|
||
// Platform messages are asynchronous, so we initialize in an async method.
|
||
Future<void> initPlatformState() async {
|
||
String platformVersion;
|
||
// Platform messages may fail, so we use a try/catch PlatformException.
|
||
// We also handle the message potentially returning null.
|
||
try {
|
||
platformVersion = await _appUpgradePlugin.getPlatformVersion() ?? 'Unknown platform version';
|
||
} on PlatformException {
|
||
platformVersion = 'Failed to get platform version.';
|
||
}
|
||
|
||
// If the widget was removed from the tree while the asynchronous platform
|
||
// message was in flight, we want to discard the reply rather than calling
|
||
// setState to update our non-existent appearance.
|
||
if (!mounted) return;
|
||
|
||
setState(() {
|
||
_platformVersion = platformVersion;
|
||
});
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return Scaffold(
|
||
appBar: AppBar(
|
||
title: const Text('App Upgrade Plugin 示例'),
|
||
),
|
||
body: Padding(
|
||
padding: const EdgeInsets.all(16.0),
|
||
child: Column(
|
||
mainAxisAlignment: MainAxisAlignment.center,
|
||
children: [
|
||
Text('Running on: $_platformVersion\n'),
|
||
const SizedBox(height: 16),
|
||
ElevatedButton(
|
||
onPressed: () {
|
||
AppUpgradeSimple.instance.checkUpdate(
|
||
context: context,
|
||
future: () async {
|
||
// 模拟获取服务器版本信息
|
||
return AppUpgradeVersion(
|
||
versionName: '1.0.1',
|
||
versionBuildNumber: 11,
|
||
updateContent: '这是一个新版本',
|
||
downloadUrl: 'https://example.com/app.apk',
|
||
);
|
||
},
|
||
showNoUpdateToast: true,
|
||
autoDownload: false,
|
||
autoInstall: false,
|
||
);
|
||
},
|
||
child: const Text('检查更新'),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|