native_updater

当有最新应用版本通过用户定义的逻辑可用时,会显示一个简单的警报提示小部件。如今的现代应用商店很少需要说服用户更新,因为大多数用户已经在使用自动更新功能。然而,有时应用需要比平时更快地更新,而不断提醒用户更新会促使他们更快地进行更新。

该UI有两种风格:Android的Material Design和iOS的Cupertino。UpdateMaterialAlert小部件用于显示原生的Android警报提示,UpdateCupertinoAlert小部件用于显示原生的iOS警报提示。

通过GitHub安装(仅供测试)

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^0.1.3

  # Add this inside pubspec.yaml
  native_updater:
    git: https://github.com/loadsmileau/native_updater

设置

Android

已准备就绪。

iOS

要能在Cupertino Alert Dialog中显示您的应用名称,请将以下键添加到您的Info.plist文件中,该文件位于<project root>/ios/Runner/Info.plist

<key>CFBundleDisplayName</key>
<string>YOUR APP NAME</string>

用法

只需在您想显示更新警报的任何地方添加此代码,它就会处理其余的事情。

NativeUpdater.displayUpdateAlert(
  context,
  forceUpdate: true,
);

或者使用可选参数来自定义警报。

NativeUpdater.displayUpdateAlert(
  context,
  forceUpdate: true,
  appStoreUrl: '<Your App Store URL>',
  playStoreUrl: '<Your Play Store URL>',
  iOSDescription: '<Your iOS Description>',
  iOSUpdateButtonLabel: '<Your iOS Update Button Label>',
  iOSCloseButtonLabel: '<Your iOS Close Button Label>',
  iOSIgnoreButtonLabel: '<Your iOS Ignore Button Label>',
);

参数说明

必需参数

context 是此小部件构建在树中的位置。

forceUpdate 用于告知警报是否强制更新。如果强制更新,则设置为true。如果允许稍后更新,则设置为false

可选参数

appStoreUrl 用于启动您的App Store URL(如果您正在为iOS开发)。请遵循此链接来查找您的App Store URL。

playStoreUrl 用于启动您的Play Store URL(如果您正在为Android开发)。请遵循此链接来查找您的Play Store URL。

iOSDescription 用于在UpdateCupertinoAlert中使用您的自定义警报描述。默认值为<应用名称>需要您更新到最新版本。在更新之前您无法使用此应用。<应用名称>建议您更新到最新版本。您可以在下载更新的同时继续使用此应用。

iOSUpdateButtonLabel 用于在UpdateCupertinoAlert中使用您的自定义更新按钮标签。默认值为更新

iOSCloseButtonLabel 用于在UpdateCupertinoAlert中使用您的自定义关闭按钮标签。默认值为关闭应用

iOSIgnoreButtonLabel 用于在UpdateCupertinoAlert中使用您的自定义忽略按钮标签。默认值为稍后

完整示例

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'native_updater example',
      home: Home(),
    );
  }
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  void initState() {
    super.initState();
    checkVersion();
  }

  Future<void> checkVersion() async {
    /// For example: You got status code of 412 from the
    /// response of HTTP request.
    /// Let's say the statusCode 412 requires you to force update
    int statusCode = 412;

    /// This could be kept in our local
    int localVersion = 9;

    /// This could get from the API
    int serverLatestVersion = 10;

    Future.delayed(Duration.zero, () {
      if (statusCode == 412) {
        NativeUpdater.displayUpdateAlert(
          context,
          forceUpdate: true,
          appStoreUrl: '<Your App Store URL>',
          playStoreUrl: '<Your Play Store URL>',
          iOSDescription: '<Your iOS description>',
          iOSUpdateButtonLabel: 'Upgrade',
          iOSCloseButtonLabel: 'Exit',
        );
      } else if (serverLatestVersion > localVersion) {
        NativeUpdater.displayUpdateAlert(
          context,
          forceUpdate: false,
          appStoreUrl: '<Your App Store URL>',
          playStoreUrl: '<Your Play Store URL>',
          iOSDescription: '<Your description>',
          iOSUpdateButtonLabel: 'Upgrade',
          iOSIgnoreButtonLabel: 'Next Time',
        );
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Your App'),
      ),
      body: Center(
        child: Text('Testing...'),
      ),
    );
  }
}

Material Alert的截图

灵活更新流程示例

android_flexible_flow--2-

即时更新流程示例

android_immediate_flow

Cupertino Alert的截图

强制更新 稍后可更新
cupertino_example_1 cupertino_example_2

GitHub

https://github.com/ofload/native_updater