Skip to content

Commit

Permalink
fix/feat: make SearchBar's view height adjustable; add new properti…
Browse files Browse the repository at this point in the history
…es (#4039)

* create BoxConstraints dataclass

* SearchBar new props: view_size_constraints, view_header_height

* new parseWidgetState* utils:

* SearchBar: expose more props
  • Loading branch information
ndonkoHenri authored Oct 18, 2024
1 parent 5c8b0be commit 5e87936
Show file tree
Hide file tree
Showing 11 changed files with 314 additions and 19 deletions.
38 changes: 32 additions & 6 deletions packages/flet/lib/src/controls/search_anchor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import 'package:flutter/material.dart';
import '../flet_control_backend.dart';
import '../models/control.dart';
import '../utils/borders.dart';
import '../utils/box.dart';
import '../utils/colors.dart';
import '../utils/edge_insets.dart';
import '../utils/form_field.dart';
import '../utils/numbers.dart';
import '../utils/text.dart';
import 'create_control.dart';

Expand Down Expand Up @@ -52,7 +55,7 @@ class _SearchAnchorControlState extends State<SearchAnchorControl> {
});
widget.backend.triggerControlEvent(
widget.control.id, _focusNode.hasFocus ? "focus" : "blur");
}
}

@override
void dispose() {
Expand Down Expand Up @@ -164,6 +167,9 @@ class _SearchAnchorControlState extends State<SearchAnchorControl> {
dividerColor: dividerColor,
viewHintText: widget.control.attrString("viewHintText"),
viewElevation: widget.control.attrDouble("viewElevation"),
headerHeight: widget.control.attrDouble("viewHeaderHeight"),
viewConstraints:
parseBoxConstraints(widget.control, "viewSizeConstraints"),
viewShape: parseOutlinedBorder(widget.control, "viewShape"),
viewTrailing: viewTrailingCtrls.isNotEmpty
? viewTrailingCtrls.map((ctrl) {
Expand Down Expand Up @@ -203,6 +209,19 @@ class _SearchAnchorControlState extends State<SearchAnchorControl> {
autoFocus: widget.control.attrBool("autoFocus", false)!,
focusNode: _focusNode,
hintText: widget.control.attrString("barHintText"),
elevation: parseWidgetStateDouble(widget.control, "barElevation"),
shape: parseWidgetStateOutlinedBorder(widget.control, "barShape"),
padding: parseWidgetStateEdgeInsets(widget.control, "barPadding"),
textStyle: parseWidgetStateTextStyle(
Theme.of(context), widget.control, "barTextStyle"),
hintStyle: parseWidgetStateTextStyle(
Theme.of(context), widget.control, "barHintTextStyle"),
shadowColor: parseWidgetStateColor(
Theme.of(context), widget.control, "barShadowColor"),
surfaceTintColor: parseWidgetStateColor(
Theme.of(context), widget.control, "barSurfaceTintColor"),
side: parseWidgetStateBorderSide(
Theme.of(context), widget.control, "barBorderSide"),
backgroundColor: parseWidgetStateColor(
Theme.of(context), widget.control, "barBgcolor"),
overlayColor: parseWidgetStateColor(
Expand All @@ -218,11 +237,18 @@ class _SearchAnchorControlState extends State<SearchAnchorControl> {
parentAdaptive: widget.parentAdaptive);
})
: null,
onTap: () {
if (onTap) {
widget.backend.triggerControlEvent(widget.control.id, "tap");
}
},
onTap: onTap
? () {
widget.backend
.triggerControlEvent(widget.control.id, "tap");
}
: null,
onTapOutside: widget.control.attrBool("onTapOutsideBar", false)!
? (PointerDownEvent? event) {
widget.backend.triggerControlEvent(
widget.control.id, "tapOutsideBar");
}
: null,
onSubmitted: onSubmit
? (String value) {
debugPrint("SearchBar.onSubmit: $value");
Expand Down
12 changes: 12 additions & 0 deletions packages/flet/lib/src/utils/borders.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/material.dart';

import '../models/control.dart';
import 'colors.dart';
import 'material_state.dart';
import 'numbers.dart';

BorderRadius? parseBorderRadius(Control control, String propName,
Expand Down Expand Up @@ -172,3 +173,14 @@ class WidgetStateBorderSideFromJSON extends WidgetStateBorderSide {
return _defaultValue;
}
}

WidgetStateProperty<OutlinedBorder?>? parseWidgetStateOutlinedBorder(
Control control, String propName) {
var v = control.attrString(propName, null);
if (v == null) {
return null;
}

return getWidgetStateProperty<OutlinedBorder?>(
jsonDecode(v), (jv) => outlinedBorderFromJSON(jv), null);
}
23 changes: 23 additions & 0 deletions packages/flet/lib/src/utils/box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,29 @@ import 'numbers.dart';
import 'others.dart';
import 'transforms.dart';

BoxConstraints? parseBoxConstraints(Control control, String propName) {
var v = control.attrString(propName);
if (v == null) {
return null;
}

final j1 = json.decode(v);
return boxConstraintsFromJSON(j1);
}

BoxConstraints? boxConstraintsFromJSON(dynamic json,
[BoxConstraints? defValue]) {
if (json == null) {
return null;
}
return BoxConstraints(
minHeight: parseDouble(json["min_height"], 0.0)!,
minWidth: parseDouble(json["min_width"], 0.0)!,
maxHeight: parseDouble(json["max_height"], 0.0)!,
maxWidth: parseDouble(json["max_width"], 0.0)!,
);
}

List<BoxShadow> parseBoxShadow(
ThemeData theme, Control control, String propName) {
var v = control.attrString(propName);
Expand Down
10 changes: 5 additions & 5 deletions packages/flet/lib/src/utils/colors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,16 @@ extension ColorExtension on Color {
}
}

WidgetStateProperty<Color?>? parseWidgetStateColor(
ThemeData theme, Control control, String propName) {
WidgetStateProperty<Color?>? parseWidgetStateColor(ThemeData theme,
Control control, String propName,
[Color? defaultValue]) {
var v = control.attrString(propName, null);
if (v == null) {
return null;
}

final j1 = json.decode(v);
return getWidgetStateProperty<Color?>(
j1, (jv) => HexColor.fromString(theme, jv as String), null);
return getWidgetStateProperty<Color?>(jsonDecode(v),
(jv) => HexColor.fromString(theme, jv as String), defaultValue);
}

Color? parseColor(ThemeData? theme, String? colorString,
Expand Down
13 changes: 13 additions & 0 deletions packages/flet/lib/src/utils/edge_insets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert';
import 'package:flutter/widgets.dart';

import '../models/control.dart';
import 'material_state.dart';
import 'numbers.dart';

EdgeInsets? parseEdgeInsets(Control control, String propName,
Expand Down Expand Up @@ -54,3 +55,15 @@ EdgeInsetsDirectional? edgeInsetsDirectionalFromJson(dynamic json,
parseDouble(json['r'], 0)!,
parseDouble(json['b'], 0)!);
}

WidgetStateProperty<EdgeInsets?>? parseWidgetStateEdgeInsets(
Control control, String propName,
[EdgeInsets? defaultValue]) {
var v = control.attrString(propName, null);
if (v == null) {
return null;
}

return getWidgetStateProperty<EdgeInsets?>(
jsonDecode(v), (jv) => edgeInsetsFromJson(jv), defaultValue);
}
39 changes: 39 additions & 0 deletions packages/flet/lib/src/utils/numbers.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import 'dart:convert';

import 'package:flutter/material.dart';

import '../models/control.dart';
import 'material_state.dart';

double? parseDouble(dynamic v, [double? defValue]) {
if (v is double) {
return v;
Expand All @@ -8,6 +15,17 @@ double? parseDouble(dynamic v, [double? defValue]) {
}
}

WidgetStateProperty<double?>? parseWidgetStateDouble(
Control control, String propName,
[double? defaultValue]) {
var v = control.attrString(propName, null);
if (v == null) {
return null;
}
return getWidgetStateProperty<double?>(
jsonDecode(v), (jv) => parseDouble(jv), defaultValue);
}

int? parseInt(dynamic v, [int? defValue]) {
if (v is int) {
return v;
Expand All @@ -18,6 +36,16 @@ int? parseInt(dynamic v, [int? defValue]) {
}
}

WidgetStateProperty<int?>? parseWidgetStateInt(Control control, String propName,
[int? defaultValue]) {
var v = control.attrString(propName, null);
if (v == null) {
return null;
}
return getWidgetStateProperty<int?>(
jsonDecode(v), (jv) => parseInt(jv), defaultValue);
}

bool? parseBool(dynamic v, [bool? defValue]) {
if (v is bool) {
return v;
Expand All @@ -27,3 +55,14 @@ bool? parseBool(dynamic v, [bool? defValue]) {
return "true" == v.toString().toLowerCase();
}
}

WidgetStateProperty<bool?>? parseWidgetStateBool(
Control control, String propName,
[bool? defaultValue]) {
var v = control.attrString(propName, null);
if (v == null) {
return null;
}
return getWidgetStateProperty<bool?>(
jsonDecode(v), (jv) => parseBool(jv), defaultValue);
}
11 changes: 11 additions & 0 deletions packages/flet/lib/src/utils/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import '../utils/drawing.dart';
import '../utils/numbers.dart';
import 'colors.dart';
import 'launch_url.dart';
import 'material_state.dart';

TextStyle? getTextStyle(BuildContext context, String styleName) {
var textTheme = Theme.of(context).textTheme;
Expand Down Expand Up @@ -243,3 +244,13 @@ TextStyle? textStyleFromJson(ThemeData theme, Map<String, dynamic>? json) {
textBaseline: parseTextBaseline(json['text_baseline']),
);
}

WidgetStateProperty<TextStyle?>? parseWidgetStateTextStyle(
ThemeData theme, Control control, String propName) {
var v = control.attrString(propName);
if (v == null) {
return null;
}
return getWidgetStateProperty<TextStyle?>(
jsonDecode(v), (jv) => textStyleFromJson(theme, jv), null);
}
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 @@ -56,6 +56,7 @@
ShadowBlurStyle,
BoxDecoration,
BoxShape,
BoxConstraints,
DecorationImage,
FilterQuality,
ColorFilter,
Expand Down
8 changes: 8 additions & 0 deletions sdk/python/packages/flet-core/src/flet_core/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,11 @@ class BoxDecoration:
gradient: Optional[Gradient] = None
shape: Optional[BoxShape] = None
blend_mode: Optional[BlendMode] = None


@dataclass
class BoxConstraints:
min_width: OptionalNumber = None
min_height: OptionalNumber = None
max_width: OptionalNumber = None
max_height: OptionalNumber = None
9 changes: 9 additions & 0 deletions sdk/python/packages/flet-core/src/flet_core/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
ResponsiveNumber,
SupportsStr,
OptionalControlEventCallable,
ControlState,
)
from flet_core.utils import deprecated

Expand Down Expand Up @@ -179,6 +180,14 @@ def _set_attr_json(self, name: str, value: V) -> None:
if ov != nv:
self._set_attr(name, nv)

def _set_control_state_attr_json(self, name: str, value: V) -> None:
if value is not None and not isinstance(value, dict):
value = {ControlState.DEFAULT: value}
ov = self._get_attr(name)
nv = self._convert_attr_json(value)
if ov != nv:
self._set_attr(name, nv)

def _convert_attr_json(self, value: V) -> str:
return (
json.dumps(value, cls=EmbedJsonEncoder, separators=(",", ":"))
Expand Down
Loading

0 comments on commit 5e87936

Please sign in to comment.