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

Ian Hickson's avatar
Ian Hickson committed
5
import 'dart:async';
6
import 'dart:developer' show Timeline, Flow;
7
import 'dart:io' show Platform;
Ian Hickson's avatar
Ian Hickson committed
8

9
import 'package:flutter/foundation.dart';
10 11
import 'package:flutter/scheduler.dart';
import 'package:flutter/widgets.dart' hide Flow;
Ian Hickson's avatar
Ian Hickson committed
12 13 14 15 16

import 'app_bar.dart';
import 'debug.dart';
import 'dialog.dart';
import 'flat_button.dart';
17
import 'list_tile.dart';
18
import 'material_localizations.dart';
Ian Hickson's avatar
Ian Hickson committed
19
import 'page.dart';
Ian Hickson's avatar
Ian Hickson committed
20
import 'progress_indicator.dart';
Ian Hickson's avatar
Ian Hickson committed
21
import 'scaffold.dart';
22
import 'scrollbar.dart';
Ian Hickson's avatar
Ian Hickson committed
23 24
import 'theme.dart';

25
/// A [ListTile] that shows an about box.
Ian Hickson's avatar
Ian Hickson committed
26
///
27 28
/// This widget is often added to an app's [Drawer]. When tapped it shows
/// an about box dialog with [showAboutDialog].
Ian Hickson's avatar
Ian Hickson committed
29 30
///
/// The about box will include a button that shows licenses for software used by
31 32
/// the application. The licenses shown are those returned by the
/// [LicenseRegistry] API, which can be used to add more licenses to the list.
Ian Hickson's avatar
Ian Hickson committed
33 34 35
///
/// If your application does not have a [Drawer], you should provide an
/// affordance to call [showAboutDialog] or (at least) [showLicensePage].
36 37
class AboutListTile extends StatelessWidget {
  /// Creates a list tile for showing an about box.
Ian Hickson's avatar
Ian Hickson committed
38 39 40 41
  ///
  /// The arguments are all optional. The application name, if omitted, will be
  /// derived from the nearest [Title] widget. The version, icon, and legalese
  /// values default to the empty string.
42
  const AboutListTile({
Ian Hickson's avatar
Ian Hickson committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    Key key,
    this.icon: const Icon(null),
    this.child,
    this.applicationName,
    this.applicationVersion,
    this.applicationIcon,
    this.applicationLegalese,
    this.aboutBoxChildren
  }) : super(key: key);

  /// The icon to show for this drawer item.
  ///
  /// By default no icon is shown.
  ///
  /// This is not necessarily the same as the image shown in the dialog box
  /// itself; which is controlled by the [applicationIcon] property.
  final Widget icon;

  /// The label to show on this drawer item.
  ///
  /// Defaults to a text widget that says "About Foo" where "Foo" is the
  /// application name specified by [applicationName].
  final Widget child;

  /// The name of the application.
  ///
  /// This string is used in the default label for this drawer item (see
  /// [child]) and as the caption of the [AboutDialog] that is shown.
  ///
  /// Defaults to the value of [Title.title], if a [Title] widget can be found.
73
  /// Otherwise, defaults to [Platform.resolvedExecutable].
Ian Hickson's avatar
Ian Hickson committed
74 75 76 77 78 79 80 81 82 83 84 85 86
  final String applicationName;

  /// The version of this build of the application.
  ///
  /// This string is shown under the application name in the [AboutDialog].
  ///
  /// Defaults to the empty string.
  final String applicationVersion;

  /// The icon to show next to the application name in the [AboutDialog].
  ///
  /// By default no icon is shown.
  ///
87 88 89
  /// Typically this will be an [ImageIcon] widget. It should honor the
  /// [IconTheme]'s [IconThemeData.size].
  ///
Ian Hickson's avatar
Ian Hickson committed
90 91
  /// This is not necessarily the same as the icon shown on the drawer item
  /// itself, which is controlled by the [icon] property.
92
  final Widget applicationIcon;
Ian Hickson's avatar
Ian Hickson committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

  /// A string to show in small print in the [AboutDialog].
  ///
  /// Typically this is a copyright notice.
  ///
  /// Defaults to the empty string.
  final String applicationLegalese;

  /// Widgets to add to the [AboutDialog] after the name, version, and legalese.
  ///
  /// This could include a link to a Web site, some descriptive text, credits,
  /// or other information to show in the about box.
  ///
  /// Defaults to nothing.
  final List<Widget> aboutBoxChildren;

  @override
  Widget build(BuildContext context) {
    assert(debugCheckHasMaterial(context));
112 113
    return new ListTile(
      leading: icon,
Hans Muller's avatar
Hans Muller committed
114 115
      title: child ??
        new Text(MaterialLocalizations.of(context).aboutListTileTitle(applicationName ?? _defaultApplicationName(context))),
116
      onTap: () {
Ian Hickson's avatar
Ian Hickson committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        showAboutDialog(
          context: context,
          applicationName: applicationName,
          applicationVersion: applicationVersion,
          applicationIcon: applicationIcon,
          applicationLegalese: applicationLegalese,
          children: aboutBoxChildren
        );
      }
    );
  }
}

/// Displays an [AboutDialog], which describes the application and provides a
/// button to show licenses for software used by the application.
///
/// The arguments correspond to the properties on [AboutDialog].
///
135
/// If the application has a [Drawer], consider using [AboutListTile] instead
Ian Hickson's avatar
Ian Hickson committed
136 137 138 139
/// of calling this directly.
///
/// If you do not need an about box in your application, you should at least
/// provide an affordance to call [showLicensePage].
140 141 142
///
/// The licenses shown on the [LicensePage] are those returned by the
/// [LicenseRegistry] API, which can be used to add more licenses to the list.
Ian Hickson's avatar
Ian Hickson committed
143 144 145
///
/// The `context` argument is passed to [showDialog], the documentation for
/// which discusses how it is used.
Ian Hickson's avatar
Ian Hickson committed
146 147 148 149
void showAboutDialog({
  @required BuildContext context,
  String applicationName,
  String applicationVersion,
150
  Widget applicationIcon,
Ian Hickson's avatar
Ian Hickson committed
151
  String applicationLegalese,
152
  List<Widget> children,
Ian Hickson's avatar
Ian Hickson committed
153
}) {
154
  assert(context != null);
155
  showDialog<void>(
Ian Hickson's avatar
Ian Hickson committed
156
    context: context,
157 158 159 160 161 162 163 164 165
    builder: (BuildContext context) {
      return new AboutDialog(
        applicationName: applicationName,
        applicationVersion: applicationVersion,
        applicationIcon: applicationIcon,
        applicationLegalese: applicationLegalese,
        children: children,
      );
    }
Ian Hickson's avatar
Ian Hickson committed
166 167 168 169 170 171 172 173
  );
}

/// Displays a [LicensePage], which shows licenses for software used by the
/// application.
///
/// The arguments correspond to the properties on [LicensePage].
///
174
/// If the application has a [Drawer], consider using [AboutListTile] instead
Ian Hickson's avatar
Ian Hickson committed
175 176 177 178
/// of calling this directly.
///
/// The [AboutDialog] shown by [showAboutDialog] includes a button that calls
/// [showLicensePage].
179 180 181
///
/// The licenses shown on the [LicensePage] are those returned by the
/// [LicenseRegistry] API, which can be used to add more licenses to the list.
Ian Hickson's avatar
Ian Hickson committed
182 183 184 185
void showLicensePage({
  @required BuildContext context,
  String applicationName,
  String applicationVersion,
186
  Widget applicationIcon,
Ian Hickson's avatar
Ian Hickson committed
187 188
  String applicationLegalese
}) {
189
  assert(context != null);
190
  Navigator.push(context, new MaterialPageRoute<void>(
191 192 193 194 195 196
    builder: (BuildContext context) => new LicensePage(
      applicationName: applicationName,
      applicationVersion: applicationVersion,
      applicationLegalese: applicationLegalese
    )
  ));
Ian Hickson's avatar
Ian Hickson committed
197 198 199 200 201 202 203
}

/// An about box. This is a dialog box with the application's icon, name,
/// version number, and copyright, plus a button to show licenses for software
/// used by the application.
///
/// To show an [AboutDialog], use [showAboutDialog].
204
///
205
/// If the application has a [Drawer], the [AboutListTile] widget can make the
206 207 208 209 210 211 212
/// process of showing an about dialog simpler.
///
/// The [AboutDialog] shown by [showAboutDialog] includes a button that calls
/// [showLicensePage].
///
/// The licenses shown on the [LicensePage] are those returned by the
/// [LicenseRegistry] API, which can be used to add more licenses to the list.
Ian Hickson's avatar
Ian Hickson committed
213 214 215 216 217 218
class AboutDialog extends StatelessWidget {
  /// Creates an about box.
  ///
  /// The arguments are all optional. The application name, if omitted, will be
  /// derived from the nearest [Title] widget. The version, icon, and legalese
  /// values default to the empty string.
219
  const AboutDialog({
Ian Hickson's avatar
Ian Hickson committed
220 221 222 223 224
    Key key,
    this.applicationName,
    this.applicationVersion,
    this.applicationIcon,
    this.applicationLegalese,
225
    this.children,
Ian Hickson's avatar
Ian Hickson committed
226 227 228 229 230
  }) : super(key: key);

  /// The name of the application.
  ///
  /// Defaults to the value of [Title.title], if a [Title] widget can be found.
231
  /// Otherwise, defaults to [Platform.resolvedExecutable].
Ian Hickson's avatar
Ian Hickson committed
232 233 234 235 236 237 238 239 240 241 242 243
  final String applicationName;

  /// The version of this build of the application.
  ///
  /// This string is shown under the application name.
  ///
  /// Defaults to the empty string.
  final String applicationVersion;

  /// The icon to show next to the application name.
  ///
  /// By default no icon is shown.
244 245 246 247
  ///
  /// Typically this will be an [ImageIcon] widget. It should honor the
  /// [IconTheme]'s [IconThemeData.size].
  final Widget applicationIcon;
Ian Hickson's avatar
Ian Hickson committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

  /// A string to show in small print.
  ///
  /// Typically this is a copyright notice.
  ///
  /// Defaults to the empty string.
  final String applicationLegalese;

  /// Widgets to add to the dialog box after the name, version, and legalese.
  ///
  /// This could include a link to a Web site, some descriptive text, credits,
  /// or other information to show in the about box.
  ///
  /// Defaults to nothing.
  final List<Widget> children;

  @override
  Widget build(BuildContext context) {
    final String name = applicationName ?? _defaultApplicationName(context);
    final String version = applicationVersion ?? _defaultApplicationVersion(context);
268
    final Widget icon = applicationIcon ?? _defaultApplicationIcon(context);
Ian Hickson's avatar
Ian Hickson committed
269
    List<Widget> body = <Widget>[];
270
    if (icon != null)
271
      body.add(new IconTheme(data: const IconThemeData(size: 48.0), child: icon));
272
    body.add(new Expanded(
Ian Hickson's avatar
Ian Hickson committed
273
      child: new Padding(
274
        padding: const EdgeInsets.symmetric(horizontal: 24.0),
275
        child: new ListBody(
Ian Hickson's avatar
Ian Hickson committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
          children: <Widget>[
            new Text(name, style: Theme.of(context).textTheme.headline),
            new Text(version, style: Theme.of(context).textTheme.body1),
            new Container(height: 18.0),
            new Text(applicationLegalese ?? '', style: Theme.of(context).textTheme.caption)
          ]
        )
      )
    ));
    body = <Widget>[
      new Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: body
      ),
    ];
    if (children != null)
      body.addAll(children);
293
    return new AlertDialog(
294
      content: new SingleChildScrollView(
295
        child: new ListBody(children: body),
Ian Hickson's avatar
Ian Hickson committed
296 297 298
      ),
      actions: <Widget>[
        new FlatButton(
299
          child: new Text(MaterialLocalizations.of(context).viewLicensesButtonLabel),
Ian Hickson's avatar
Ian Hickson committed
300 301 302 303 304 305 306 307 308 309 310
          onPressed: () {
            showLicensePage(
              context: context,
              applicationName: applicationName,
              applicationVersion: applicationVersion,
              applicationIcon: applicationIcon,
              applicationLegalese: applicationLegalese
            );
          }
        ),
        new FlatButton(
311
          child: new Text(MaterialLocalizations.of(context).closeButtonLabel),
Ian Hickson's avatar
Ian Hickson committed
312 313 314 315 316 317 318 319 320 321 322 323
          onPressed: () {
            Navigator.pop(context);
          }
        ),
      ]
    );
  }
}

/// A page that shows licenses for software used by the application.
///
/// To show a [LicensePage], use [showLicensePage].
324
///
325
/// The [AboutDialog] shown by [showAboutDialog] and [AboutListTile] includes
326 327 328 329
/// a button that calls [showLicensePage].
///
/// The licenses shown on the [LicensePage] are those returned by the
/// [LicenseRegistry] API, which can be used to add more licenses to the list.
330
class LicensePage extends StatefulWidget {
Ian Hickson's avatar
Ian Hickson committed
331 332 333 334 335
  /// Creates a page that shows licenses for software used by the application.
  ///
  /// The arguments are all optional. The application name, if omitted, will be
  /// derived from the nearest [Title] widget. The version and legalese values
  /// default to the empty string.
336 337 338
  ///
  /// The licenses shown on the [LicensePage] are those returned by the
  /// [LicenseRegistry] API, which can be used to add more licenses to the list.
Ian Hickson's avatar
Ian Hickson committed
339 340 341 342 343 344 345 346 347 348
  const LicensePage({
    Key key,
    this.applicationName,
    this.applicationVersion,
    this.applicationLegalese
  }) : super(key: key);

  /// The name of the application.
  ///
  /// Defaults to the value of [Title.title], if a [Title] widget can be found.
349
  /// Otherwise, defaults to [Platform.resolvedExecutable].
Ian Hickson's avatar
Ian Hickson committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
  final String applicationName;

  /// The version of this build of the application.
  ///
  /// This string is shown under the application name.
  ///
  /// Defaults to the empty string.
  final String applicationVersion;

  /// A string to show in small print.
  ///
  /// Typically this is a copyright notice.
  ///
  /// Defaults to the empty string.
  final String applicationLegalese;

366 367 368 369 370
  @override
  _LicensePageState createState() => new _LicensePageState();
}

class _LicensePageState extends State<LicensePage> {
Ian Hickson's avatar
Ian Hickson committed
371 372 373 374 375 376
  @override
  void initState() {
    super.initState();
    _initLicenses();
  }

377
  final List<Widget> _licenses = <Widget>[];
Ian Hickson's avatar
Ian Hickson committed
378 379 380
  bool _loaded = false;

  Future<Null> _initLicenses() async {
381 382
    final Flow flow = Flow.begin();
    Timeline.timeSync('_initLicenses()', () { }, flow: flow);
Ian Hickson's avatar
Ian Hickson committed
383
    await for (LicenseEntry license in LicenseRegistry.licenses) {
384 385
      if (!mounted)
        return;
386 387 388 389 390 391 392 393
      Timeline.timeSync('_initLicenses()', () { }, flow: Flow.step(flow.id));
      final List<LicenseParagraph> paragraphs =
        await SchedulerBinding.instance.scheduleTask<List<LicenseParagraph>>(
          () => license.paragraphs.toList(),
          Priority.animation,
          debugLabel: 'License',
          flow: flow,
        );
Ian Hickson's avatar
Ian Hickson committed
394
      setState(() {
395
        _licenses.add(const Padding(
396
          padding: const EdgeInsets.symmetric(vertical: 18.0),
397
          child: const Text(
Ian Hickson's avatar
Ian Hickson committed
398 399 400 401
            '🍀‬', // That's U+1F340. Could also use U+2766 (❦) if U+1F340 doesn't work everywhere.
            textAlign: TextAlign.center
          )
        ));
402
        _licenses.add(new Container(
403 404
          decoration: const BoxDecoration(
            border: const Border(bottom: const BorderSide(width: 0.0))
405 406 407
          ),
          child: new Text(
            license.packages.join(', '),
408
            style: const TextStyle(fontWeight: FontWeight.bold),
409 410 411
            textAlign: TextAlign.center
          )
        ));
412
        for (LicenseParagraph paragraph in paragraphs) {
Ian Hickson's avatar
Ian Hickson committed
413 414
          if (paragraph.indent == LicenseParagraph.centeredIndent) {
            _licenses.add(new Padding(
415
              padding: const EdgeInsets.only(top: 16.0),
Ian Hickson's avatar
Ian Hickson committed
416 417
              child: new Text(
                paragraph.text,
418
                style: const TextStyle(fontWeight: FontWeight.bold),
Ian Hickson's avatar
Ian Hickson committed
419 420 421 422 423 424
                textAlign: TextAlign.center
              )
            ));
          } else {
            assert(paragraph.indent >= 0);
            _licenses.add(new Padding(
425
              padding: new EdgeInsetsDirectional.only(top: 8.0, start: 16.0 * paragraph.indent),
Ian Hickson's avatar
Ian Hickson committed
426 427 428
              child: new Text(paragraph.text)
            ));
          }
429
        }
Ian Hickson's avatar
Ian Hickson committed
430
      });
431
    }
Ian Hickson's avatar
Ian Hickson committed
432 433 434
    setState(() {
      _loaded = true;
    });
435
    Timeline.timeSync('Build scheduled', () { }, flow: Flow.end(flow.id));
436 437
  }

Ian Hickson's avatar
Ian Hickson committed
438 439
  @override
  Widget build(BuildContext context) {
440 441
    final String name = widget.applicationName ?? _defaultApplicationName(context);
    final String version = widget.applicationVersion ?? _defaultApplicationVersion(context);
Hans Muller's avatar
Hans Muller committed
442
    final MaterialLocalizations localizations = MaterialLocalizations.of(context);
443 444 445 446
    final List<Widget> contents = <Widget>[
      new Text(name, style: Theme.of(context).textTheme.headline, textAlign: TextAlign.center),
      new Text(version, style: Theme.of(context).textTheme.body1, textAlign: TextAlign.center),
      new Container(height: 18.0),
447
      new Text(widget.applicationLegalese ?? '', style: Theme.of(context).textTheme.caption, textAlign: TextAlign.center),
448 449 450 451 452
      new Container(height: 18.0),
      new Text('Powered by Flutter', style: Theme.of(context).textTheme.body1, textAlign: TextAlign.center),
      new Container(height: 24.0),
    ];
    contents.addAll(_licenses);
Ian Hickson's avatar
Ian Hickson committed
453
    if (!_loaded) {
454
      contents.add(const Padding(
455
        padding: const EdgeInsets.symmetric(vertical: 24.0),
456 457
        child: const Center(
          child: const CircularProgressIndicator()
Ian Hickson's avatar
Ian Hickson committed
458 459 460
        )
      ));
    }
Ian Hickson's avatar
Ian Hickson committed
461 462
    return new Scaffold(
      appBar: new AppBar(
Hans Muller's avatar
Hans Muller committed
463
        title: new Text(localizations.licensesPageTitle),
Ian Hickson's avatar
Ian Hickson committed
464
      ),
Hans Muller's avatar
Hans Muller committed
465 466 467 468 469 470 471 472 473 474 475 476
      // All of the licenses page text is English. We don't want localized text
      // or text direction.
      body: new Localizations.override(
        locale: const Locale('en', 'US'),
        context: context,
        child: new DefaultTextStyle(
          style: Theme.of(context).textTheme.caption,
          child: new Scrollbar(
            child: new ListView(
              padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 12.0),
              children: contents,
            ),
477 478 479
          ),
        ),
      ),
Ian Hickson's avatar
Ian Hickson committed
480 481 482 483 484
    );
  }
}

String _defaultApplicationName(BuildContext context) {
485
  final Title ancestorTitle = context.ancestorWidgetOfExactType(Title);
486
  return ancestorTitle?.title ?? Platform.resolvedExecutable.split(Platform.pathSeparator).last;
Ian Hickson's avatar
Ian Hickson committed
487 488 489 490 491 492 493
}

String _defaultApplicationVersion(BuildContext context) {
  // TODO(ianh): Get this from the embedder somehow.
  return '';
}

494
Widget _defaultApplicationIcon(BuildContext context) {
Ian Hickson's avatar
Ian Hickson committed
495 496 497
  // TODO(ianh): Get this from the embedder somehow.
  return null;
}