updater.dart 2.06 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
  const Updater({ required this.updateUrlFetcher, this.child, super.key });
13 14

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

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

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

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

37
    final String? updateUrl = await widget.updateUrlFetcher();
38 39 40 41 42
    if (mounted) {
      final bool? wantsUpdate = await showDialog<bool>(context: context, builder: _buildDialog);
      if (wantsUpdate != null && updateUrl != null && wantsUpdate) {
        launchUrl(Uri.parse(updateUrl));
      }
43
    }
44 45
  }

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

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