Skip to content

Commit 7fa9de7

Browse files
authored
Add AppBar.adaptive (#2458)
* AppBar.adaptive * add support for AppBar.adaptive
1 parent 3850d1e commit 7fa9de7

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

package/lib/src/controls/app_bar.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import 'package:flutter/foundation.dart';
12
import 'package:flutter/material.dart';
23

34
import '../models/control.dart';
45
import '../utils/colors.dart';
56
import 'create_control.dart';
7+
import 'cupertino_app_bar.dart';
68

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

28+
bool adaptive = control.attrBool("adaptive", false)!;
29+
if (adaptive &&
30+
(defaultTargetPlatform == TargetPlatform.iOS ||
31+
defaultTargetPlatform == TargetPlatform.macOS)) {
32+
return CupertinoAppBarControl(
33+
control: control,
34+
parentDisabled: parentDisabled,
35+
children: children,
36+
bgcolor: HexColor.fromString(
37+
Theme.of(context), control.attrString("bgcolor", "")!));
38+
}
39+
2640
var leadingCtrls =
2741
children.where((c) => c.name == "leading" && c.isVisible);
2842
var titleCtrls = children.where((c) => c.name == "title" && c.isVisible);

package/lib/src/controls/cupertino_app_bar.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ class CupertinoAppBarControl extends StatelessWidget
2929

3030
var leadingCtrls =
3131
children.where((c) => c.name == "leading" && c.isVisible);
32-
var middleCtrls = children.where((c) => c.name == "middle" && c.isVisible);
33-
var trailingCtrls =
34-
children.where((c) => c.name == "trailing" && c.isVisible);
32+
33+
// if the material AppBar was used with adaptive=True, AppBar.title will be used as middle control
34+
var middleCtrls = children
35+
.where((c) => (c.name == "middle" || c.name == "title") && c.isVisible);
36+
37+
// if the material AppBar was used with adaptive=True, AppBar.actions[0] will be used as trailing control
38+
var trailingCtrls = children.where(
39+
(c) => (c.name == "trailing" || c.name == "action") && c.isVisible);
3540

3641
var automaticallyImplyLeading =
3742
control.attrBool("automaticallyImplyLeading", true)!;

sdk/python/packages/flet-core/src/flet_core/app_bar.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def __init__(
6161
bgcolor: Optional[str] = None,
6262
elevation: OptionalNumber = None,
6363
actions: Optional[List[Control]] = None,
64+
adaptive: Optional[bool] = None,
6465
):
6566
Control.__init__(self, ref=ref)
6667

@@ -78,6 +79,7 @@ def __init__(
7879
self.bgcolor = bgcolor
7980
self.elevation = elevation
8081
self.actions = actions
82+
self.adaptive = adaptive
8183

8284
def _get_control_name(self):
8385
return "appbar"
@@ -191,3 +193,12 @@ def actions(self):
191193
@actions.setter
192194
def actions(self, value):
193195
self.__actions = value if value is not None else []
196+
197+
# adaptive
198+
@property
199+
def adaptive(self) -> Optional[bool]:
200+
return self._get_attr("adaptive", data_type="bool", def_value=False)
201+
202+
@adaptive.setter
203+
def adaptive(self, value: Optional[bool]):
204+
self._set_attr("adaptive", value)

0 commit comments

Comments
 (0)