flutter_app_auto_update_checker 1.0.0
flutter_app_auto_update_checker: ^1.0.0 copied to clipboard
A lightweight, cross-platform Flutter package that automatically checks for app updates from Google Play Store and Apple App Store, compares versions, and shows customizable update dialogs.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_app_auto_update_checker/flutter_app_auto_update_checker.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter App Auto Update Checker',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
AppUpdateInfo? _updateInfo;
bool _isChecking = false;
String? _errorMessage;
@override
void initState() {
super.initState();
// Check for updates when app starts
_checkForUpdate();
}
Future<void> _checkForUpdate() async {
setState(() {
_isChecking = true;
_errorMessage = null;
});
try {
final updateInfo = await UpdateCheckerService.checkForUpdate();
setState(() {
_updateInfo = updateInfo;
_isChecking = false;
});
// Show update dialog if update is available
if (updateInfo.hasUpdate && mounted) {
_showUpdateDialog();
}
} catch (e) {
setState(() {
_errorMessage = e.toString();
_isChecking = false;
});
}
}
void _showUpdateDialog() {
if (_updateInfo == null) return;
UpdateDialog.show(
context,
updateInfo: _updateInfo!,
title: 'Update Available! 🎉',
message: 'A new version of the app is available. Update now to get the latest features and improvements.',
updateButtonText: 'Update Now',
laterButtonText: 'Maybe Later',
isCritical: false,
);
}
void _showForceUpdateDialog() {
if (_updateInfo == null) return;
UpdateDialog.show(
context,
updateInfo: _updateInfo!,
title: 'Update Required! ⚠️',
message: 'This update includes important security fixes and bug improvements. Please update to continue using the app.',
updateButtonText: 'Update Now',
laterButtonText: 'Later',
showLaterButton: false,
isCritical: true,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Auto Update Checker'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Icon
Icon(
_updateInfo?.hasUpdate == true
? Icons.system_update
: Icons.check_circle_outline,
size: 80,
color: _updateInfo?.hasUpdate == true
? Colors.orange
: Colors.green,
),
const SizedBox(height: 24),
// Title
Text(
_isChecking
? 'Checking for updates...'
: _updateInfo?.hasUpdate == true
? 'Update Available!'
: 'App is Up to Date',
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 16),
// Status message
if (_isChecking)
const CircularProgressIndicator()
else if (_errorMessage != null)
Text(
'Error: $_errorMessage',
style: const TextStyle(color: Colors.red),
textAlign: TextAlign.center,
)
else if (_updateInfo != null) ...[
// Current version
_buildInfoCard(
'Current Version',
_updateInfo!.currentVersion,
Icons.phone_android,
),
const SizedBox(height: 12),
// Latest version
_buildInfoCard(
'Latest Version',
_updateInfo!.latestVersion,
Icons.cloud_download,
color: _updateInfo!.hasUpdate ? Colors.green : null,
),
const SizedBox(height: 12),
// Package name
_buildInfoCard(
'Package Name',
_updateInfo!.packageName,
Icons.info_outline,
),
const SizedBox(height: 24),
// Action buttons
if (_updateInfo!.hasUpdate) ...[
ElevatedButton.icon(
onPressed: _showUpdateDialog,
icon: const Icon(Icons.update),
label: const Text('Show Update Dialog'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(
horizontal: 24,
vertical: 12,
),
),
),
const SizedBox(height: 12),
OutlinedButton.icon(
onPressed: _showForceUpdateDialog,
icon: const Icon(Icons.warning),
label: const Text('Show Force Update Dialog'),
style: OutlinedButton.styleFrom(
padding: const EdgeInsets.symmetric(
horizontal: 24,
vertical: 12,
),
),
),
],
],
const SizedBox(height: 24),
// Check again button
OutlinedButton.icon(
onPressed: _isChecking ? null : _checkForUpdate,
icon: const Icon(Icons.refresh),
label: const Text('Check Again'),
),
],
),
),
),
);
}
Widget _buildInfoCard(String label, String value, IconData icon,
{Color? color}) {
return Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.grey[100],
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: color ?? Colors.grey[300]!,
width: 2,
),
),
child: Row(
children: [
Icon(icon, color: color),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
label,
style: TextStyle(
fontSize: 12,
color: Colors.grey[600],
),
),
const SizedBox(height: 4),
Text(
value,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: color,
),
),
],
),
),
],
),
);
}
}