app.dart 5.21 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';

xster's avatar
xster committed
7
import 'package:flutter/cupertino.dart';
8
import 'package:flutter/foundation.dart' show defaultTargetPlatform;
9
import 'package:flutter/material.dart';
10
import 'package:flutter/scheduler.dart' show timeDilation;
11 12
import 'package:flutter_gallery/demo/shrine/model/app_state_model.dart';
import 'package:scoped_model/scoped_model.dart';
13

14 15 16
import 'package:url_launcher/url_launcher.dart';

import 'demos.dart';
17
import 'home.dart';
18 19 20 21
import 'options.dart';
import 'scales.dart';
import 'themes.dart';
import 'updater.dart';
22

23
class GalleryApp extends StatefulWidget {
24
  const GalleryApp({
25
    Key key,
26
    this.updateUrlFetcher,
27 28 29
    this.enablePerformanceOverlay = true,
    this.enableRasterCacheImagesCheckerboard = true,
    this.enableOffscreenLayersCheckerboard = true,
30
    this.onSendFeedback,
31
    this.testMode = false,
32
  }) : super(key: key);
33 34

  final UpdateUrlFetcher updateUrlFetcher;
35
  final bool enablePerformanceOverlay;
36 37
  final bool enableRasterCacheImagesCheckerboard;
  final bool enableOffscreenLayersCheckerboard;
38
  final VoidCallback onSendFeedback;
39
  final bool testMode;
40

41
  @override
42
  _GalleryAppState createState() => _GalleryAppState();
43 44
}

45 46
class _GalleryAppState extends State<GalleryApp> {
  GalleryOptions _options;
47
  Timer _timeDilationTimer;
48
  AppStateModel model;
49

50 51 52 53
  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
54
    return Map<String, WidgetBuilder>.fromIterable(
55 56 57 58 59 60
      kAllGalleryDemos,
      key: (dynamic demo) => '${demo.routeName}',
      value: (dynamic demo) => demo.buildRoute,
    );
  }

61 62 63
  @override
  void initState() {
    super.initState();
64
    _options = GalleryOptions(
65
      themeMode: ThemeMode.system,
66 67 68 69
      textScaleFactor: kAllGalleryTextScaleValues[0],
      timeDilation: timeDilation,
      platform: defaultTargetPlatform,
    );
70
    model = AppStateModel()..loadProducts();
71 72 73 74 75 76 77 78
  }

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

80 81 82 83 84 85 86 87 88
  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.
89
          _timeDilationTimer = Timer(const Duration(milliseconds: 150), () {
90 91 92 93 94 95 96 97 98 99 100 101
            timeDilation = newOptions.timeDilation;
          });
        } else {
          timeDilation = newOptions.timeDilation;
        }
      }

      _options = newOptions;
    });
  }

  Widget _applyTextScaleFactor(Widget child) {
102
    return Builder(
103
      builder: (BuildContext context) {
104
        return MediaQuery(
105 106 107 108 109 110
          data: MediaQuery.of(context).copyWith(
            textScaleFactor: _options.textScaleFactor.scale,
          ),
          child: child,
        );
      },
111 112 113
    );
  }

114
  @override
115
  Widget build(BuildContext context) {
116
    Widget home = GalleryHome(
117
      testMode: widget.testMode,
118
      optionsPage: GalleryOptionsPage(
119 120 121
        options: _options,
        onOptionsChanged: _handleOptionsChanged,
        onSendFeedback: widget.onSendFeedback ?? () {
122
          launch('https://github.com/flutter/flutter/issues/new/choose', forceSafariVC: false);
123 124
        },
      ),
125 126
    );

127
    if (widget.updateUrlFetcher != null) {
128
      home = Updater(
129
        updateUrlFetcher: widget.updateUrlFetcher,
130
        child: home,
131
      );
132
    }
133 134 135 136

    return ScopedModel<AppStateModel>(
      model: model,
      child: MaterialApp(
137 138 139
        theme: kLightGalleryTheme.copyWith(platform: _options.platform),
        darkTheme: kDarkGalleryTheme.copyWith(platform: _options.platform),
        themeMode: _options.themeMode,
140 141 142 143 144 145 146 147 148
        title: 'Flutter Gallery',
        color: Colors.grey,
        showPerformanceOverlay: _options.showPerformanceOverlay,
        checkerboardOffscreenLayers: _options.showOffscreenLayersCheckerboard,
        checkerboardRasterCacheImages: _options.showRasterCacheImagesCheckerboard,
        routes: _buildRoutes(),
        builder: (BuildContext context, Widget child) {
          return Directionality(
            textDirection: _options.textDirection,
149 150 151 152
            child: _applyTextScaleFactor(
              // Specifically use a blank Cupertino theme here and do not transfer
              // over the Material primary color etc except the brightness to
              // showcase standard iOS looks.
153 154 155 156 157 158 159 160
              Builder(builder: (BuildContext context) {
                return CupertinoTheme(
                  data: CupertinoThemeData(
                    brightness: Theme.of(context).brightness,
                  ),
                  child: child,
                );
              }),
161
            ),
162 163 164 165
          );
        },
        home: home,
      ),
166 167 168
    );
  }
}