Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CupertinoRadio control and Radio.adaptive #2225

Merged
merged 4 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 353c8bcc5d5b0994e508d035b5431cfe18c1dea7

COCOAPODS: 1.12.1
COCOAPODS: 1.14.2
8 changes: 8 additions & 0 deletions package/lib/src/controls/create_control.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import 'popup_menu_button.dart';
import 'progress_bar.dart';
import 'progress_ring.dart';
import 'radio.dart';
import 'cupertino_radio.dart';
import 'radio_group.dart';
import 'range_slider.dart';
import 'responsive_row.dart';
Expand Down Expand Up @@ -532,6 +533,13 @@ Widget createWidget(Key? key, ControlViewModel controlView, Control? parent,
control: controlView.control,
parentDisabled: parentDisabled,
dispatch: controlView.dispatch);
case "cupertinoradio":
return CupertinoRadioControl(
key: key,
parent: parent,
control: controlView.control,
parentDisabled: parentDisabled,
dispatch: controlView.dispatch);
case "dropdown":
return DropdownControl(
key: key,
Expand Down
154 changes: 154 additions & 0 deletions package/lib/src/controls/cupertino_radio.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';

import '../actions.dart';
import '../flet_app_services.dart';
import '../models/app_state.dart';
import '../models/control.dart';
import '../models/control_ancestor_view_model.dart';
import '../protocol/update_control_props_payload.dart';
import '../utils/colors.dart';
import 'create_control.dart';
import 'error.dart';
import 'list_tile.dart';

enum LabelPosition { right, left }

class CupertinoRadioControl extends StatefulWidget {
final Control? parent;
final Control control;
final bool parentDisabled;
final dynamic dispatch;

const CupertinoRadioControl(
{Key? key,
this.parent,
required this.control,
required this.parentDisabled,
required this.dispatch})
: super(key: key);

@override
State<CupertinoRadioControl> createState() => _CupertinoRadioControlState();
}

class _CupertinoRadioControlState extends State<CupertinoRadioControl> {
late final FocusNode _focusNode;

@override
void initState() {
super.initState();
_focusNode = FocusNode();
_focusNode.addListener(_onFocusChange);
}

void _onFocusChange() {
FletAppServices.of(context).server.sendPageEvent(
eventTarget: widget.control.id,
eventName: _focusNode.hasFocus ? "focus" : "blur",
eventData: "");
}

@override
void dispose() {
_focusNode.removeListener(_onFocusChange);
_focusNode.dispose();
super.dispose();
}

void _onChange(String ancestorId, String? value) {
var svalue = value ?? "";
debugPrint(svalue);
List<Map<String, String>> props = [
{"i": ancestorId, "value": svalue}
];
widget.dispatch(
UpdateControlPropsAction(UpdateControlPropsPayload(props: props)));

final server = FletAppServices.of(context).server;
server.updateControlProps(props: props);
server.sendPageEvent(
eventTarget: ancestorId, eventName: "change", eventData: svalue);
}

@override
Widget build(BuildContext context) {
debugPrint("CupertinoRadio build: ${widget.control.id}");

String label = widget.control.attrString("label", "")!;
String value = widget.control.attrString("value", "")!;
LabelPosition labelPosition = LabelPosition.values.firstWhere(
(p) =>
p.name.toLowerCase() ==
widget.control.attrString("labelPosition", "")!.toLowerCase(),
orElse: () => LabelPosition.right);
bool autofocus = widget.control.attrBool("autofocus", false)!;
bool disabled = widget.control.isDisabled || widget.parentDisabled;

return StoreConnector<AppState, ControlAncestorViewModel>(
distinct: true,
ignoreChange: (state) {
return state.controls[widget.control.id] == null;
},
converter: (store) => ControlAncestorViewModel.fromStore(
store, widget.control.id, "radiogroup"),
builder: (context, viewModel) {
debugPrint(
"CupertinoRadio StoreConnector build: ${widget.control.id}");

if (viewModel.ancestor == null) {
return const ErrorControl(
"CupertinoRadio control must be enclosed with RadioGroup.");
}

String groupValue = viewModel.ancestor!.attrString("value", "")!;
String ancestorId = viewModel.ancestor!.id;

var cupertinoRadio = CupertinoRadio<String>(
autofocus: autofocus,
focusNode: _focusNode,
groupValue: groupValue,
value: value,
useCheckmarkStyle:
widget.control.attrBool("useCheckmarkStyle", false)!,
fillColor: HexColor.fromString(Theme.of(context),
widget.control.attrString("fillColor", "")!),
activeColor: HexColor.fromString(Theme.of(context),
widget.control.attrString("activeColor", "")!),
inactiveColor: HexColor.fromString(Theme.of(context),
widget.control.attrString("inactiveColor", "")!),
onChanged: !disabled
? (String? value) {
_onChange(ancestorId, value);
}
: null);

ListTileClicks.of(context)?.notifier.addListener(() {
_onChange(ancestorId, value);
});

Widget result = cupertinoRadio;
if (label != "") {
var labelWidget = disabled
? Text(label,
style: TextStyle(color: Theme.of(context).disabledColor))
: MouseRegion(
cursor: SystemMouseCursors.click, child: Text(label));
result = MergeSemantics(
child: GestureDetector(
onTap: !disabled
? () {
_onChange(ancestorId, value);
}
: null,
child: labelPosition == LabelPosition.right
? Row(children: [cupertinoRadio, labelWidget])
: Row(children: [labelWidget, cupertinoRadio])));
}

return constrainedControl(
context, result, widget.parent, widget.control);
});
}
}
15 changes: 15 additions & 0 deletions package/lib/src/controls/radio.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';

Expand All @@ -8,7 +9,9 @@ import '../models/control.dart';
import '../models/control_ancestor_view_model.dart';
import '../protocol/update_control_props_payload.dart';
import '../utils/buttons.dart';
import '../utils/colors.dart';
import 'create_control.dart';
import 'cupertino_radio.dart';
import 'error.dart';
import 'list_tile.dart';

Expand Down Expand Up @@ -75,6 +78,16 @@ class _RadioControlState extends State<RadioControl> {
Widget build(BuildContext context) {
debugPrint("Radio build: ${widget.control.id}");

bool adaptive = widget.control.attrBool("adaptive", false)!;
if (adaptive &&
(defaultTargetPlatform == TargetPlatform.iOS ||
defaultTargetPlatform == TargetPlatform.macOS)) {
return CupertinoRadioControl(
control: widget.control,
parentDisabled: widget.parentDisabled,
dispatch: widget.dispatch);
}

String label = widget.control.attrString("label", "")!;
String value = widget.control.attrString("value", "")!;
LabelPosition labelPosition = LabelPosition.values.firstWhere(
Expand Down Expand Up @@ -108,6 +121,8 @@ class _RadioControlState extends State<RadioControl> {
focusNode: _focusNode,
groupValue: groupValue,
value: value,
activeColor: HexColor.fromString(Theme.of(context),
widget.control.attrString("activeColor", "")!),
fillColor: parseMaterialStateColor(
Theme.of(context), widget.control, "fillColor"),
onChanged: !disabled
Expand Down
1 change: 1 addition & 0 deletions sdk/python/packages/flet-core/src/flet_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,5 @@
from flet_core.badge import Badge
from flet_core.navigation_drawer import NavigationDrawer, NavigationDrawerDestination
from flet_core.selection_area import SelectionArea
from flet_core.cupertino_radio import CupertinoRadio
from flet_core.cupertino_checkbox import CupertinoCheckbox
Loading