Skip to content

Commit

Permalink
Add AppBar.adaptive (#2458)
Browse files Browse the repository at this point in the history
* AppBar.adaptive

* add support for AppBar.adaptive
  • Loading branch information
ndonkoHenri committed Jan 22, 2024
1 parent 3850d1e commit 7fa9de7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
14 changes: 14 additions & 0 deletions package/lib/src/controls/app_bar.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import '../models/control.dart';
import '../utils/colors.dart';
import 'create_control.dart';
import 'cupertino_app_bar.dart';

class AppBarControl extends StatelessWidget implements PreferredSizeWidget {
final Control? parent;
Expand All @@ -23,6 +25,18 @@ class AppBarControl extends StatelessWidget implements PreferredSizeWidget {
Widget build(BuildContext context) {
debugPrint("AppBar build: ${control.id}");

bool adaptive = control.attrBool("adaptive", false)!;
if (adaptive &&
(defaultTargetPlatform == TargetPlatform.iOS ||
defaultTargetPlatform == TargetPlatform.macOS)) {
return CupertinoAppBarControl(
control: control,
parentDisabled: parentDisabled,
children: children,
bgcolor: HexColor.fromString(
Theme.of(context), control.attrString("bgcolor", "")!));
}

var leadingCtrls =
children.where((c) => c.name == "leading" && c.isVisible);
var titleCtrls = children.where((c) => c.name == "title" && c.isVisible);
Expand Down
11 changes: 8 additions & 3 deletions package/lib/src/controls/cupertino_app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ class CupertinoAppBarControl extends StatelessWidget

var leadingCtrls =
children.where((c) => c.name == "leading" && c.isVisible);
var middleCtrls = children.where((c) => c.name == "middle" && c.isVisible);
var trailingCtrls =
children.where((c) => c.name == "trailing" && c.isVisible);

// if the material AppBar was used with adaptive=True, AppBar.title will be used as middle control
var middleCtrls = children
.where((c) => (c.name == "middle" || c.name == "title") && c.isVisible);

// if the material AppBar was used with adaptive=True, AppBar.actions[0] will be used as trailing control
var trailingCtrls = children.where(
(c) => (c.name == "trailing" || c.name == "action") && c.isVisible);

var automaticallyImplyLeading =
control.attrBool("automaticallyImplyLeading", true)!;
Expand Down
11 changes: 11 additions & 0 deletions sdk/python/packages/flet-core/src/flet_core/app_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def __init__(
bgcolor: Optional[str] = None,
elevation: OptionalNumber = None,
actions: Optional[List[Control]] = None,
adaptive: Optional[bool] = None,
):
Control.__init__(self, ref=ref)

Expand All @@ -78,6 +79,7 @@ def __init__(
self.bgcolor = bgcolor
self.elevation = elevation
self.actions = actions
self.adaptive = adaptive

def _get_control_name(self):
return "appbar"
Expand Down Expand Up @@ -191,3 +193,12 @@ def actions(self):
@actions.setter
def actions(self, value):
self.__actions = value if value is not None else []

# 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)

0 comments on commit 7fa9de7

Please sign in to comment.