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
47 changes: 26 additions & 21 deletions packages/flet/lib/src/controls/base_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,7 @@ Widget _positionedControl(
top: top,
right: right,
bottom: bottom,
onEnd: control.getBool("on_animation_end", false)!
? () {
control.triggerEvent("animation_end", "position");
}
: null,
onEnd: () => control.triggerEvent("animation_end", "position"),
child: widget,
);
} else if (left != null || top != null || right != null || bottom != null) {
Expand All @@ -305,25 +301,34 @@ Widget _positionedControl(
}

Widget _sizedControl(Widget widget, Control control) {
final skipProps = control.internals?["skip_properties"] as List?;
if (skipProps?.contains("width") == true ||
skipProps?.contains("height") == true) {
final skipProps = control.internals?['skip_properties'] as List?;
if (skipProps != null && ['width', 'height'].any(skipProps.contains)) {
return widget;
}

var width = control.getDouble("width");
var height = control.getDouble("height");
final width = control.getDouble("width");
final height = control.getDouble("height");
final animationSize = control.getAnimation("animate_size");

if ((width != null || height != null)) {
widget = ConstrainedBox(
constraints: BoxConstraints.tightFor(width: width, height: height),
child: widget,
);
}
var animation = control.getAnimation("animate_size");
if (animation != null) {
return AnimatedSize(
duration: animation.duration, curve: animation.curve, child: widget);
final hasFixedSize = width != null || height != null;

if (animationSize != null) {
return hasFixedSize
? AnimatedContainer(
duration: animationSize.duration,
curve: animationSize.curve,
width: width,
height: height,
child: widget,
)
: AnimatedSize(
duration: animationSize.duration,
curve: animationSize.curve,
child: widget,
);
} else {
return hasFixedSize
? SizedBox(width: width, height: height, child: widget)
: widget;
}
return widget;
}
36 changes: 35 additions & 1 deletion packages/flet/lib/src/controls/cupertino_textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ class _CupertinoTextFieldControlState extends State<CupertinoTextFieldControl> {
late final FocusNode _shiftEnterfocusNode;
String? _lastFocusValue;
String? _lastBlurValue;
TextSelection? _selection;

@override
void initState() {
super.initState();
_controller = TextEditingController();
_controller.addListener(_handleControllerChange);
_shiftEnterfocusNode = FocusNode(
onKeyEvent: (FocusNode node, KeyEvent evt) {
if (!HardwareKeyboard.instance.isShiftPressed &&
Expand All @@ -67,6 +69,7 @@ class _CupertinoTextFieldControlState extends State<CupertinoTextFieldControl> {

@override
void dispose() {
_controller.removeListener(_handleControllerChange);
_controller.dispose();
_shiftEnterfocusNode.removeListener(_onShiftEnterFocusChange);
_shiftEnterfocusNode.dispose();
Expand Down Expand Up @@ -101,6 +104,25 @@ class _CupertinoTextFieldControlState extends State<CupertinoTextFieldControl> {
widget.control.triggerEvent(_focusNode.hasFocus ? "focus" : "blur");
}

void _handleControllerChange() {
final selection = _controller.selection;
if (_selection == selection) return;

_selection = selection;

if (!selection.isValid ||
!widget.control.getBool("on_selection_change", false)!) {
return;
}

widget.control.updateProperties({"selection": selection.toMap()});
widget.control.triggerEvent("selection_change", {
"selected_text":
_controller.text.substring(selection.start, selection.end),
"selection": selection.toMap()
});
}

@override
Widget build(BuildContext context) {
debugPrint("CupertinoTextField build: ${widget.control.id}");
Expand All @@ -109,7 +131,12 @@ class _CupertinoTextFieldControlState extends State<CupertinoTextFieldControl> {
var value = widget.control.getString("value", "")!;
if (_value != value) {
_value = value;
_controller.text = value;
_controller.value = TextEditingValue(
text: value,
// preserve cursor position at the end
selection: TextSelection.collapsed(offset: value.length),
);
_selection = _controller.selection;
}

var shiftEnter = widget.control.getBool("shift_enter", false)!;
Expand Down Expand Up @@ -165,6 +192,13 @@ class _CupertinoTextFieldControlState extends State<CupertinoTextFieldControl> {
_focusNode.unfocus();
}

var selection = widget.control.getTextSelection("selection",
minOffset: 0, maxOffset: _controller.text.length);
if (selection != null && selection != _controller.selection) {
_controller.selection = selection;
_selection = selection;
}

var borderRadius = widget.control.getBorderRadius("border_radius");

BoxBorder? border;
Expand Down
3 changes: 1 addition & 2 deletions packages/flet/lib/src/controls/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,12 @@ class TextControl extends StatelessWidget {

TextAlign textAlign =
parseTextAlign(control.getString("text_align"), TextAlign.start)!;

TextOverflow overflow =
parseTextOverflow(control.getString("overflow"), TextOverflow.clip)!;

onSelectionChanged(TextSelection selection, SelectionChangedCause? cause) {
control.triggerEvent("selection_change", {
"text": text,
"selected_text": text,
"cause": cause?.name ?? "unknown",
"selection": selection.toMap(),
});
Expand Down
34 changes: 32 additions & 2 deletions packages/flet/lib/src/controls/textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ class _TextFieldControlState extends State<TextFieldControl> {
late final FocusNode _shiftEnterfocusNode;
String? _lastFocusValue;
String? _lastBlurValue;
TextSelection? _selection;

@override
void initState() {
super.initState();
_controller = TextEditingController();
_controller.addListener(_handleControllerChange);
_shiftEnterfocusNode = FocusNode(
onKeyEvent: (FocusNode node, KeyEvent evt) {
if (!HardwareKeyboard.instance.isShiftPressed &&
Expand All @@ -62,6 +64,7 @@ class _TextFieldControlState extends State<TextFieldControl> {

@override
void dispose() {
_controller.removeListener(_handleControllerChange);
_controller.dispose();
_shiftEnterfocusNode.removeListener(_onShiftEnterFocusChange);
_shiftEnterfocusNode.dispose();
Expand Down Expand Up @@ -92,6 +95,25 @@ class _TextFieldControlState extends State<TextFieldControl> {
widget.control.triggerEvent(_focusNode.hasFocus ? "focus" : "blur");
}

void _handleControllerChange() {
final selection = _controller.selection;
if (_selection == selection) return;

_selection = selection;

if (!selection.isValid ||
!widget.control.getBool("on_selection_change", false)!) {
return;
}

widget.control.updateProperties({"selection": selection.toMap()});
widget.control.triggerEvent("selection_change", {
"selected_text":
_controller.text.substring(selection.start, selection.end),
"selection": selection.toMap()
});
}

@override
Widget build(BuildContext context) {
debugPrint("TextField build: ${widget.control.id}");
Expand All @@ -103,9 +125,17 @@ class _TextFieldControlState extends State<TextFieldControl> {
_value = value;
_controller.value = TextEditingValue(
text: value,
selection: TextSelection.collapsed(
offset: value.length), // preserve cursor position at the end
// preserve cursor position at the end
selection: TextSelection.collapsed(offset: value.length),
);
_selection = _controller.selection;
}

var selection = widget.control.getTextSelection("selection",
minOffset: 0, maxOffset: _controller.text.length);
if (selection != null && selection != _controller.selection) {
_controller.selection = selection;
_selection = selection;
}

var shiftEnter = widget.control.getBool("shift_enter", false)!;
Expand Down
Loading
Loading