main.dart 3.23 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
import 'package:flutter/material.dart';
6 7 8 9
import 'package:flutter/rendering.dart' show
  debugPaintBaselinesEnabled,
  debugPaintLayerBordersEnabled,
  debugPaintPointersEnabled,
10
  debugPaintSizeEnabled,
11
  debugRepaintRainbowEnabled;
12

13
import 'i18n/stock_strings.dart';
14 15 16 17 18
import 'stock_data.dart';
import 'stock_home.dart';
import 'stock_settings.dart';
import 'stock_symbol_viewer.dart';
import 'stock_types.dart';
19

20
class StocksApp extends StatefulWidget {
21
  const StocksApp({super.key});
22

23
  @override
24
  StocksAppState createState() => StocksAppState();
25
}
26

27
class StocksAppState extends State<StocksApp> {
28
  late StockData stocks = StockData();
29

30
  StockConfiguration _configuration = StockConfiguration(
31 32
    stockMode: StockMode.optimistic,
    backupMode: BackupMode.enabled,
33 34
    debugShowGrid: false,
    debugShowSizes: false,
35 36 37 38
    debugShowBaselines: false,
    debugShowLayers: false,
    debugShowPointers: false,
    debugShowRainbow: false,
Hixie's avatar
Hixie committed
39
    showPerformanceOverlay: false,
40
    showSemanticsDebugger: false,
41 42 43
  );

  void configurationUpdater(StockConfiguration value) {
44
    setState(() {
45
      _configuration = value;
46 47 48
    });
  }

49
  ThemeData get theme {
50
    switch (_configuration.stockMode) {
51
      case StockMode.optimistic:
52
        return ThemeData(
53
          useMaterial3: false,
54
          brightness: Brightness.light,
55
          primarySwatch: Colors.purple,
56 57
        );
      case StockMode.pessimistic:
58
        return ThemeData(
59
          useMaterial3: false,
60
          brightness: Brightness.dark,
61
          primarySwatch: Colors.purple,
62
        );
63
    }
64
  }
65

66
  Route<dynamic>? _getRoute(RouteSettings settings) {
67
    if (settings.name == '/stock') {
68
      final String? symbol = settings.arguments as String?;
69
      return MaterialPageRoute<void>(
70
        settings: settings,
71
        builder: (BuildContext context) => StockSymbolPage(symbol: symbol!, stocks: stocks),
72
      );
Hixie's avatar
Hixie committed
73
    }
74
    // The other paths we support are in the routes table.
Hixie's avatar
Hixie committed
75 76 77
    return null;
  }

78
  @override
79
  Widget build(BuildContext context) {
80 81
    assert(() {
      debugPaintSizeEnabled = _configuration.debugShowSizes;
82 83 84 85
      debugPaintBaselinesEnabled = _configuration.debugShowBaselines;
      debugPaintLayerBordersEnabled = _configuration.debugShowLayers;
      debugPaintPointersEnabled = _configuration.debugShowPointers;
      debugRepaintRainbowEnabled = _configuration.debugShowRainbow;
86
      return true;
87
    }());
88
    return MaterialApp(
89 90
      title: 'Stocks',
      theme: theme,
91 92
      localizationsDelegates: StockStrings.localizationsDelegates,
      supportedLocales: StockStrings.supportedLocales,
93
      debugShowMaterialGrid: _configuration.debugShowGrid,
94
      showPerformanceOverlay: _configuration.showPerformanceOverlay,
Hixie's avatar
Hixie committed
95
      showSemanticsDebugger: _configuration.showSemanticsDebugger,
96
      routes: <String, WidgetBuilder>{
97
         '/':         (BuildContext context) => StockHome(stocks, _configuration, configurationUpdater),
98
         '/settings': (BuildContext context) => StockSettings(_configuration, configurationUpdater),
Hixie's avatar
Hixie committed
99
      },
100
      onGenerateRoute: _getRoute,
101 102
    );
  }
103 104 105
}

void main() {
106
  runApp(const StocksApp());
107
}