|
| 1 | +// Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | + |
| 7 | +import 'model.dart'; |
| 8 | + |
| 9 | +/// Returns a clock [Widget] with [ClockModel]. |
| 10 | +/// |
| 11 | +/// Example: |
| 12 | +/// final myClockBuilder = (ClockModel model) => AnalogClock(model); |
| 13 | +/// |
| 14 | +/// Contestants: Do not edit this. |
| 15 | +typedef Widget ClockBuilder(ClockModel model); |
| 16 | + |
| 17 | +/// Wrapper for clock widget to allow for customizations. |
| 18 | +/// |
| 19 | +/// Puts the clock in landscape orientation with an aspect ratio of 5:3. |
| 20 | +/// Provides a drawer where users can customize the data that is sent to the |
| 21 | +/// clock. To show/hide the drawer, double-tap the clock. |
| 22 | +/// |
| 23 | +/// To use the [ClockCustomizer], pass your clock into it, using a ClockBuilder. |
| 24 | +/// |
| 25 | +/// ``` |
| 26 | +/// final myClockBuilder = (ClockModel model) => AnalogClock(model); |
| 27 | +/// return ClockCustomizer(myClockBuilder); |
| 28 | +/// ``` |
| 29 | +/// Contestants: Do not edit this. |
| 30 | +class ClockCustomizer extends StatefulWidget { |
| 31 | + const ClockCustomizer(this._clock); |
| 32 | + |
| 33 | + /// The clock widget with [ClockModel], to update and display. |
| 34 | + final ClockBuilder _clock; |
| 35 | + |
| 36 | + @override |
| 37 | + _ClockCustomizerState createState() => _ClockCustomizerState(); |
| 38 | +} |
| 39 | + |
| 40 | +class _ClockCustomizerState extends State<ClockCustomizer> { |
| 41 | + final _model = ClockModel(); |
| 42 | + ThemeMode _themeMode = ThemeMode.light; |
| 43 | + bool _configButtonShown = false; |
| 44 | + |
| 45 | + @override |
| 46 | + void initState() { |
| 47 | + super.initState(); |
| 48 | + _model.addListener(_handleModelChange); |
| 49 | + } |
| 50 | + |
| 51 | + @override |
| 52 | + void dispose() { |
| 53 | + _model.removeListener(_handleModelChange); |
| 54 | + _model.dispose(); |
| 55 | + super.dispose(); |
| 56 | + } |
| 57 | + |
| 58 | + void _handleModelChange() => setState(() {}); |
| 59 | + |
| 60 | + Widget _enumMenu<T>( |
| 61 | + String label, T value, List<T> items, ValueChanged<T> onChanged) { |
| 62 | + return InputDecorator( |
| 63 | + decoration: InputDecoration( |
| 64 | + labelText: label, |
| 65 | + ), |
| 66 | + child: DropdownButtonHideUnderline( |
| 67 | + child: DropdownButton<T>( |
| 68 | + value: value, |
| 69 | + isDense: true, |
| 70 | + onChanged: onChanged, |
| 71 | + items: items.map((T item) { |
| 72 | + return DropdownMenuItem<T>( |
| 73 | + value: item, |
| 74 | + child: Text(enumToString(item)), |
| 75 | + ); |
| 76 | + }).toList(), |
| 77 | + ), |
| 78 | + ), |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + Widget _switch(String label, bool value, ValueChanged<bool> onChanged) { |
| 83 | + return Row( |
| 84 | + children: <Widget>[ |
| 85 | + Expanded(child: Text(label)), |
| 86 | + Switch( |
| 87 | + value: value, |
| 88 | + onChanged: onChanged, |
| 89 | + ), |
| 90 | + ], |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + Widget _textField( |
| 95 | + String currentValue, String label, ValueChanged<Null> onChanged) { |
| 96 | + return TextField( |
| 97 | + decoration: InputDecoration( |
| 98 | + hintText: currentValue, |
| 99 | + helperText: label, |
| 100 | + ), |
| 101 | + onChanged: onChanged, |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + Widget _configDrawer(BuildContext context) { |
| 106 | + return SafeArea( |
| 107 | + child: Drawer( |
| 108 | + child: Padding( |
| 109 | + padding: const EdgeInsets.all(16.0), |
| 110 | + child: SingleChildScrollView( |
| 111 | + child: Column( |
| 112 | + children: <Widget>[ |
| 113 | + _textField(_model.location, 'Location', (String location) { |
| 114 | + setState(() { |
| 115 | + _model.location = location; |
| 116 | + }); |
| 117 | + }), |
| 118 | + _textField(_model.temperature.toString(), 'Temperature', |
| 119 | + (String temperature) { |
| 120 | + setState(() { |
| 121 | + _model.temperature = double.parse(temperature); |
| 122 | + }); |
| 123 | + }), |
| 124 | + _enumMenu('Theme', _themeMode, |
| 125 | + ThemeMode.values.toList()..remove(ThemeMode.system), |
| 126 | + (ThemeMode mode) { |
| 127 | + setState(() { |
| 128 | + _themeMode = mode; |
| 129 | + }); |
| 130 | + }), |
| 131 | + _switch('24-hour format', _model.is24HourFormat, (bool value) { |
| 132 | + setState(() { |
| 133 | + _model.is24HourFormat = value; |
| 134 | + }); |
| 135 | + }), |
| 136 | + _enumMenu( |
| 137 | + 'Weather', _model.weatherCondition, WeatherCondition.values, |
| 138 | + (WeatherCondition condition) { |
| 139 | + setState(() { |
| 140 | + _model.weatherCondition = condition; |
| 141 | + }); |
| 142 | + }), |
| 143 | + _enumMenu('Units', _model.unit, TemperatureUnit.values, |
| 144 | + (TemperatureUnit unit) { |
| 145 | + setState(() { |
| 146 | + _model.unit = unit; |
| 147 | + }); |
| 148 | + }), |
| 149 | + ], |
| 150 | + ), |
| 151 | + ), |
| 152 | + ), |
| 153 | + ), |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + Widget _configButton() { |
| 158 | + return Builder( |
| 159 | + builder: (BuildContext context) { |
| 160 | + return IconButton( |
| 161 | + icon: Icon(Icons.settings), |
| 162 | + tooltip: 'Configure clock', |
| 163 | + onPressed: () { |
| 164 | + Scaffold.of(context).openEndDrawer(); |
| 165 | + setState(() { |
| 166 | + _configButtonShown = false; |
| 167 | + }); |
| 168 | + }, |
| 169 | + ); |
| 170 | + }, |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + @override |
| 175 | + Widget build(BuildContext context) { |
| 176 | + final clock = Center( |
| 177 | + child: AspectRatio( |
| 178 | + aspectRatio: 5 / 3, |
| 179 | + child: Container( |
| 180 | + decoration: BoxDecoration( |
| 181 | + border: Border.all( |
| 182 | + width: 2, |
| 183 | + color: Theme.of(context).unselectedWidgetColor, |
| 184 | + ), |
| 185 | + ), |
| 186 | + child: widget._clock(_model), |
| 187 | + ), |
| 188 | + ), |
| 189 | + ); |
| 190 | + |
| 191 | + return MaterialApp( |
| 192 | + theme: ThemeData.light(), |
| 193 | + darkTheme: ThemeData.dark(), |
| 194 | + themeMode: _themeMode, |
| 195 | + debugShowCheckedModeBanner: false, |
| 196 | + home: Scaffold( |
| 197 | + resizeToAvoidBottomPadding: false, |
| 198 | + endDrawer: _configDrawer(context), |
| 199 | + body: SafeArea( |
| 200 | + child: GestureDetector( |
| 201 | + behavior: HitTestBehavior.opaque, |
| 202 | + onTap: () { |
| 203 | + setState(() { |
| 204 | + _configButtonShown = !_configButtonShown; |
| 205 | + }); |
| 206 | + }, |
| 207 | + child: Stack( |
| 208 | + children: [ |
| 209 | + clock, |
| 210 | + if (_configButtonShown) |
| 211 | + Positioned( |
| 212 | + top: 0, |
| 213 | + right: 0, |
| 214 | + child: Opacity( |
| 215 | + opacity: 0.7, |
| 216 | + child: _configButton(), |
| 217 | + ), |
| 218 | + ), |
| 219 | + ], |
| 220 | + ), |
| 221 | + ), |
| 222 | + ), |
| 223 | + ), |
| 224 | + ); |
| 225 | + } |
| 226 | +} |
0 commit comments