File size: 6,180 Bytes
7d80aa8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0a2378
 
 
 
 
 
 
 
7d80aa8
 
 
 
e0a2378
7d80aa8
 
 
 
 
 
 
 
 
 
e0a2378
 
 
 
 
 
 
 
7d80aa8
 
 
 
 
 
 
 
 
 
 
 
 
 
e0a2378
 
 
 
 
 
 
 
7d80aa8
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart' show defaultTargetPlatform;
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart' show timeDilation;
import 'package:scoped_model/scoped_model.dart';
import 'package:url_launcher/url_launcher.dart';

import '../demo/shrine/model/app_state_model.dart';
import 'demos.dart';
import 'home.dart';
import 'options.dart';
import 'scales.dart';
import 'themes.dart';
import 'updater.dart';

class GalleryApp extends StatefulWidget {
  const GalleryApp({
    super.key,
    this.updateUrlFetcher,
    this.enablePerformanceOverlay = true,
    this.enableRasterCacheImagesCheckerboard = true,
    this.enableOffscreenLayersCheckerboard = true,
    this.onSendFeedback,
    this.testMode = false,
  });

  final UpdateUrlFetcher? updateUrlFetcher;
  final bool enablePerformanceOverlay;
  final bool enableRasterCacheImagesCheckerboard;
  final bool enableOffscreenLayersCheckerboard;
  final VoidCallback? onSendFeedback;
  final bool testMode;

  @override
  State<GalleryApp> createState() => _GalleryAppState();
}

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

  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://api.flutter.dev/flutter/widgets/Navigator-class.html
    return <String, WidgetBuilder>{
      for (final GalleryDemo demo in kAllGalleryDemos) demo.routeName: demo.buildRoute,
    };
  }

  @override
  void initState() {
    super.initState();
    _options = GalleryOptions(
      themeMode: ThemeMode.system,
      textScaleFactor: kAllGalleryTextScaleValues[0],
      visualDensity: kAllGalleryVisualDensityValues[0],
      timeDilation: timeDilation,
      platform: defaultTargetPlatform,
    );
  }

  @override
  void reassemble() {
    _options = _options!.copyWith(platform: defaultTargetPlatform);
    super.reassemble();
  }

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

  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 = Timer(const Duration(milliseconds: 150), () {
            timeDilation = newOptions.timeDilation;
          });
        } else {
          timeDilation = newOptions.timeDilation;
        }
      }

      _options = newOptions;
    });
  }

  Widget _applyTextScaleFactor(Widget child) {
    return Builder(
      builder: (BuildContext context) {
        final double? textScaleFactor = _options!.textScaleFactor!.scale;
        return MediaQuery.withClampedTextScaling(
          minScaleFactor: textScaleFactor ?? 0.0,
          maxScaleFactor: textScaleFactor ?? double.infinity,
          child: child,
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    Widget home = GalleryHome(
      testMode: widget.testMode,
      optionsPage: GalleryOptionsPage(
        options: _options,
        onOptionsChanged: _handleOptionsChanged,
        onSendFeedback:
            widget.onSendFeedback ??
            () {
              launchUrl(
                Uri.parse('https://github.com/flutter/flutter/issues/new/choose'),
                mode: LaunchMode.externalApplication,
              );
            },
      ),
    );

    if (widget.updateUrlFetcher != null) {
      home = Updater(updateUrlFetcher: widget.updateUrlFetcher!, child: home);
    }

    return ScopedModel<AppStateModel>(
      model: model,
      child: MaterialApp(
        // 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),
        theme: kLightGalleryTheme.copyWith(
          platform: _options!.platform,
          visualDensity: _options!.visualDensity!.visualDensity,
        ),
        darkTheme: kDarkGalleryTheme.copyWith(
          platform: _options!.platform,
          visualDensity: _options!.visualDensity!.visualDensity,
        ),
        themeMode: _options!.themeMode,
        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,
            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.
              Builder(
                builder: (BuildContext context) {
                  return CupertinoTheme(
                    data: CupertinoThemeData(brightness: Theme.brightnessOf(context)),
                    child: child!,
                  );
                },
              ),
            ),
          );
        },
        home: home,
      ),
    );
  }
}