Flutter应用更新管理插件simpler_updater的使用

Flutter应用更新管理插件simpler_updater的使用

A Flutter package that prompts users to update their app when a newer version is available in the app store. 只需检查Flutter应用商店中的新版本。

功能

soon Features

开始使用

soon start 开始使用该包。

使用方法

soon usage 到/example文件夹。

const like = 'sample';

示例代码

以下是一个简单的示例,展示如何在应用中集成simple_updater插件:

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('应用更新管理'),
        ),
        body: Center(
          child: UpdateButton(),
        ),
      ),
    );
  }
}

class UpdateButton extends StatefulWidget {
  @override
  _UpdateButtonState createState() => _UpdateButtonState();
}

class _UpdateButtonState extends State<UpdateButton> {
  bool _isUpdating = false;

  Future<void> checkForUpdate() async {
    if (_isUpdating) return; // 防止重复点击

    setState(() {
      _isUpdating = true;
    });

    try {
      final result = await SimpleUpdater.checkForUpdate();
      if (result.updateAvailable) {
        await SimpleUpdater.showUpdateDialog(context);
      } else {
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('当前已是最新版本')));
      }
    } catch (e) {
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('检查更新时发生错误:$e')));
    } finally {
      setState(() {
        _isUpdating = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: checkForUpdate,
      child: Text(_isUpdating ? '正在检查...' : '检查更新'),
    );
  }
}

其他信息

soon … 告诉用户更多关于该包的信息:在哪里可以找到更多信息,如何为该包做出贡献,如何提交问题,用户可以期望从包作者那里得到什么响应等等。


更多关于Flutter应用更新管理插件simpler_updater的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter应用更新管理插件simpler_updater的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


simpler_updater 是一个用于 Flutter 应用的轻量级更新管理插件,它可以帮助你轻松检查应用更新并引导用户进行更新。以下是使用 simpler_updater 的基本步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 simpler_updater 的依赖:

dependencies:
  flutter:
    sdk: flutter
  simpler_updater: ^1.0.0 # 请使用最新版本

然后运行 flutter pub get 来获取依赖。

2. 初始化 simpler_updater

在你的应用的 main.dart 文件中,初始化 simpler_updater。你可以在 main() 函数中添加以下代码:

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

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

  // 初始化 simpler_updater
  final updater = SimplerUpdater(
    appId: 'com.example.yourapp', // 你的应用的包名
    storeUrl: 'https://play.google.com/store/apps/details?id=com.example.yourapp', // 应用商店的URL
  );

  runApp(MyApp(updater: updater));
}

class MyApp extends StatelessWidget {
  final SimplerUpdater updater;

  MyApp({required this.updater});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(updater: updater),
    );
  }
}

3. 检查更新

MyHomePage 或其他页面中,你可以调用 checkForUpdate() 方法来检查更新。例如:

class MyHomePage extends StatelessWidget {
  final SimplerUpdater updater;

  MyHomePage({required this.updater});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Demo Home Page'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () async {
            final updateAvailable = await updater.checkForUpdate();
            if (updateAvailable) {
              updater.showUpdateDialog(context);
            } else {
              ScaffoldMessenger.of(context).showSnackBar(
                SnackBar(content: Text('Your app is up to date!')),
              );
            }
          },
          child: Text('Check for Update'),
        ),
      ),
    );
  }
}

4. 显示更新对话框

如果检测到更新,你可以使用 showUpdateDialog() 方法显示一个对话框,提示用户更新应用:

updater.showUpdateDialog(context);

5. 处理更新逻辑

simpler_updater 会自动处理应用商店的跳转,用户点击更新按钮后,应用商店会打开,用户可以从中更新应用。

6. 自定义更新对话框

你可以通过 UpdateDialogConfig 来自定义更新对话框的样式和行为。例如:

updater.showUpdateDialog(
  context,
  config: UpdateDialogConfig(
    title: 'New Update Available',
    message: 'A new version of the app is available. Please update to continue.',
    updateButtonText: 'Update Now',
    laterButtonText: 'Later',
    forceUpdate: true, // 强制更新
  ),
);
回到顶部