flutter_appstore_checker 0.1.3
flutter_appstore_checker: ^0.1.3 copied to clipboard
A powerful Flutter plugin for iOS App Store update checking. Features automatic bundle ID detection, custom theme support, and beautiful UI components for update dialogs and pages.
import 'package:flutter/material.dart';
import 'package:flutter_appstore_checker/flutter_appstore_checker.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'App Store 更新检测示例',
theme: ThemeData(primarySwatch: Colors.blue, useMaterial3: true),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
UpdateCheckResult? _lastResult;
bool _isChecking = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('App Store 更新检测'), centerTitle: true),
body: SingleChildScrollView(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'功能演示',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 12),
const Text(
'这个插件可以:\n'
'• 自动获取当前应用的包名\n'
'• 支持传入测试包名进行检测\n'
'• 检测 App Store 是否有新版本\n'
'• 显示应用详细信息和更新内容\n'
'• 支持弹窗和页面两种展示方式',
style: TextStyle(height: 1.5),
),
],
),
),
),
const SizedBox(height: 20),
ElevatedButton.icon(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const AppUpdatePage(),
),
);
},
icon: const Icon(Icons.open_in_new),
label: const Text('打开更新检测页面'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
),
),
const SizedBox(height: 12),
ElevatedButton.icon(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
const AppUpdatePage(testBundleId: 'com.tencent.xin'),
),
);
},
icon: const Icon(Icons.science),
label: const Text('使用测试包名检测'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
),
),
const SizedBox(height: 12),
OutlinedButton.icon(
onPressed: _isChecking ? null : _showUpdateDialog,
icon: _isChecking
? const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.notifications),
label: Text(_isChecking ? '检测中...' : '弹窗方式检测更新'),
style: OutlinedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
),
),
const SizedBox(height: 12),
OutlinedButton.icon(
onPressed: _isChecking ? null : _showTestDialog,
icon: _isChecking
? const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.bug_report),
label: Text(_isChecking ? '检测中...' : '测试包名弹窗检测'),
style: OutlinedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
),
),
const SizedBox(height: 12),
OutlinedButton.icon(
onPressed: _isChecking ? null : _showDialogWithoutReleaseNotes,
icon: _isChecking
? const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.visibility_off),
label: Text(_isChecking ? '检测中...' : '隐藏更新内容弹窗'),
style: OutlinedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
),
),
if (_lastResult != null) ...[
const SizedBox(height: 20),
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'最近检测结果',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Text('包名: ${_lastResult!.bundleId}'),
Text('当前版本: ${_lastResult!.currentVersion}'),
if (_lastResult!.success) ...[
Text('商店版本: ${_lastResult!.latestVersion}'),
Text('有新版本: ${_lastResult!.hasNewVersion ? "是" : "否"}'),
] else
Text('错误: ${_lastResult!.errorMessage}'),
],
),
),
),
],
],
),
),
);
}
Future<void> _showUpdateDialog() async {
setState(() => _isChecking = true);
try {
final hasUpdate = await AppUpdateDialog.checkAndShow(
context,
forceCheck: true,
);
final result = AppUpdateChecker.instance.cachedResult;
if (result != null) {
setState(() => _lastResult = result);
}
if (!hasUpdate && mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(const SnackBar(content: Text('当前已是最新版本或检测失败')));
}
} finally {
setState(() => _isChecking = false);
}
}
Future<void> _showTestDialog() async {
setState(() => _isChecking = true);
try {
final hasUpdate = await AppUpdateDialog.checkAndShow(
context,
bundleId: 'com.tencent.xin',
forceCheck: true,
);
final result = AppUpdateChecker.instance.cachedResult;
if (result != null) {
setState(() => _lastResult = result);
}
if (!hasUpdate && mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(const SnackBar(content: Text('测试包名检测完成')));
}
} finally {
setState(() => _isChecking = false);
}
}
Future<void> _showDialogWithoutReleaseNotes() async {
setState(() => _isChecking = true);
try {
final hasUpdate = await AppUpdateDialog.checkAndShow(
context,
bundleId: 'com.tencent.xin',
forceCheck: true,
showReleaseNotes: false, // 隐藏更新内容
);
final result = AppUpdateChecker.instance.cachedResult;
if (result != null) {
setState(() => _lastResult = result);
}
if (!hasUpdate && mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(const SnackBar(content: Text('隐藏更新内容的弹窗测试完成')));
}
} finally {
setState(() => _isChecking = false);
}
}
}