Skip to content

Commit 2f7b6c7

Browse files
committed
Handle ListTile click events in RadioControl
Added support for responding to ListTile click events in RadioControl by listening to a notifier from ListTileClicks. This enables radio controls to react to tile clicks, updating the radio group selection if not disabled.
1 parent d3e4236 commit 2f7b6c7

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

packages/flet/lib/src/controls/radio.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import '../utils/text.dart';
99
import '../utils/theme.dart';
1010
import '../widgets/error.dart';
1111
import 'base_controls.dart';
12+
import 'list_tile.dart';
1213

1314
class RadioControl extends StatefulWidget {
1415
final Control control;
@@ -22,6 +23,7 @@ class RadioControl extends StatefulWidget {
2223

2324
class _RadioControlState extends State<RadioControl> {
2425
late final FocusNode _focusNode;
26+
Listenable? _tileClicksNotifier;
2527

2628
@override
2729
void initState() {
@@ -30,13 +32,38 @@ class _RadioControlState extends State<RadioControl> {
3032
_focusNode.addListener(_onFocusChange);
3133
}
3234

35+
@override
36+
void didChangeDependencies() {
37+
super.didChangeDependencies();
38+
final newNotifier = ListTileClicks.of(context)?.notifier;
39+
40+
// If the inherited source changed, swap listeners
41+
if (!identical(_tileClicksNotifier, newNotifier)) {
42+
_tileClicksNotifier?.removeListener(_handleTileClick);
43+
_tileClicksNotifier = newNotifier;
44+
_tileClicksNotifier?.addListener(_handleTileClick);
45+
}
46+
}
47+
3348
void _onFocusChange() {
3449
widget.control.triggerEvent(_focusNode.hasFocus ? "focus" : "blur");
3550
}
3651

52+
void _handleTileClick() {
53+
if (widget.control.disabled) {
54+
return;
55+
}
56+
final radioGroup = RadioGroup.maybeOf<String>(context);
57+
if (radioGroup != null) {
58+
final value = widget.control.getString("value", "")!;
59+
radioGroup.onChanged(value);
60+
}
61+
}
62+
3763
@override
3864
void dispose() {
3965
_focusNode.removeListener(_onFocusChange);
66+
_tileClicksNotifier?.removeListener(_handleTileClick);
4067
_focusNode.dispose();
4168
super.dispose();
4269
}

0 commit comments

Comments
 (0)