radio.dart 17.3 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/widgets.dart';
6

7
import 'constants.dart';
8
import 'debug.dart';
9
import 'material_state.dart';
10
import 'theme.dart';
11
import 'theme_data.dart';
12
import 'toggleable.dart';
13

14 15
const double _kOuterRadius = 8.0;
const double _kInnerRadius = 4.5;
16

17 18
/// A material design radio button.
///
19 20 21 22
/// Used to select between a number of mutually exclusive values. When one radio
/// button in a group is selected, the other radio buttons in the group cease to
/// be selected. The values are of type `T`, the type parameter of the [Radio]
/// class. Enums are commonly used for this purpose.
23
///
24 25 26 27 28 29
/// The radio button itself does not maintain any state. Instead, selecting the
/// radio invokes the [onChanged] callback, passing [value] as a parameter. If
/// [groupValue] and [value] match, this radio will be selected. Most widgets
/// will respond to [onChanged] by calling [State.setState] to update the
/// radio button's [groupValue].
///
30
/// {@tool dartpad --template=stateful_widget_scaffold_center}
31 32 33 34 35 36 37 38 39 40 41 42 43
/// Here is an example of Radio widgets wrapped in ListTiles, which is similar
/// to what you could get with the RadioListTile widget.
///
/// The currently selected character is passed into `groupValue`, which is
/// maintained by the example's `State`. In this case, the first `Radio`
/// will start off selected because `_character` is initialized to
/// `SingingCharacter.lafayette`.
///
/// If the second radio button is pressed, the example's state is updated
/// with `setState`, updating `_character` to `SingingCharacter.jefferson`.
/// This causes the buttons to rebuild with the updated `groupValue`, and
/// therefore the selection of the second button.
///
44 45
/// Requires one of its ancestors to be a [Material] widget.
///
46
/// ** See code in examples/api/lib/material/radio/radio.0.dart **
47
/// {@end-tool}
48 49
///
/// See also:
50
///
51 52
///  * [RadioListTile], which combines this widget with a [ListTile] so that
///    you can give the radio button a label.
53 54
///  * [Slider], for selecting a value in a range.
///  * [Checkbox] and [Switch], for toggling a particular value on or off.
55
///  * <https://material.io/design/components/selection-controls.html#radio-buttons>
56
class Radio<T> extends StatefulWidget {
57 58
  /// Creates a material design radio button.
  ///
59 60 61 62 63
  /// The radio button itself does not maintain any state. Instead, when the
  /// radio button is selected, the widget calls the [onChanged] callback. Most
  /// widgets that use a radio button will listen for the [onChanged] callback
  /// and rebuild the radio button with a new [groupValue] to update the visual
  /// appearance of the radio button.
64
  ///
65 66 67 68 69
  /// The following arguments are required:
  ///
  /// * [value] and [groupValue] together determine whether the radio button is
  ///   selected.
  /// * [onChanged] is called when the user selects this radio button.
70
  const Radio({
71 72 73 74
    Key? key,
    required this.value,
    required this.groupValue,
    required this.onChanged,
75
    this.mouseCursor,
76
    this.toggleable = false,
77
    this.activeColor,
78
    this.fillColor,
79 80
    this.focusColor,
    this.hoverColor,
81
    this.overlayColor,
82
    this.splashRadius,
83
    this.materialTapTargetSize,
84
    this.visualDensity,
85 86 87
    this.focusNode,
    this.autofocus = false,
  }) : assert(autofocus != null),
88
       assert(toggleable != null),
89
       super(key: key);
90

91
  /// The value represented by this radio button.
Hixie's avatar
Hixie committed
92
  final T value;
93

94
  /// The currently selected value for a group of radio buttons.
95 96 97
  ///
  /// This radio button is considered selected if its [value] matches the
  /// [groupValue].
98
  final T? groupValue;
99 100 101

  /// Called when the user selects this radio button.
  ///
102 103 104 105 106
  /// The radio button passes [value] as a parameter to this callback. The radio
  /// button does not actually change state until the parent widget rebuilds the
  /// radio button with the new [groupValue].
  ///
  /// If null, the radio button will be displayed as disabled.
107
  ///
108 109 110
  /// The provided callback will not be invoked if this radio button is already
  /// selected.
  ///
111
  /// The callback provided to [onChanged] should update the state of the parent
112 113 114 115
  /// [StatefulWidget] using the [State.setState] method, so that the parent
  /// gets rebuilt; for example:
  ///
  /// ```dart
116
  /// Radio<SingingCharacter>(
117 118 119 120 121 122 123
  ///   value: SingingCharacter.lafayette,
  ///   groupValue: _character,
  ///   onChanged: (SingingCharacter newValue) {
  ///     setState(() {
  ///       _character = newValue;
  ///     });
  ///   },
124
  /// )
125
  /// ```
126
  final ValueChanged<T?>? onChanged;
127

128
  /// {@template flutter.material.radio.mouseCursor}
129 130 131 132 133 134 135 136 137 138
  /// The cursor for a mouse pointer when it enters or is hovering over the
  /// widget.
  ///
  /// If [mouseCursor] is a [MaterialStateProperty<MouseCursor>],
  /// [MaterialStateProperty.resolve] is used for the following [MaterialState]s:
  ///
  ///  * [MaterialState.selected].
  ///  * [MaterialState.hovered].
  ///  * [MaterialState.focused].
  ///  * [MaterialState.disabled].
139
  /// {@endtemplate}
140
  ///
141 142 143 144 145 146 147 148
  /// If null, then the value of [RadioThemeData.mouseCursor] is used.
  /// If that is also null, then [MaterialStateMouseCursor.clickable] is used.
  ///
  /// See also:
  ///
  ///  * [MaterialStateMouseCursor], a [MouseCursor] that implements
  ///    `MaterialStateProperty` which is used in APIs that need to accept
  ///    either a [MouseCursor] or a [MaterialStateProperty<MouseCursor>].
149
  final MouseCursor? mouseCursor;
150

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
  /// Set to true if this radio button is allowed to be returned to an
  /// indeterminate state by selecting it again when selected.
  ///
  /// To indicate returning to an indeterminate state, [onChanged] will be
  /// called with null.
  ///
  /// If true, [onChanged] can be called with [value] when selected while
  /// [groupValue] != [value], or with null when selected again while
  /// [groupValue] == [value].
  ///
  /// If false, [onChanged] will be called with [value] when it is selected
  /// while [groupValue] != [value], and only by selecting another radio button
  /// in the group (i.e. changing the value of [groupValue]) can this radio
  /// button be unselected.
  ///
  /// The default is false.
  ///
168
  /// {@tool dartpad --template=stateful_widget_scaffold}
169 170 171
  /// This example shows how to enable deselecting a radio button by setting the
  /// [toggleable] attribute.
  ///
172
  /// ** See code in examples/api/lib/material/radio/radio.toggleable.0.dart **
173 174 175
  /// {@end-tool}
  final bool toggleable;

176 177
  /// The color to use when this radio button is selected.
  ///
178
  /// Defaults to [ThemeData.toggleableActiveColor].
179 180 181
  ///
  /// If [fillColor] returns a non-null color in the [MaterialState.selected]
  /// state, it will be used instead of this color.
182
  final Color? activeColor;
183

184 185
  /// {@template flutter.material.radio.fillColor}
  /// The color that fills the radio button, in all [MaterialState]s.
186 187 188 189 190 191
  ///
  /// Resolves in the following states:
  ///  * [MaterialState.selected].
  ///  * [MaterialState.hovered].
  ///  * [MaterialState.focused].
  ///  * [MaterialState.disabled].
192 193 194 195 196 197 198 199
  /// {@endtemplate}
  ///
  /// If null, then the value of [activeColor] is used in the selected state. If
  /// that is also null, then the value of [RadioThemeData.fillColor] is used.
  /// If that is also null, then [ThemeData.disabledColor] is used in
  /// the disabled state, [ThemeData.toggleableActiveColor] is used in the
  /// selected state, and [ThemeData.unselectedWidgetColor] is used in the
  /// default state.
200 201
  final MaterialStateProperty<Color?>? fillColor;

202
  /// {@template flutter.material.radio.materialTapTargetSize}
203
  /// Configures the minimum size of the tap target.
204
  /// {@endtemplate}
205
  ///
206 207 208
  /// If null, then the value of [RadioThemeData.materialTapTargetSize] is used.
  /// If that is also null, then the value of [ThemeData.materialTapTargetSize]
  /// is used.
209 210 211
  ///
  /// See also:
  ///
212
  ///  * [MaterialTapTargetSize], for a description of how this affects tap targets.
213
  final MaterialTapTargetSize? materialTapTargetSize;
214

215
  /// {@template flutter.material.radio.visualDensity}
216
  /// Defines how compact the radio's layout will be.
217
  /// {@endtemplate}
218 219 220
  ///
  /// {@macro flutter.material.themedata.visualDensity}
  ///
221 222 223
  /// If null, then the value of [RadioThemeData.visualDensity] is used. If that
  /// is also null, then the value of [ThemeData.visualDensity] is used.
  ///
224 225
  /// See also:
  ///
226 227
  ///  * [ThemeData.visualDensity], which specifies the [visualDensity] for all
  ///    widgets within a [Theme].
228
  final VisualDensity? visualDensity;
229

230
  /// The color for the radio's [Material] when it has the input focus.
231
  ///
232 233 234
  /// If [overlayColor] returns a non-null color in the [MaterialState.focused]
  /// state, it will be used instead.
  ///
235 236 237
  /// If null, then the value of [RadioThemeData.overlayColor] is used in the
  /// focused state. If that is also null, then the value of
  /// [ThemeData.focusColor] is used.
238
  final Color? focusColor;
239 240

  /// The color for the radio's [Material] when a pointer is hovering over it.
241
  ///
242 243 244
  /// If [overlayColor] returns a non-null color in the [MaterialState.hovered]
  /// state, it will be used instead.
  ///
245 246 247
  /// If null, then the value of [RadioThemeData.overlayColor] is used in the
  /// hovered state. If that is also null, then the value of
  /// [ThemeData.hoverColor] is used.
248
  final Color? hoverColor;
249

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
  /// {@template flutter.material.radio.overlayColor}
  /// The color for the checkbox's [Material].
  ///
  /// Resolves in the following states:
  ///  * [MaterialState.pressed].
  ///  * [MaterialState.selected].
  ///  * [MaterialState.hovered].
  ///  * [MaterialState.focused].
  /// {@endtemplate}
  ///
  /// If null, then the value of [activeColor] with alpha
  /// [kRadialReactionAlpha], [focusColor] and [hoverColor] is used in the
  /// pressed, focused and hovered state. If that is also null,
  /// the value of [RadioThemeData.overlayColor] is used. If that is also null,
  /// then the value of [ThemeData.toggleableActiveColor] with alpha
  /// [kRadialReactionAlpha], [ThemeData.focusColor] and [ThemeData.hoverColor]
  /// is used in the pressed, focused and hovered state.
  final MaterialStateProperty<Color?>? overlayColor;

269
  /// {@template flutter.material.radio.splashRadius}
270
  /// The splash radius of the circular [Material] ink response.
271
  /// {@endtemplate}
272
  ///
273 274
  /// If null, then the value of [RadioThemeData.splashRadius] is used. If that
  /// is also null, then [kRadialReactionRadius] is used.
275 276
  final double? splashRadius;

277
  /// {@macro flutter.widgets.Focus.focusNode}
278
  final FocusNode? focusNode;
279 280 281 282

  /// {@macro flutter.widgets.Focus.autofocus}
  final bool autofocus;

283 284
  bool get _selected => value == groupValue;

285
  @override
286
  State<Radio<T>> createState() => _RadioState<T>();
287 288
}

289 290
class _RadioState<T> extends State<Radio<T>> with TickerProviderStateMixin, ToggleableStateMixin {
  final _RadioPainter _painter = _RadioPainter();
291

292 293 294 295 296 297 298 299 300
  void _handleChanged(bool? selected) {
    if (selected == null) {
      widget.onChanged!(null);
      return;
    }
    if (selected) {
      widget.onChanged!(widget.value);
    }
  }
301

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
  @override
  void didUpdateWidget(Radio<T> oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget._selected != oldWidget._selected) {
      animateToValue();
    }
  }

  @override
  void dispose() {
    _painter.dispose();
    super.dispose();
  }

  @override
  ValueChanged<bool?>? get onChanged => widget.onChanged != null ? _handleChanged : null;

  @override
  bool get tristate => widget.toggleable;
321

322 323
  @override
  bool? get value => widget._selected;
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

  MaterialStateProperty<Color?> get _widgetFillColor {
    return MaterialStateProperty.resolveWith((Set<MaterialState> states) {
      if (states.contains(MaterialState.disabled)) {
        return null;
      }
      if (states.contains(MaterialState.selected)) {
        return widget.activeColor;
      }
      return null;
    });
  }

  MaterialStateProperty<Color> get _defaultFillColor {
    final ThemeData themeData = Theme.of(context);
    return MaterialStateProperty.resolveWith((Set<MaterialState> states) {
      if (states.contains(MaterialState.disabled)) {
        return themeData.disabledColor;
      }
      if (states.contains(MaterialState.selected)) {
        return themeData.toggleableActiveColor;
      }
      return themeData.unselectedWidgetColor;
    });
  }

350
  @override
351
  Widget build(BuildContext context) {
352
    assert(debugCheckHasMaterial(context));
353
    final ThemeData themeData = Theme.of(context);
354 355 356 357 358 359
    final MaterialTapTargetSize effectiveMaterialTapTargetSize = widget.materialTapTargetSize
      ?? themeData.radioTheme.materialTapTargetSize
      ?? themeData.materialTapTargetSize;
    final VisualDensity effectiveVisualDensity = widget.visualDensity
      ?? themeData.radioTheme.visualDensity
      ?? themeData.visualDensity;
360
    Size size;
361
    switch (effectiveMaterialTapTargetSize) {
362
      case MaterialTapTargetSize.padded:
363
        size = const Size(kMinInteractiveDimension, kMinInteractiveDimension);
364 365
        break;
      case MaterialTapTargetSize.shrinkWrap:
366
        size = const Size(kMinInteractiveDimension - 8.0, kMinInteractiveDimension - 8.0);
367 368
        break;
    }
369
    size += effectiveVisualDensity.baseSizeAdjustment;
370 371 372 373 374 375

    final MaterialStateProperty<MouseCursor> effectiveMouseCursor = MaterialStateProperty.resolveWith<MouseCursor>((Set<MaterialState> states) {
      return MaterialStateProperty.resolveAs<MouseCursor?>(widget.mouseCursor, states)
        ?? themeData.radioTheme.mouseCursor?.resolve(states)
        ?? MaterialStateProperty.resolveAs<MouseCursor>(MaterialStateMouseCursor.clickable, states);
    });
376

377 378
    // Colors need to be resolved in selected and non selected states separately
    // so that they can be lerped between.
379 380
    final Set<MaterialState> activeStates = states..add(MaterialState.selected);
    final Set<MaterialState> inactiveStates = states..remove(MaterialState.selected);
381 382
    final Color effectiveActiveColor = widget.fillColor?.resolve(activeStates)
      ?? _widgetFillColor.resolve(activeStates)
383
      ?? themeData.radioTheme.fillColor?.resolve(activeStates)
384 385 386
      ?? _defaultFillColor.resolve(activeStates);
    final Color effectiveInactiveColor = widget.fillColor?.resolve(inactiveStates)
      ?? _widgetFillColor.resolve(inactiveStates)
387
      ?? themeData.radioTheme.fillColor?.resolve(inactiveStates)
388
      ?? _defaultFillColor.resolve(inactiveStates);
389

390
    final Set<MaterialState> focusedStates = states..add(MaterialState.focused);
391 392 393 394 395
    final Color effectiveFocusOverlayColor = widget.overlayColor?.resolve(focusedStates)
      ?? widget.focusColor
      ?? themeData.radioTheme.overlayColor?.resolve(focusedStates)
      ?? themeData.focusColor;

396
    final Set<MaterialState> hoveredStates = states..add(MaterialState.hovered);
397 398 399
    final Color effectiveHoverOverlayColor = widget.overlayColor?.resolve(hoveredStates)
        ?? widget.hoverColor
        ?? themeData.radioTheme.overlayColor?.resolve(hoveredStates)
400 401
        ?? themeData.hoverColor;

402 403 404 405 406 407 408 409 410 411
    final Set<MaterialState> activePressedStates = activeStates..add(MaterialState.pressed);
    final Color effectiveActivePressedOverlayColor = widget.overlayColor?.resolve(activePressedStates)
        ?? themeData.radioTheme.overlayColor?.resolve(activePressedStates)
        ?? effectiveActiveColor.withAlpha(kRadialReactionAlpha);

    final Set<MaterialState> inactivePressedStates = inactiveStates..add(MaterialState.pressed);
    final Color effectiveInactivePressedOverlayColor = widget.overlayColor?.resolve(inactivePressedStates)
        ?? themeData.radioTheme.overlayColor?.resolve(inactivePressedStates)
        ?? effectiveActiveColor.withAlpha(kRadialReactionAlpha);

412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
    return Semantics(
      inMutuallyExclusiveGroup: true,
      checked: widget._selected,
      child: buildToggleable(
        focusNode: widget.focusNode,
        autofocus: widget.autofocus,
        mouseCursor: effectiveMouseCursor,
        size: size,
        painter: _painter
          ..position = position
          ..reaction = reaction
          ..reactionFocusFade = reactionFocusFade
          ..reactionHoverFade = reactionHoverFade
          ..inactiveReactionColor = effectiveInactivePressedOverlayColor
          ..reactionColor = effectiveActivePressedOverlayColor
          ..hoverColor = effectiveHoverOverlayColor
          ..focusColor = effectiveFocusOverlayColor
          ..splashRadius = widget.splashRadius ?? themeData.radioTheme.splashRadius ?? kRadialReactionRadius
          ..downPosition = downPosition
          ..isFocused = states.contains(MaterialState.focused)
          ..isHovered = states.contains(MaterialState.hovered)
          ..activeColor = effectiveActiveColor
434
          ..inactiveColor = effectiveInactiveColor,
435
      ),
436 437 438
    );
  }
}
439

440
class _RadioPainter extends ToggleablePainter {
441
  @override
442 443
  void paint(Canvas canvas, Size size) {
    paintRadialReaction(canvas: canvas, origin: size.center(Offset.zero));
444

445
    final Offset center = (Offset.zero & size).center;
446 447

    // Outer circle
448
    final Paint paint = Paint()
449
      ..color = Color.lerp(inactiveColor, activeColor, position.value)!
450
      ..style = PaintingStyle.stroke
451 452 453 454 455
      ..strokeWidth = 2.0;
    canvas.drawCircle(center, _kOuterRadius, paint);

    // Inner circle
    if (!position.isDismissed) {
456
      paint.style = PaintingStyle.fill;
457 458 459 460
      canvas.drawCircle(center, _kInnerRadius * position.value, paint);
    }
  }
}