version_check_updater 0.0.1
version_check_updater: ^0.0.1 copied to clipboard
A simple and customizable Flutter package to handle version update notifications by comparing with App Store and Google Play Store.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:version_check_updater/version_check_updater.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Version Check Updater Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late VersionCheckUpdater _versionChecker;
VersionInfo? _versionInfo;
bool _isChecking = false;
String _currentAppName = 'Instagram';
bool _useTestMode = false;
// Popular apps for testing
final Map<String, Map<String, String>> _testApps = {
'Instagram': {
'android': 'com.instagram.android',
'ios': 'com.burbn.instagram',
},
'Spotify': {
'android': 'com.spotify.music',
'ios': 'com.spotify.client',
},
'Twitter': {
'android': 'com.twitter.android',
'ios': 'com.atebits.Tweetie2',
},
'Facebook': {
'android': 'com.facebook.katana',
'ios': 'com.facebook.Facebook',
},
'TikTok': {
'android': 'com.zhiliaoapp.musically',
'ios': 'com.zhiliaoapp.musically',
},
};
@override
void initState() {
super.initState();
_initializeVersionChecker();
}
void _initializeVersionChecker() {
final appIds = _testApps[_currentAppName]!;
// Reset the singleton to force new configuration
VersionCheckUpdater.reset();
// Configure version checker
_versionChecker = VersionCheckUpdater(
config: UpdateConfig(
androidId: appIds['android'],
iosId: appIds['ios'],
showReleaseNotes: true,
forceUpdate: false,
checkInterval: const Duration(hours: 24),
dialogConfig: UpdateDialogConfig(
title: 'New update available',
updateButtonText: 'Update now',
laterButtonText: 'Later',
showAppIcon: true,
),
),
);
}
Future<void> _checkForUpdates() async {
setState(() {
_isChecking = true;
_versionInfo = null;
});
try {
final versionInfo = await _versionChecker.checkForUpdate(force: true);
if (mounted) {
setState(() {
_versionInfo = versionInfo;
_isChecking = false;
});
if (versionInfo == null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Could not fetch version info for $_currentAppName'),
backgroundColor: Colors.orange,
),
);
}
}
} catch (e) {
if (mounted) {
setState(() {
_isChecking = false;
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Error: ${e.toString()}'),
backgroundColor: Colors.red,
),
);
}
}
}
Future<void> _showUpdateDialog() async {
await _versionChecker.showUpdateDialog(
context,
force: true,
onLater: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Update postponed'),
),
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('Version Check Updater'),
),
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.system_update,
size: 80,
color: Colors.blue,
),
const SizedBox(height: 24),
Text(
'Version Check Updater Demo',
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 24),
// Test mode toggle
Card(
color: Colors.green.shade50,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
const Text(
'Test Mode',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
SwitchListTile(
title: const Text('Use simulated update'),
subtitle: const Text('Shows update dialog with version 2.0.0'),
value: _useTestMode,
onChanged: (bool value) {
setState(() {
_useTestMode = value;
_versionInfo = null;
});
if (value) {
VersionCheckUpdater.enableTestMode(mockStoreVersion: '2.0.0');
} else {
VersionCheckUpdater.disableTestMode();
}
_initializeVersionChecker();
},
),
],
),
),
),
const SizedBox(height: 16),
// App selector dropdown
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Text(
_useTestMode ? 'Test Mode Active' : 'Select an app to test:',
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
DropdownButton<String>(
value: _currentAppName,
isExpanded: true,
items: _testApps.keys.map((String appName) {
return DropdownMenuItem<String>(
value: appName,
child: Text(appName),
);
}).toList(),
onChanged: _useTestMode ? null : (String? newValue) {
if (newValue != null) {
setState(() {
_currentAppName = newValue;
_versionInfo = null;
});
_initializeVersionChecker();
}
},
),
const SizedBox(height: 8),
Text(
'Android: ${_testApps[_currentAppName]!["android"]}',
style: Theme.of(context).textTheme.bodySmall,
),
Text(
'iOS: ${_testApps[_currentAppName]!["ios"]}',
style: Theme.of(context).textTheme.bodySmall,
),
],
),
),
),
const SizedBox(height: 24),
if (_isChecking)
Column(
children: [
const CircularProgressIndicator(),
const SizedBox(height: 16),
Text('Checking $_currentAppName version...'),
],
)
else if (_versionInfo != null) ...[
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
ListTile(
leading: const Icon(Icons.phone_android),
title: const Text('Current version'),
subtitle: Text(_versionInfo!.currentVersion),
),
ListTile(
leading: const Icon(Icons.store),
title: const Text('Store version'),
subtitle: Text(_versionInfo!.storeVersion),
),
ListTile(
leading: Icon(
_versionInfo!.isUpdateAvailable
? Icons.update
: Icons.check_circle,
color: _versionInfo!.isUpdateAvailable
? Colors.orange
: Colors.green,
),
title: const Text('Status'),
subtitle: Text(
_versionInfo!.isUpdateAvailable
? 'Update available'
: 'You are on the latest version',
),
),
],
),
),
),
const SizedBox(height: 24),
ElevatedButton.icon(
onPressed: _checkForUpdates,
icon: const Icon(Icons.refresh),
label: const Text('Check for updates'),
),
if (_versionInfo!.isUpdateAvailable) ...[
const SizedBox(height: 16),
ElevatedButton.icon(
onPressed: _showUpdateDialog,
icon: const Icon(Icons.system_update_alt),
label: const Text('Show update dialog'),
style: ElevatedButton.styleFrom(
backgroundColor: Theme.of(context).colorScheme.primary,
foregroundColor: Colors.white,
),
),
],
const SizedBox(height: 48),
Card(
color: Colors.blue.shade50,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
const Icon(Icons.info_outline, color: Colors.blue),
const SizedBox(height: 8),
Text(
'This example uses real app IDs for demonstration.\n'
'In your app, replace these with your own package IDs:\n\n'
'androidId: "com.yourcompany.app"\n'
'iosId: "com.yourcompany.app"',
style: Theme.of(context).textTheme.bodySmall,
textAlign: TextAlign.center,
),
],
),
),
),
] else ...[
// No version info yet
const Icon(
Icons.touch_app,
size: 48,
color: Colors.grey,
),
const SizedBox(height: 16),
const Text(
'Tap "Check for updates" to start',
style: TextStyle(color: Colors.grey),
),
const SizedBox(height: 24),
ElevatedButton.icon(
onPressed: _checkForUpdates,
icon: const Icon(Icons.refresh),
label: const Text('Check for updates'),
),
],
],
),
),
),
);
}
}