native_updater
when a latest app version is available via user defined logic, a simple alert prompt widget is displayed. with today’s modern app stores, there is little need to persuade users to update because most of them are already using the auto update feature. however, there may be times when an app needs to be updated more quickly than usual, and nagging a user to update will entice the update sooner.
the ui comes in two flavors: material design for android and cupertino for ios. the updatematerialalert widget is used to display the native android alert prompt, and the updatecupertinoalert widget is used to display the native ios alert prompt.
installation via github (for test only)
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.3
# add this inside pubspec.yaml
native_updater:
git: https://github.com/loadsmileau/native_updater
setup
android
already good to go.
ios
to be able to show your app name in the cupertino alert dialog, add the following keys to your info.plist file, located in <project root>/ios/runner/info.plist
:
<key>cfbundledisplayname</key>
<string>your app name</string>
usage
just add this code whenever you want to display the update alert, and it will handle the rest.
nativeupdater.displayupdatealert(
context,
forceupdate: true,
);
or with the optional parameters to customize the alert.
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>',
);
parameters explanation
required parameters
context is the location in the tree where this widget builds.
forceupdate is to tell whether the alert is forcing an update or not. set to true
if you are forcing an update. set to false
if you are giving an option to update later.
optional parameters
appstoreurl is to launch your app store url if you’re developing for ios. follow this link on how to find your app store url.
playstoreurl is to launch your play store url if you’re developing for android. follow this link on how to find your play store url.
iosdescription is to use your custom alert description on updatecupertinoalert
. the default is <app name> requires that you update to the latest version. you cannot use this app until it is updated.
or <app name> recommends that you update to the latest version. you can keep using this app while downloading the update.
iosupdatebuttonlabel is to use your custom update button label on updatecupertinoalert
. the default is update
.
iosclosebuttonlabel is to use your custom close button label onupdatecupertinoalert
. the default is close app
.
iosignorebuttonlabel is to use your custom ignore button label onupdatecupertinoalert
. the default is later
.
full example
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...'),
),
);
}
}
screenshots of material alert
an example of a flexible update flow
an example of an immediate update flow
screenshots of cupertino alert
force update | can update later |
---|---|
Comments are closed.