Skip to main content

app_hub_upgrader 1.0.1 copy "app_hub_upgrader: ^1.0.1" to clipboard
app_hub_upgrader: ^1.0.1 copied to clipboard

A Flutter package for checking app version updates from API and displaying customizable update dialogs.

example/lib/main.dart

import 'package:app_hub_upgrader/app_hub_upgrader.dart';
import 'package:flutter/material.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // Initialize AppHubUpgrader with global parameters
  AppHubUpgrader.initialize(
    appID: 'your-app-id', // Replace with your actual app ID
    navigatorKey: MyApp.navigatorKey,
    useProduction: true,
    dialogConfig: UpdateDialogConfig(
      title: 'New Version Available!',
      updateButtonText: 'Update Now',
      laterButtonText: 'Maybe Later',
      primaryColor: Colors.blue,
      onUpdate: () {
        debugPrint('User clicked update button');
      },
      onLater: () {
        debugPrint('User clicked later button');
      },
    ),
  );

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // Global key for navigator - recommended for showing dialogs
  static final GlobalKey<NavigatorState> navigatorKey =
      GlobalKey<NavigatorState>();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'App Hub Upgrader Example',
      navigatorKey: navigatorKey, // Add navigator key
      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 AppHubUpgrader _upgrader;
  UpdateInfo? _updateInfo;
  bool _isChecking = false;
  String? _errorMessage;

  @override
  void initState() {
    super.initState();
    // Create instance using initialized values (no parameters needed!)
    _upgrader = AppHubUpgrader();

    // Or override specific values if needed:
    // _upgrader = AppHubUpgrader(useProduction: false);
  }

  Future<void> _checkForUpdate({bool showDialog = true}) async {
    setState(() {
      _isChecking = true;
      _errorMessage = null;
    });

    try {
      final updateInfo = await _upgrader.checkForUpdate(
        context: context,
        showDialog: showDialog,
      );

      setState(() {
        _updateInfo = updateInfo;
        _isChecking = false;
      });

      if (!mounted) return;

      if (updateInfo != null && updateInfo.updateAvailable) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text('Update available: ${updateInfo.latestVersion}'),
            backgroundColor: Colors.green,
          ),
        );
      } else {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(
            content: Text('You are using the latest version!'),
            backgroundColor: Colors.blue,
          ),
        );
      }
    } catch (e) {
      setState(() {
        _errorMessage = e.toString();
        _isChecking = false;
      });
      if (context.mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Error: $e'), backgroundColor: Colors.red),
        );
      }
    }
  }

  Future<void> _checkOnly() async {
    setState(() {
      _isChecking = true;
      _errorMessage = null;
    });

    try {
      final updateInfo = await _upgrader.checkForUpdate(
        context: context,
        checkOnly: true,
      );

      setState(() {
        _updateInfo = updateInfo;
        _isChecking = false;
      });

      if (updateInfo != null && updateInfo.updateAvailable) {
        // Show dialog manually - context is optional when using navigatorKey
        if (mounted) {
          await _upgrader.showUpdateDialog(updateInfo, context);
        }
      }
    } catch (e) {
      setState(() {
        _errorMessage = e.toString();
        _isChecking = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('App Hub Upgrader Example'),
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              const Icon(Icons.system_update, size: 80, color: Colors.blue),
              const SizedBox(height: 32),
              const Text(
                'App Hub Upgrader',
                style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
              ),
              const SizedBox(height: 8),
              const Text(
                'Check for app updates from API',
                style: TextStyle(fontSize: 16, color: Colors.grey),
              ),
              const SizedBox(height: 48),
              if (_isChecking)
                const CircularProgressIndicator()
              else
                Column(
                  children: [
                    ElevatedButton.icon(
                      onPressed: () => _checkForUpdate(showDialog: true),
                      icon: const Icon(Icons.check_circle),
                      label: const Text('Check for Update (Auto Dialog)'),
                      style: ElevatedButton.styleFrom(
                        padding: const EdgeInsets.symmetric(
                          horizontal: 24,
                          vertical: 12,
                        ),
                      ),
                    ),
                    const SizedBox(height: 16),
                    ElevatedButton.icon(
                      onPressed: _checkOnly,
                      icon: const Icon(Icons.info),
                      label: const Text('Check Only (Manual Dialog)'),
                      style: ElevatedButton.styleFrom(
                        padding: const EdgeInsets.symmetric(
                          horizontal: 24,
                          vertical: 12,
                        ),
                      ),
                    ),
                  ],
                ),
              const SizedBox(height: 32),
              if (_updateInfo != null) ...[
                Container(
                  padding: const EdgeInsets.all(16),
                  decoration: BoxDecoration(
                    color: Colors.grey[200],
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const Text(
                        'Update Information:',
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 16,
                        ),
                      ),
                      const SizedBox(height: 8),
                      Text('Has Update: ${_updateInfo!.updateAvailable}'),
                      if (_updateInfo!.latestVersion != null)
                        Text('Latest Version: ${_updateInfo!.latestVersion}'),
                      Text('Forced Update: ${_updateInfo!.isForced}'),
                      if (_updateInfo!.downloadURL != null)
                        Text('Download URL: ${_updateInfo!.downloadURL}'),
                    ],
                  ),
                ),
              ],
              if (_errorMessage != null) ...[
                const SizedBox(height: 16),
                Container(
                  padding: const EdgeInsets.all(16),
                  decoration: BoxDecoration(
                    color: Colors.red[100],
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Text(
                    'Error: $_errorMessage',
                    style: const TextStyle(color: Colors.red),
                  ),
                ),
              ],
            ],
          ),
        ),
      ),
    );
  }
}
1
likes
120
points
39
downloads

Documentation

Documentation
API reference

Publisher

unverified uploader

Weekly Downloads

A Flutter package for checking app version updates from API and displaying customizable update dialogs.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, http, package_info_plus, url_launcher

More

Packages that depend on app_hub_upgrader