app.dart 4.24 KB
Newer Older
1 2 3 4
// Copyright 2015 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.

5 6
import 'dart:async';

7
import 'package:flutter/foundation.dart' show defaultTargetPlatform;
8
import 'package:flutter/material.dart';
9
import 'package:flutter/scheduler.dart' show timeDilation;
10

11 12 13
import 'package:url_launcher/url_launcher.dart';

import 'demos.dart';
14
import 'home.dart';
15 16 17 18
import 'options.dart';
import 'scales.dart';
import 'themes.dart';
import 'updater.dart';
19

20
class GalleryApp extends StatefulWidget {
21
  const GalleryApp({
22
    Key key,
23 24
    this.updateUrlFetcher,
    this.enablePerformanceOverlay: true,
25 26
    this.enableRasterCacheImagesCheckerboard: true,
    this.enableOffscreenLayersCheckerboard: true,
27
    this.onSendFeedback,
28
  }) : super(key: key);
29 30

  final UpdateUrlFetcher updateUrlFetcher;
31
  final bool enablePerformanceOverlay;
32 33
  final bool enableRasterCacheImagesCheckerboard;
  final bool enableOffscreenLayersCheckerboard;
34 35
  final VoidCallback onSendFeedback;

36
  @override
37
  _GalleryAppState createState() => new _GalleryAppState();
38 39
}

40 41
class _GalleryAppState extends State<GalleryApp> {
  GalleryOptions _options;
42
  Timer _timeDilationTimer;
43

44 45 46 47 48 49 50 51 52 53 54
  Map<String, WidgetBuilder> _buildRoutes() {
    // For a different example of how to set up an application routing table
    // using named routes, consider the example in the Navigator class documentation:
    // https://docs.flutter.io/flutter/widgets/Navigator-class.html
    return new Map<String, WidgetBuilder>.fromIterable(
      kAllGalleryDemos,
      key: (dynamic demo) => '${demo.routeName}',
      value: (dynamic demo) => demo.buildRoute,
    );
  }

55 56 57
  @override
  void initState() {
    super.initState();
58 59 60 61 62 63
    _options = new GalleryOptions(
      theme: kLightGalleryTheme,
      textScaleFactor: kAllGalleryTextScaleValues[0],
      timeDilation: timeDilation,
      platform: defaultTargetPlatform,
    );
64 65 66 67 68 69 70 71
  }

  @override
  void dispose() {
    _timeDilationTimer?.cancel();
    _timeDilationTimer = null;
    super.dispose();
  }
72

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  void _handleOptionsChanged(GalleryOptions newOptions) {
    setState(() {
      if (_options.timeDilation != newOptions.timeDilation) {
        _timeDilationTimer?.cancel();
        _timeDilationTimer = null;
        if (newOptions.timeDilation > 1.0) {
          // We delay the time dilation change long enough that the user can see
          // that UI has started reacting and then we slam on the brakes so that
          // they see that the time is in fact now dilated.
          _timeDilationTimer = new Timer(const Duration(milliseconds: 150), () {
            timeDilation = newOptions.timeDilation;
          });
        } else {
          timeDilation = newOptions.timeDilation;
        }
      }

      _options = newOptions;
    });
  }

  Widget _applyTextScaleFactor(Widget child) {
95
    return new Builder(
96 97 98 99 100 101 102 103
      builder: (BuildContext context) {
        return new MediaQuery(
          data: MediaQuery.of(context).copyWith(
            textScaleFactor: _options.textScaleFactor.scale,
          ),
          child: child,
        );
      },
104 105 106
    );
  }

107
  @override
108
  Widget build(BuildContext context) {
109
    Widget home = new GalleryHome(
110 111 112 113
      optionsPage: new GalleryOptionsPage(
        options: _options,
        onOptionsChanged: _handleOptionsChanged,
        onSendFeedback: widget.onSendFeedback ?? () {
114
          launch('https://github.com/flutter/flutter/issues/new', forceSafariVC: false);
115 116
        },
      ),
117 118
    );

119
    if (widget.updateUrlFetcher != null) {
120
      home = new Updater(
121
        updateUrlFetcher: widget.updateUrlFetcher,
122
        child: home,
123
      );
124
    }
125

126
    return new MaterialApp(
127
      theme: _options.theme.data.copyWith(platform: _options.platform),
128
      title: 'Flutter Gallery',
129
      color: Colors.grey,
130 131 132 133
      showPerformanceOverlay: _options.showPerformanceOverlay,
      checkerboardOffscreenLayers: _options.showOffscreenLayersCheckerboard,
      checkerboardRasterCacheImages: _options.showRasterCacheImagesCheckerboard,
      routes: _buildRoutes(),
134 135
      builder: (BuildContext context, Widget child) {
        return new Directionality(
136 137
          textDirection: _options.textDirection,
          child: _applyTextScaleFactor(child),
138 139
        );
      },
140
      home: home,
141 142 143
    );
  }
}