updater.dart 2.03 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
6 7

import 'package:url_launcher/url_launcher.dart';
8

9
typedef UpdateUrlFetcher = Future<String?> Function();
10 11

class Updater extends StatefulWidget {
12 13
  const Updater({ required this.updateUrlFetcher, this.child, Key? key })
    : super(key: key);
14 15

  final UpdateUrlFetcher updateUrlFetcher;
16
  final Widget? child;
17 18

  @override
19
  State createState() => UpdaterState();
20 21 22 23 24 25 26 27 28
}

class UpdaterState extends State<Updater> {
  @override
  void initState() {
    super.initState();
    _checkForUpdates();
  }

29
  static DateTime? _lastUpdateCheck;
30
  Future<void> _checkForUpdates() async {
31 32
    // Only prompt once a day
    if (_lastUpdateCheck != null &&
33
        DateTime.now().difference(_lastUpdateCheck!) < const Duration(days: 1)) {
34
      return; // We already checked for updates recently
35
    }
36
    _lastUpdateCheck = DateTime.now();
37

38
    final String? updateUrl = await widget.updateUrlFetcher();
39
    final bool? wantsUpdate = await showDialog<bool>(context: context, builder: _buildDialog);
40
    if (wantsUpdate != null && updateUrl != null && wantsUpdate)
41
      launch(updateUrl);
42 43
  }

44
  Widget _buildDialog(BuildContext context) {
45 46
    final ThemeData theme = Theme.of(context);
    final TextStyle dialogTextStyle =
47
        theme.textTheme.subtitle1!.copyWith(color: theme.textTheme.caption!.color);
48
    return AlertDialog(
49
      title: const Text('Update Flutter Gallery?'),
50
      content: Text('A newer version is available.', style: dialogTextStyle),
51
      actions: <Widget>[
52
        TextButton(
53 54 55 56 57
          child: const Text('NO THANKS'),
          onPressed: () {
            Navigator.pop(context, false);
          },
        ),
58
        TextButton(
59 60 61 62 63 64 65
          child: const Text('UPDATE'),
          onPressed: () {
            Navigator.pop(context, true);
          },
        ),
      ],
    );
66 67 68
  }

  @override
69
  Widget build(BuildContext context) => widget.child!;
70
}