import 'dart:async'; import 'package:app_upgrade_plugin/app_upgrade_plugin.dart'; import 'package:app_upgrade_plugin/app_upgrade_plugin_enhanced.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完全初始化后 Future.delayed(Duration.zero, () { AppUpgradePluginEnhanced.instance.configure( debugMode: true, wifiOnly: false, autoCheck: true, ); }); 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 createState() => _HomePageState(); } class _HomePageState extends State { 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 _testNetworkFunctionality() async { await AppUpgradeSimple.instance.checkUpdate( context: context, url: 'https://dpc-teacher-api.23544.com/api/infra/AppVersion/Get', params: { 'appName': 'making_school_asignment_app', 'ftuType': 1, }, showNoUpdateToast: true, autoDownload: false, autoInstall: true, ); debugPrint('=== 网络功能测试完成 ==='); } // Platform messages are asynchronous, so we initialize in an async method. Future 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, url: 'https://dpc-teacher-api.23544.com/api/infra/AppVersion/Get', params: { 'appName': 'making_school_asignment_app', 'ftuType': 1, }, showNoUpdateToast: true, autoDownload: false, autoInstall: false, ); }, child: const Text('检查更新'), ), ], ), ), ); } }