Skip to content
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
21 changes: 20 additions & 1 deletion package/lib/src/controls/checkbox.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:flet/src/controls/cupertino_checkbox.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import '../actions.dart';
Expand Down Expand Up @@ -69,7 +71,6 @@ class _CheckboxControlState extends State<CheckboxControl> {

void _onChange(bool? value) {
var svalue = value != null ? value.toString() : "";
//debugPrint(svalue);
setState(() {
_value = value;
});
Expand All @@ -87,6 +88,15 @@ class _CheckboxControlState extends State<CheckboxControl> {
@override
Widget build(BuildContext context) {
debugPrint("Checkbox build: ${widget.control.id}");
bool adaptive = widget.control.attrBool("adaptive", false)!;
if (adaptive &&
(defaultTargetPlatform == TargetPlatform.iOS ||
defaultTargetPlatform == TargetPlatform.macOS)) {
return CupertinoCheckboxControl(
control: widget.control,
parentDisabled: widget.parentDisabled,
dispatch: widget.dispatch);
}

String label = widget.control.attrString("label", "")!;
LabelPosition labelPosition = LabelPosition.values.firstWhere(
Expand All @@ -96,6 +106,7 @@ class _CheckboxControlState extends State<CheckboxControl> {
orElse: () => LabelPosition.right);
_tristate = widget.control.attrBool("tristate", false)!;
bool autofocus = widget.control.attrBool("autofocus", false)!;

bool disabled = widget.control.isDisabled || widget.parentDisabled;

debugPrint("Checkbox StoreConnector build: ${widget.control.id}");
Expand All @@ -109,6 +120,14 @@ class _CheckboxControlState extends State<CheckboxControl> {
autofocus: autofocus,
focusNode: _focusNode,
value: _value,
activeColor: HexColor.fromString(
Theme.of(context), widget.control.attrString("activeColor", "")!),
focusColor: HexColor.fromString(
Theme.of(context), widget.control.attrString("focusColor", "")!),
hoverColor: HexColor.fromString(
Theme.of(context), widget.control.attrString("hoverColor", "")!),
overlayColor: parseMaterialStateColor(
Theme.of(context), widget.control, "overlayColor"),
checkColor: HexColor.fromString(
Theme.of(context), widget.control.attrString("checkColor", "")!),
fillColor: parseMaterialStateColor(
Expand Down
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 @@ -84,6 +84,7 @@ import 'transparent_pointer.dart';
import 'vertical_divider.dart';
import 'webview.dart';
import 'window_drag_area.dart';
import 'cupertino_checkbox.dart';

Widget createControl(Control? parent, String id, bool parentDisabled,
{Widget? nextChild}) {
Expand Down Expand Up @@ -477,6 +478,13 @@ Widget createWidget(Key? key, ControlViewModel controlView, Control? parent,
control: controlView.control,
parentDisabled: parentDisabled,
dispatch: controlView.dispatch);
case "cupertinocheckbox":
return CupertinoCheckboxControl(
key: key,
parent: parent,
control: controlView.control,
parentDisabled: parentDisabled,
dispatch: controlView.dispatch);
case "switch":
return SwitchControl(
key: key,
Expand Down
146 changes: 146 additions & 0 deletions package/lib/src/controls/cupertino_checkbox.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

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

enum LabelPosition { right, left }

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

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

@override
State<CupertinoCheckboxControl> createState() => _CheckboxControlState();
}

class _CheckboxControlState extends State<CupertinoCheckboxControl> {
bool? _value;
bool _tristate = false;
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 _toggleValue() {
bool? newValue;
if (!_tristate) {
newValue = !_value!;
} else if (_tristate && _value == null) {
newValue = false;
} else if (_tristate && _value == false) {
newValue = true;
}
_onChange(newValue);
}

void _onChange(bool? value) {
var svalue = value != null ? value.toString() : "";
setState(() {
_value = value;
});
List<Map<String, String>> props = [
{"i": widget.control.id, "value": svalue}
];
widget.dispatch(
UpdateControlPropsAction(UpdateControlPropsPayload(props: props)));
var server = FletAppServices.of(context).server;
server.updateControlProps(props: props);
server.sendPageEvent(
eventTarget: widget.control.id, eventName: "change", eventData: svalue);
}

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

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

debugPrint("CupertinoCheckbox StoreConnector build: ${widget.control.id}");

bool? value = widget.control.attrBool("value", _tristate ? null : false);
if (_value != value) {
_value = value;
}

var cupertinoCheckbox = CupertinoCheckbox(
autofocus: autofocus,
focusNode: _focusNode,
value: _value,
activeColor: HexColor.fromString(
Theme.of(context), widget.control.attrString("activeColor", "")!),
checkColor: HexColor.fromString(
Theme.of(context), widget.control.attrString("checkColor", "")!),
focusColor: HexColor.fromString(
Theme.of(context), widget.control.attrString("focusColor", "")!),
inactiveColor: HexColor.fromString(
Theme.of(context), widget.control.attrString("inactiveColor", "")!),
tristate: _tristate,
onChanged: !disabled
? (bool? value) {
_onChange(value);
}
: null);

ListTileClicks.of(context)?.notifier.addListener(() {
_toggleValue();
});

Widget result = cupertinoCheckbox;
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 ? _toggleValue : null,
child: labelPosition == LabelPosition.right
? Row(children: [cupertinoCheckbox, labelWidget])
: Row(children: [labelWidget, cupertinoCheckbox])));
}

return constrainedControl(context, result, widget.parent, widget.control);
}
}
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 @@ -225,3 +225,4 @@
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_checkbox import CupertinoCheckbox
61 changes: 56 additions & 5 deletions sdk/python/packages/flet-core/src/flet_core/checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
ScaleValue,
)

try:
from typing import Literal
except ImportError:
from typing_extensions import Literal


class Checkbox(ConstrainedControl):
"""
Expand Down Expand Up @@ -91,7 +86,12 @@ def __init__(
tristate: Optional[bool] = None,
autofocus: Optional[bool] = None,
fill_color: Union[None, str, Dict[MaterialState, str]] = None,
overlay_color: Union[None, str, Dict[MaterialState, str]] = None,
check_color: Optional[str] = None,
active_color: Optional[str] = None,
hover_color: Optional[str] = None,
focus_color: Optional[str] = None,
adaptive: Optional[bool] = None,
on_change=None,
on_focus=None,
on_blur=None,
Expand Down Expand Up @@ -132,6 +132,11 @@ def __init__(
self.autofocus = autofocus
self.check_color = check_color
self.fill_color = fill_color
self.focus_color = focus_color
self.hover_color = hover_color
self.overlay_color = overlay_color
self.active_color = active_color
self.adaptive = adaptive
self.on_change = on_change
self.on_focus = on_focus
self.on_blur = on_blur
Expand All @@ -142,6 +147,7 @@ def _get_control_name(self):
def _before_build_command(self):
super()._before_build_command()
self._set_attr_json("fillColor", self.__fill_color)
self._set_attr_json("overlayColor", self.__overlay_color)

# value
@property
Expand All @@ -163,6 +169,15 @@ def tristate(self) -> Optional[bool]:
def tristate(self, value: Optional[bool]):
self._set_attr("tristate", value)

# adaptive
@property
def adaptive(self) -> Optional[bool]:
return self._get_attr("adaptive", data_type="bool", def_value=False)

@adaptive.setter
def adaptive(self, value: Optional[bool]):
self._set_attr("adaptive", value)

# label
@property
def label(self):
Expand Down Expand Up @@ -206,6 +221,33 @@ def check_color(self):
def check_color(self, value):
self._set_attr("checkColor", value)

# active_color
@property
def active_color(self):
return self._get_attr("activeColor")

@active_color.setter
def active_color(self, value):
self._set_attr("activeColor", value)

# focus_color
@property
def focus_color(self):
return self._get_attr("focusColor")

@focus_color.setter
def focus_color(self, value):
self._set_attr("focusColor", value)

# hover_color
@property
def hover_color(self):
return self._get_attr("hoverColor")

@hover_color.setter
def hover_color(self, value):
self._set_attr("hoverColor", value)

# fill_color
@property
def fill_color(self) -> Union[None, str, Dict[MaterialState, str]]:
Expand All @@ -215,6 +257,15 @@ def fill_color(self) -> Union[None, str, Dict[MaterialState, str]]:
def fill_color(self, value: Union[None, str, Dict[MaterialState, str]]):
self.__fill_color = value

# overlay_color
@property
def overlay_color(self) -> Union[None, str, Dict[MaterialState, str]]:
return self.__overlay_color

@overlay_color.setter
def overlay_color(self, value: Union[None, str, Dict[MaterialState, str]]):
self.__overlay_color = value

# on_change
@property
def on_change(self):
Expand Down
Loading