tupgrade 1.1.0
tupgrade: ^1.1.0 copied to clipboard
App升级.
example/lib/main.dart
import 'dart:io';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:tupgrade/app_upgrade.dart';
import 'package:tupgrade/tupgrade.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: const HomePage(), //const TestWidgetsPage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String _platformVersion = 'Unknown';
@override
void initState() {
super.initState();
initPlatformState();
_checkAppUpgrade();
}
// 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 Tupgrade.platformVersion ?? '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;
});
}
_checkAppUpgrade() async {
AppUpgradeInfo appUpgradeInfo = await _checkAppInfo();
AppUpgrade.appUpgrade(
context,
appUpgradeInfo,
cancelText: '以后再说',
okText: '马上升级',
onCancel: () {
print('onCancel');
},
onOk: () {
print('onOk');
},
downloadProgress: (count, total) {
print('count:$count,total:$total');
},
downloadStatusChange: (DownloadStatus status, {dynamic error}) {
print('status:$status,error:$error');
},
);
}
Future<AppUpgradeInfo> _checkAppInfo() async {
//这里一般访问网络接口,将返回的数据解析成如下格式
return Future.delayed(Duration(seconds: 5), () {
return AppUpgradeInfo(
title: '新版本V1.1.1',
contents: [
'1、支持立体声蓝牙耳机,同时改善配对性能',
'2、提供屏幕虚拟键盘',
'3、更简洁更流畅,使用起来更快',
'4、修复一些软件在使用时自动退出bug',
'5、新增加了分类查看功能'
],
force: true,
appDownloadUrl: Platform.isIOS ? "1530900054" : 'http://jjg-1300239480.cos.ap-guangzhou.myqcloud.com/475975f0-6853-bc08-6a94-a40533586c57/SmartDetect-v1.3.9.apk',
version: 'v1.1.1',
appName: 'SmartDetect',
appDownloadName: 'SmartDetect_v1.1.1.apk',
);
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Running on: $_platformVersion\n'),
),
),
);
}
}