basic_force_update 1.0.6 copy "basic_force_update: ^1.0.6" to clipboard
basic_force_update: ^1.0.6 copied to clipboard

unlisted

Basic Force Update

example/lib/main.dart

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

void main() {
  runApp(const MainApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Force Update Example',
      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> {
  bool _showUpdateDialog = false;
  ForceUpdateType _updateType = ForceUpdateType.dialog;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Force Update Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const OptionalUpdateWidget(
              delegate: OptionalUpdateDelegate(
                version: '1.0.1', // Current app version

                title: 'Update Required',

                appStoreId:
                    'com.example.app', // Replace with your iOS App Store ID
                packageName:
                    'com.example.app', // Replace with your Android package name
                borderRadius: BorderRadius.all(Radius.circular(16)),
                backgroundColor: Colors.red,
                foregroundColor: Colors.white,
                icon: Icons.info,
              ),
              provider: DummyUpdateProvider(
                minimum: '1.0.1',
                latest: '1.0.2',
                active: true,
              ),
            ),
            const Text(
              'Force Update Example App',
              style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 20),
            const Text(
              'Current Version: 1.0.0\nRequired Version: 1.0.1',
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 20),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const Text('Update Type: '),
                const SizedBox(width: 10),
                SegmentedButton<ForceUpdateType>(
                  segments: const [
                    ButtonSegment<ForceUpdateType>(
                      value: ForceUpdateType.dialog,
                      label: Text('Dialog'),
                    ),
                    ButtonSegment<ForceUpdateType>(
                      value: ForceUpdateType.sheet,
                      label: Text('Sheet'),
                    ),
                  ],
                  selected: {_updateType},
                  onSelectionChanged: (Set<ForceUpdateType> newSelection) {
                    setState(() {
                      _updateType = newSelection.first;
                    });
                  },
                ),
              ],
            ),
            const SizedBox(height: 40),
            ElevatedButton(
              onPressed: () {
                setState(() {
                  _showUpdateDialog = !_showUpdateDialog;
                });
                if (_showUpdateDialog) {
                  if (_updateType == ForceUpdateType.dialog) {
                    showForceUpdateDialog(
                      context,
                      delegate: const ForceUpdateDelegate(
                        version: '1.0.0',
                        title: 'Manual Update Check',
                        description:
                            'This is a manually triggered update dialog.',
                        button: 'Update Now',
                        appStoreId: 'com.example.app',
                        packageName: 'com.example.app',
                      ),
                    );
                  } else {
                    showForceUpdateSheet(
                      context,
                      delegate: const ForceUpdateDelegate(
                        version: '1.0.0',
                        title: 'Manual Update Check',
                        description:
                            'This is a manually triggered update sheet.',
                        button: 'Update Now',
                        appStoreId: 'com.example.app',
                        packageName: 'com.example.app',
                      ),
                    );
                  }
                }
              },
              child: Text(_showUpdateDialog
                  ? 'Hide Update Dialog'
                  : 'Show Update Dialog'),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                if (_updateType == ForceUpdateType.dialog) {
                  showForceUpdateDialog(
                    context,
                    delegate: const ForceUpdateDelegate(
                      version: '1.0.0',
                      title: 'Update Available',
                      description:
                          'This is a manually triggered update UI based on your selection.',
                      button: 'Update Now',
                      appStoreId: 'com.example.app',
                      packageName: 'com.example.app',
                    ),
                  );
                } else {
                  showForceUpdateSheet(
                    context,
                    delegate: const ForceUpdateDelegate(
                      version: '1.0.0',
                      title: 'Update Available',
                      description:
                          'This is a manually triggered update UI based on your selection.',
                      button: 'Update Now',
                      appStoreId: 'com.example.app',
                      packageName: 'com.example.app',
                    ),
                  );
                }
              },
              child: const Text('Show Selected Update UI'),
            ),
          ],
        ),
      ),
    );
  }
}