app.dart 5.81 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// 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
import 'package:scoped_model/scoped_model.dart';
12 13
import 'package:url_launcher/url_launcher.dart';

14
import '../demo/shrine/model/app_state_model.dart';
15
import 'demos.dart';
16
import 'home.dart';
17 18 19 20
import 'options.dart';
import 'scales.dart';
import 'themes.dart';
import 'updater.dart';
21

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

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

40
  @override
41
  State<GalleryApp> createState() => _GalleryAppState();
42 43
}

44
class _GalleryAppState extends State<GalleryApp> {
45 46
  GalleryOptions? _options;
  Timer? _timeDilationTimer;
47
  late final AppStateModel model = AppStateModel()..loadProducts();
48

49 50 51
  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:
52
    // https://api.flutter.dev/flutter/widgets/Navigator-class.html
53 54 55
    return <String, WidgetBuilder>{
      for (final GalleryDemo demo in kAllGalleryDemos) demo.routeName: demo.buildRoute,
    };
56 57
  }

58 59 60
  @override
  void initState() {
    super.initState();
61
    _options = GalleryOptions(
62
      themeMode: ThemeMode.system,
63
      textScaleFactor: kAllGalleryTextScaleValues[0],
64
      visualDensity: kAllGalleryVisualDensityValues[0],
65 66 67
      timeDilation: timeDilation,
      platform: defaultTargetPlatform,
    );
68 69
  }

70 71
  @override
  void reassemble() {
72
    _options = _options!.copyWith(platform: defaultTargetPlatform);
73 74 75
    super.reassemble();
  }

76 77 78 79 80 81
  @override
  void dispose() {
    _timeDilationTimer?.cancel();
    _timeDilationTimer = null;
    super.dispose();
  }
82

83 84
  void _handleOptionsChanged(GalleryOptions newOptions) {
    setState(() {
85
      if (_options!.timeDilation != newOptions.timeDilation) {
86 87 88 89 90 91
        _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.
92
          _timeDilationTimer = Timer(const Duration(milliseconds: 150), () {
93 94 95 96 97 98 99 100 101 102 103 104
            timeDilation = newOptions.timeDilation;
          });
        } else {
          timeDilation = newOptions.timeDilation;
        }
      }

      _options = newOptions;
    });
  }

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

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

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

    return ScopedModel<AppStateModel>(
      model: model,
      child: MaterialApp(
140 141 142 143 144
        // The automatically applied scrollbars on desktop can cause a crash for
        // demos where many scrollables are all attached to the same
        // PrimaryScrollController. The gallery needs to be migrated before
        // enabling this. https://github.com/flutter/gallery/issues/523
        scrollBehavior: const MaterialScrollBehavior().copyWith(scrollbars: false),
145 146 147
        theme: kLightGalleryTheme.copyWith(platform: _options!.platform, visualDensity: _options!.visualDensity!.visualDensity),
        darkTheme: kDarkGalleryTheme.copyWith(platform: _options!.platform, visualDensity: _options!.visualDensity!.visualDensity),
        themeMode: _options!.themeMode,
148 149
        title: 'Flutter Gallery',
        color: Colors.grey,
150 151 152
        showPerformanceOverlay: _options!.showPerformanceOverlay,
        checkerboardOffscreenLayers: _options!.showOffscreenLayersCheckerboard,
        checkerboardRasterCacheImages: _options!.showRasterCacheImagesCheckerboard,
153
        routes: _buildRoutes(),
154
        builder: (BuildContext context, Widget? child) {
155
          return Directionality(
156
            textDirection: _options!.textDirection,
157 158 159 160
            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.
161 162 163 164 165
              Builder(builder: (BuildContext context) {
                return CupertinoTheme(
                  data: CupertinoThemeData(
                    brightness: Theme.of(context).brightness,
                  ),
166
                  child: child!,
167 168
                );
              }),
169
            ),
170 171 172 173
          );
        },
        home: home,
      ),
174 175 176
    );
  }
}