From 8238a76e4225ae5e2cc2213690808372fafc419d Mon Sep 17 00:00:00 2001 From: Chase Naples Date: Thu, 18 Sep 2025 20:17:44 -0400 Subject: [PATCH] docs(diagrams): migrate radio_list_tile example to RadioGroup (flutter/flutter#175355) --- .../diagrams/lib/src/radio_list_tile.dart | 164 +++++++----------- 1 file changed, 66 insertions(+), 98 deletions(-) diff --git a/packages/diagrams/lib/src/radio_list_tile.dart b/packages/diagrams/lib/src/radio_list_tile.dart index e1ea2426..166b0460 100644 --- a/packages/diagrams/lib/src/radio_list_tile.dart +++ b/packages/diagrams/lib/src/radio_list_tile.dart @@ -15,17 +15,14 @@ class LinkedLabelRadio extends StatelessWidget { const LinkedLabelRadio({ required this.label, required this.padding, - required this.groupValue, required this.value, - required this.onChanged, super.key, }); final String label; final EdgeInsets padding; - final bool groupValue; final bool value; - final ValueChanged onChanged; + @override Widget build(BuildContext context) { @@ -33,13 +30,7 @@ class LinkedLabelRadio extends StatelessWidget { padding: padding, child: Row( children: [ - Radio( - groupValue: groupValue, - value: value, - onChanged: (bool? newValue) { - onChanged(newValue); - }, - ), + Radio(value: value), RichText( text: TextSpan( text: label, @@ -63,37 +54,26 @@ class LabeledRadio extends StatelessWidget { const LabeledRadio({ required this.label, required this.padding, - required this.groupValue, required this.value, - required this.onChanged, super.key, }); final String label; final EdgeInsets padding; - final bool groupValue; final bool value; - final ValueChanged onChanged; + @override Widget build(BuildContext context) { return InkWell( onTap: () { - if (value != groupValue) { - onChanged(value); - } + RadioGroup.maybeOf(context)?.onChanged(value); }, child: Padding( padding: padding, child: Row( children: [ - Radio( - groupValue: groupValue, - value: value, - onChanged: (bool? newValue) { - onChanged(newValue); - }, - ), + Radio(value: value), Text(label), ], ), @@ -127,29 +107,25 @@ class _RadioListTileDiagramState extends State { alignment: FractionalOffset.center, padding: const EdgeInsets.all(5.0), color: Colors.white, - child: Column( - children: [ - RadioListTile( - title: const Text('Lafayette'), - value: SingingCharacter.lafayette, - groupValue: _character, - onChanged: (SingingCharacter? value) { - setState(() { - _character = value; - }); - }, - ), - RadioListTile( - title: const Text('Thomas Jefferson'), - value: SingingCharacter.jefferson, - groupValue: _character, - onChanged: (SingingCharacter? value) { - setState(() { - _character = value; - }); - }, - ), - ], + child: RadioGroup( + groupValue: _character, + onChanged: (SingingCharacter? value) { + setState(() { + _character = value; + }); + }, + child: const Column( + children: [ + RadioListTile( + title: Text('Lafayette'), + value: SingingCharacter.lafayette, + ), + RadioListTile( + title: Text('Thomas Jefferson'), + value: SingingCharacter.jefferson, + ), + ], + ), ), ), ); @@ -161,31 +137,27 @@ class _RadioListTileDiagramState extends State { alignment: FractionalOffset.center, padding: const EdgeInsets.all(5.0), color: Colors.white, - child: Column( - children: [ - LinkedLabelRadio( - label: 'First tappable label text', - padding: const EdgeInsets.symmetric(horizontal: 5.0), - value: true, - groupValue: _isRadioSelected, - onChanged: (bool? newValue) { - setState(() { - _isRadioSelected = newValue!; - }); - }, - ), - LinkedLabelRadio( - label: 'Second tappable label text', - padding: const EdgeInsets.symmetric(horizontal: 5.0), - value: false, - groupValue: _isRadioSelected, - onChanged: (bool? newValue) { - setState(() { - _isRadioSelected = newValue!; - }); - }, - ), - ], + child: RadioGroup( + groupValue: _isRadioSelected, + onChanged: (bool? newValue) { + setState(() { + _isRadioSelected = newValue!; + }); + }, + child: const Column( + children: [ + LinkedLabelRadio( + label: 'First tappable label text', + padding: EdgeInsets.symmetric(horizontal: 5.0), + value: true, + ), + LinkedLabelRadio( + label: 'Second tappable label text', + padding: EdgeInsets.symmetric(horizontal: 5.0), + value: false, + ), + ], + ), ), ), ); @@ -197,31 +169,27 @@ class _RadioListTileDiagramState extends State { alignment: FractionalOffset.center, padding: const EdgeInsets.all(5.0), color: Colors.white, - child: Column( - children: [ - LabeledRadio( - label: 'This is the first label text', - padding: const EdgeInsets.symmetric(horizontal: 5.0), - value: true, - groupValue: _isRadioSelected, - onChanged: (bool? newValue) { - setState(() { - _isRadioSelected = newValue!; - }); - }, - ), - LabeledRadio( - label: 'This is the second label text', - padding: const EdgeInsets.symmetric(horizontal: 5.0), - value: false, - groupValue: _isRadioSelected, - onChanged: (bool? newValue) { - setState(() { - _isRadioSelected = newValue!; - }); - }, - ), - ], + child: RadioGroup( + groupValue: _isRadioSelected, + onChanged: (bool? newValue) { + setState(() { + _isRadioSelected = newValue!; + }); + }, + child: const Column( + children: [ + LabeledRadio( + label: 'This is the first label text', + padding: EdgeInsets.symmetric(horizontal: 5.0), + value: true, + ), + LabeledRadio( + label: 'This is the second label text', + padding: EdgeInsets.symmetric(horizontal: 5.0), + value: false, + ), + ], + ), ), ), );