From f3f1fd8d7c970b730410379b0d64bd31d2413da3 Mon Sep 17 00:00:00 2001 From: Hixie Date: Mon, 26 Oct 2015 11:02:09 -0700 Subject: [PATCH] Radio and ValueChanged Make Radio widgets take a type that describes the type of their value, so that you can catch when you use the wrong value. Standardise on ValueChanged instead of having a FooValueChanged for every value of Foo. --- examples/fitness/lib/feed.dart | 2 +- examples/stocks/lib/stock_home.dart | 4 +-- examples/widgets/card_collection.dart | 35 +++++++++++++------ .../sky/lib/src/material/checkbox.dart | 2 -- .../sky/lib/src/material/date_picker.dart | 14 ++++---- sky/packages/sky/lib/src/material/input.dart | 9 +++-- sky/packages/sky/lib/src/material/radio.dart | 11 +++--- sky/packages/sky/lib/src/material/switch.dart | 2 -- 8 files changed, 43 insertions(+), 36 deletions(-) diff --git a/examples/fitness/lib/feed.dart b/examples/fitness/lib/feed.dart index 57cb7c2dfe6..9abb84d558a 100644 --- a/examples/fitness/lib/feed.dart +++ b/examples/fitness/lib/feed.dart @@ -243,7 +243,7 @@ class AddItemDialogState extends State { for (String routeName in _labels.keys) { menuItems.add(new DialogMenuItem([ new Flexible(child: new Text(_labels[routeName])), - new Radio(value: routeName, groupValue: _addItemRoute, onChanged: _handleAddItemRouteChanged), + new Radio(value: routeName, groupValue: _addItemRoute, onChanged: _handleAddItemRouteChanged), ], onPressed: () => _handleAddItemRouteChanged(routeName))); } return new Dialog( diff --git a/examples/stocks/lib/stock_home.dart b/examples/stocks/lib/stock_home.dart index 42d5db4776e..03bf9d3d97a 100644 --- a/examples/stocks/lib/stock_home.dart +++ b/examples/stocks/lib/stock_home.dart @@ -123,7 +123,7 @@ class StockHomeState extends State { onPressed: () => _handleStockModeChange(StockMode.optimistic), child: new Row([ new Flexible(child: new Text('Optimistic')), - new Radio(value: StockMode.optimistic, groupValue: config.stockMode, onChanged: _handleStockModeChange) + new Radio(value: StockMode.optimistic, groupValue: config.stockMode, onChanged: _handleStockModeChange) ]) ), new DrawerItem( @@ -131,7 +131,7 @@ class StockHomeState extends State { onPressed: () => _handleStockModeChange(StockMode.pessimistic), child: new Row([ new Flexible(child: new Text('Pessimistic')), - new Radio(value: StockMode.pessimistic, groupValue: config.stockMode, onChanged: _handleStockModeChange) + new Radio(value: StockMode.pessimistic, groupValue: config.stockMode, onChanged: _handleStockModeChange) ]) ), new DrawerDivider(), diff --git a/examples/widgets/card_collection.dart b/examples/widgets/card_collection.dart index b3a5f9b9033..b212e5c763b 100644 --- a/examples/widgets/card_collection.dart +++ b/examples/widgets/card_collection.dart @@ -124,14 +124,14 @@ class CardCollectionState extends State { buildDrawerCheckbox("Fixed size cards", _fixedSizeCards, _toggleFixedSizeCards), buildDrawerCheckbox("Let the sun shine", _sunshine, _toggleSunshine), new DrawerDivider(), - buildDrawerRadioItem("Deep Purple", Colors.deepPurple, _primaryColor, _selectColor), - buildDrawerRadioItem("Green", Colors.green, _primaryColor, _selectColor), - buildDrawerRadioItem("Amber", Colors.amber, _primaryColor, _selectColor), - buildDrawerRadioItem("Teal", Colors.teal, _primaryColor, _selectColor), + buildDrawerColorRadioItem("Deep Purple", Colors.deepPurple, _primaryColor, _selectColor), + buildDrawerColorRadioItem("Green", Colors.green, _primaryColor, _selectColor), + buildDrawerColorRadioItem("Amber", Colors.amber, _primaryColor, _selectColor), + buildDrawerColorRadioItem("Teal", Colors.teal, _primaryColor, _selectColor), new DrawerDivider(), - buildDrawerRadioItem("Dismiss horizontally", DismissDirection.horizontal, _dismissDirection, _changeDismissDirection, icon: 'action/code'), - buildDrawerRadioItem("Dismiss left", DismissDirection.left, _dismissDirection, _changeDismissDirection, icon: 'navigation/arrow_back'), - buildDrawerRadioItem("Dismiss right", DismissDirection.right, _dismissDirection, _changeDismissDirection, icon: 'navigation/arrow_forward'), + buildDrawerDirectionRadioItem("Dismiss horizontally", DismissDirection.horizontal, _dismissDirection, _changeDismissDirection, icon: 'action/code'), + buildDrawerDirectionRadioItem("Dismiss left", DismissDirection.left, _dismissDirection, _changeDismissDirection, icon: 'navigation/arrow_back'), + buildDrawerDirectionRadioItem("Dismiss right", DismissDirection.right, _dismissDirection, _changeDismissDirection, icon: 'navigation/arrow_forward'), ]) ) ); @@ -161,7 +161,7 @@ class CardCollectionState extends State { }); } - void _selectColor(selection) { + void _selectColor(Map selection) { setState(() { _primaryColor = selection; }); @@ -184,13 +184,28 @@ class CardCollectionState extends State { ); } - Widget buildDrawerRadioItem(String label, itemValue, currentValue, RadioValueChanged onChanged, { String icon }) { + Widget buildDrawerColorRadioItem(String label, Map itemValue, Map currentValue, ValueChanged> onChanged, { String icon }) { return new DrawerItem( icon: icon, onPressed: () { onChanged(itemValue); }, child: new Row([ new Flexible(child: new Text(label)), - new Radio( + new Radio>( + value: itemValue, + groupValue: currentValue, + onChanged: onChanged + ) + ]) + ); + } + + Widget buildDrawerDirectionRadioItem(String label, DismissDirection itemValue, DismissDirection currentValue, ValueChanged onChanged, { String icon }) { + return new DrawerItem( + icon: icon, + onPressed: () { onChanged(itemValue); }, + child: new Row([ + new Flexible(child: new Text(label)), + new Radio( value: itemValue, groupValue: currentValue, onChanged: onChanged diff --git a/sky/packages/sky/lib/src/material/checkbox.dart b/sky/packages/sky/lib/src/material/checkbox.dart index 6b58d93e6eb..91d7eefadf0 100644 --- a/sky/packages/sky/lib/src/material/checkbox.dart +++ b/sky/packages/sky/lib/src/material/checkbox.dart @@ -9,8 +9,6 @@ import 'package:flutter/widgets.dart'; import 'theme.dart'; -export 'package:flutter/rendering.dart' show ValueChanged; - const double _kMidpoint = 0.5; const Color _kLightUncheckedColor = const Color(0x8A000000); const Color _kDarkUncheckedColor = const Color(0xB2FFFFFF); diff --git a/sky/packages/sky/lib/src/material/date_picker.dart b/sky/packages/sky/lib/src/material/date_picker.dart index fd5cbe453af..abea0ba77fc 100644 --- a/sky/packages/sky/lib/src/material/date_picker.dart +++ b/sky/packages/sky/lib/src/material/date_picker.dart @@ -4,18 +4,16 @@ import 'dart:async'; -import 'package:intl/date_symbols.dart'; -import 'package:intl/intl.dart'; import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; +import 'package:intl/date_symbols.dart'; +import 'package:intl/intl.dart'; import 'colors.dart'; import 'ink_well.dart'; import 'theme.dart'; import 'typography.dart'; -typedef void DatePickerValueChanged(DateTime dateTime); - enum DatePickerMode { day, year } typedef void DatePickerModeChanged(DatePickerMode value); @@ -33,7 +31,7 @@ class DatePicker extends StatefulComponent { } final DateTime selectedDate; - final DatePickerValueChanged onChanged; + final ValueChanged onChanged; final DateTime firstDate; final DateTime lastDate; @@ -176,7 +174,7 @@ class DayPicker extends StatelessComponent { final DateTime selectedDate; final DateTime currentDate; - final DatePickerValueChanged onChanged; + final ValueChanged onChanged; final DateTime displayedMonth; Widget build(BuildContext context) { @@ -282,7 +280,7 @@ class MonthPicker extends ScrollableWidgetList { } final DateTime selectedDate; - final DatePickerValueChanged onChanged; + final ValueChanged onChanged; final DateTime firstDate; final DateTime lastDate; @@ -355,7 +353,7 @@ class YearPicker extends ScrollableWidgetList { } final DateTime selectedDate; - final DatePickerValueChanged onChanged; + final ValueChanged onChanged; final DateTime firstDate; final DateTime lastDate; diff --git a/sky/packages/sky/lib/src/material/input.dart b/sky/packages/sky/lib/src/material/input.dart index 0b79987dda3..ba327d0bef2 100644 --- a/sky/packages/sky/lib/src/material/input.dart +++ b/sky/packages/sky/lib/src/material/input.dart @@ -3,16 +3,15 @@ // found in the LICENSE file. import 'package:flutter/animation.dart'; +import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; import 'theme.dart'; +export 'package:flutter/rendering.dart' show ValueChanged; export 'package:flutter/services.dart' show KeyboardType; -typedef void StringValueChanged(String value); -typedef void StringValueSubmitted(String value); - // TODO(eseidel): This isn't right, it's 16px on the bottom: // http://www.google.com/design/spec/components/text-fields.html#text-fields-single-line-text-field const EdgeDims _kTextfieldPadding = const EdgeDims.symmetric(vertical: 8.0); @@ -34,8 +33,8 @@ class Input extends Scrollable { final String initialValue; final KeyboardType keyboardType; final String placeholder; - final StringValueChanged onChanged; - final StringValueSubmitted onSubmitted; + final ValueChanged onChanged; + final ValueChanged onSubmitted; InputState createState() => new InputState(); } diff --git a/sky/packages/sky/lib/src/material/radio.dart b/sky/packages/sky/lib/src/material/radio.dart index a8c33aabf3c..b8442933cc7 100644 --- a/sky/packages/sky/lib/src/material/radio.dart +++ b/sky/packages/sky/lib/src/material/radio.dart @@ -11,9 +11,7 @@ import 'theme.dart'; const Color _kLightOffColor = const Color(0x8A000000); const Color _kDarkOffColor = const Color(0xB2FFFFFF); -typedef void RadioValueChanged(Object value); - -class Radio extends StatelessComponent { +class Radio extends StatelessComponent { Radio({ Key key, this.value, @@ -23,9 +21,9 @@ class Radio extends StatelessComponent { assert(onChanged != null); } - final Object value; - final Object groupValue; - final RadioValueChanged onChanged; + final T value; + final T groupValue; + final ValueChanged onChanged; Color _getColor(BuildContext context) { ThemeData themeData = Theme.of(context); @@ -59,6 +57,7 @@ class Radio extends StatelessComponent { paint.style = ui.PaintingStyle.fill; canvas.drawCircle(const Point(kOuterRadius, kOuterRadius), kInnerRadius, paint); } + } ) ) diff --git a/sky/packages/sky/lib/src/material/switch.dart b/sky/packages/sky/lib/src/material/switch.dart index 6b25853b29d..d5872475e48 100644 --- a/sky/packages/sky/lib/src/material/switch.dart +++ b/sky/packages/sky/lib/src/material/switch.dart @@ -13,8 +13,6 @@ import 'radial_reaction.dart'; import 'shadows.dart'; import 'theme.dart'; -export 'package:flutter/rendering.dart' show ValueChanged; - const Color _kThumbOffColor = const Color(0xFFFAFAFA); const Color _kTrackOffColor = const Color(0x42000000); const double _kSwitchWidth = 35.0;