stock_settings.dart 7.36 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 7
import 'package:flutter/material.dart';

import 'stock_types.dart';
8

9
class StockSettings extends StatefulWidget {
10
  const StockSettings(this.configuration, this.updater, {super.key});
11

12 13
  final StockConfiguration configuration;
  final ValueChanged<StockConfiguration> updater;
14

15
  @override
16
  StockSettingsState createState() => StockSettingsState();
17
}
18

19
class StockSettingsState extends State<StockSettings> {
20
  void _handleOptimismChanged(bool? value) {
21
    value ??= false;
22
    sendUpdates(widget.configuration.copyWith(stockMode: value ? StockMode.optimistic : StockMode.pessimistic));
23 24 25
  }

  void _handleBackupChanged(bool value) {
26
    sendUpdates(widget.configuration.copyWith(backupMode: value ? BackupMode.enabled : BackupMode.disabled));
Ian Hickson's avatar
Ian Hickson committed
27 28 29
  }

  void _handleShowGridChanged(bool value) {
30
    sendUpdates(widget.configuration.copyWith(debugShowGrid: value));
31 32 33
  }

  void _handleShowSizesChanged(bool value) {
34
    sendUpdates(widget.configuration.copyWith(debugShowSizes: value));
35 36
  }

37
  void _handleShowBaselinesChanged(bool value) {
38
    sendUpdates(widget.configuration.copyWith(debugShowBaselines: value));
39 40 41
  }

  void _handleShowLayersChanged(bool value) {
42
    sendUpdates(widget.configuration.copyWith(debugShowLayers: value));
43 44 45
  }

  void _handleShowPointersChanged(bool value) {
46
    sendUpdates(widget.configuration.copyWith(debugShowPointers: value));
47 48 49
  }

  void _handleShowRainbowChanged(bool value) {
50
    sendUpdates(widget.configuration.copyWith(debugShowRainbow: value));
51 52 53
  }


54
  void _handleShowPerformanceOverlayChanged(bool value) {
55
    sendUpdates(widget.configuration.copyWith(showPerformanceOverlay: value));
56 57
  }

Hixie's avatar
Hixie committed
58
  void _handleShowSemanticsDebuggerChanged(bool value) {
59
    sendUpdates(widget.configuration.copyWith(showSemanticsDebugger: value));
Hixie's avatar
Hixie committed
60 61
  }

Adam Barth's avatar
Adam Barth committed
62
  void _confirmOptimismChange() {
63
    switch (widget.configuration.stockMode) {
64 65 66
      case StockMode.optimistic:
        _handleOptimismChanged(false);
      case StockMode.pessimistic:
67
        showDialog<bool>(
Adam Barth's avatar
Adam Barth committed
68
          context: context,
69
          builder: (BuildContext context) {
70
            return AlertDialog(
71 72 73
              title: const Text('Change mode?'),
              content: const Text('Optimistic mode means everything is awesome. Are you sure you can handle that?'),
              actions: <Widget>[
74
                TextButton(
75 76 77
                  child: const Text('NO THANKS'),
                  onPressed: () {
                    Navigator.pop(context, false);
78
                  },
79
                ),
80
                TextButton(
81 82 83
                  child: const Text('AGREE'),
                  onPressed: () {
                    Navigator.pop(context, true);
84
                  },
85 86 87 88
                ),
              ],
            );
          },
89
        ).then<void>(_handleOptimismChanged);
90 91 92
    }
  }

93
  void sendUpdates(StockConfiguration value) {
94
    widget.updater(value);
95 96
  }

97
  AppBar buildAppBar(BuildContext context) {
98
    return AppBar(
99
      title: const Text('Settings'),
100 101 102
    );
  }

103
  Widget buildSettingsPane(BuildContext context) {
104
    final List<Widget> rows = <Widget>[
105
      ListTile(
106 107
        leading: const Icon(Icons.thumb_up),
        title: const Text('Everything is awesome'),
108
        onTap: _confirmOptimismChange,
109
        trailing: Checkbox(
110
          value: widget.configuration.stockMode == StockMode.optimistic,
111
          onChanged: (bool? value) => _confirmOptimismChange(),
112
        ),
Ian Hickson's avatar
Ian Hickson committed
113
      ),
114
      ListTile(
115 116
        leading: const Icon(Icons.backup),
        title: const Text('Back up stock list to the cloud'),
117
        onTap: () { _handleBackupChanged(!(widget.configuration.backupMode == BackupMode.enabled)); },
118
        trailing: Switch(
119
          value: widget.configuration.backupMode == BackupMode.enabled,
120 121
          onChanged: _handleBackupChanged,
        ),
Ian Hickson's avatar
Ian Hickson committed
122
      ),
123
      ListTile(
124 125
        leading: const Icon(Icons.picture_in_picture),
        title: const Text('Show rendering performance overlay'),
126
        onTap: () { _handleShowPerformanceOverlayChanged(!widget.configuration.showPerformanceOverlay); },
127
        trailing: Switch(
128
          value: widget.configuration.showPerformanceOverlay,
129 130
          onChanged: _handleShowPerformanceOverlayChanged,
        ),
131
      ),
132
      ListTile(
133 134
        leading: const Icon(Icons.accessibility),
        title: const Text('Show semantics overlay'),
135
        onTap: () { _handleShowSemanticsDebuggerChanged(!widget.configuration.showSemanticsDebugger); },
136
        trailing: Switch(
137
          value: widget.configuration.showSemanticsDebugger,
138 139
          onChanged: _handleShowSemanticsDebuggerChanged,
        ),
Hixie's avatar
Hixie committed
140
      ),
Ian Hickson's avatar
Ian Hickson committed
141 142
    ];
    assert(() {
143
      // material grid and size construction lines are only available in debug mode
Ian Hickson's avatar
Ian Hickson committed
144
      rows.addAll(<Widget>[
145
        ListTile(
146 147
          leading: const Icon(Icons.border_clear),
          title: const Text('Show material grid (for debugging)'),
148
          onTap: () { _handleShowGridChanged(!widget.configuration.debugShowGrid); },
149
          trailing: Switch(
150
            value: widget.configuration.debugShowGrid,
151 152
            onChanged: _handleShowGridChanged,
          ),
153
        ),
154
        ListTile(
155 156
          leading: const Icon(Icons.border_all),
          title: const Text('Show construction lines (for debugging)'),
157
          onTap: () { _handleShowSizesChanged(!widget.configuration.debugShowSizes); },
158
          trailing: Switch(
159
            value: widget.configuration.debugShowSizes,
160 161
            onChanged: _handleShowSizesChanged,
          ),
162
        ),
163
        ListTile(
164 165
          leading: const Icon(Icons.format_color_text),
          title: const Text('Show baselines (for debugging)'),
166
          onTap: () { _handleShowBaselinesChanged(!widget.configuration.debugShowBaselines); },
167
          trailing: Switch(
168
            value: widget.configuration.debugShowBaselines,
169 170
            onChanged: _handleShowBaselinesChanged,
          ),
171
        ),
172
        ListTile(
173 174
          leading: const Icon(Icons.filter_none),
          title: const Text('Show layer boundaries (for debugging)'),
175
          onTap: () { _handleShowLayersChanged(!widget.configuration.debugShowLayers); },
176
          trailing: Switch(
177
            value: widget.configuration.debugShowLayers,
178 179
            onChanged: _handleShowLayersChanged,
          ),
180
        ),
181
        ListTile(
182 183
          leading: const Icon(Icons.mouse),
          title: const Text('Show pointer hit-testing (for debugging)'),
184
          onTap: () { _handleShowPointersChanged(!widget.configuration.debugShowPointers); },
185
          trailing: Switch(
186
            value: widget.configuration.debugShowPointers,
187 188
            onChanged: _handleShowPointersChanged,
          ),
189
        ),
190
        ListTile(
191 192
          leading: const Icon(Icons.gradient),
          title: const Text('Show repaint rainbow (for debugging)'),
193
          onTap: () { _handleShowRainbowChanged(!widget.configuration.debugShowRainbow); },
194
          trailing: Switch(
195
            value: widget.configuration.debugShowRainbow,
196 197
            onChanged: _handleShowRainbowChanged,
          ),
198
        ),
199
      ]);
Ian Hickson's avatar
Ian Hickson committed
200
      return true;
201
    }());
202
    return ListView(
203
      padding: const EdgeInsets.symmetric(vertical: 20.0),
204
      children: rows,
205 206 207
    );
  }

208
  @override
209
  Widget build(BuildContext context) {
210
    return Scaffold(
211
      appBar: buildAppBar(context),
212
      body: buildSettingsPane(context),
213
    );
214 215
  }
}