Skip to content

Commit 987836c

Browse files
authored
Merge pull request zero-one-group#170 from smartoryu/feat/211-set-automaticallyimplyleading-to-leading-zeroappbar
Feat/211 set automaticallyimplyleading to leading zeroappbar
2 parents e7c76e9 + c758bd2 commit 987836c

File tree

2 files changed

+71
-13
lines changed

2 files changed

+71
-13
lines changed

packages/mobile/example/lib/component/navigation/app_bar_example.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ class ZeroAppBarExample extends StatelessWidget {
5454
.push(MaterialPageRoute(builder: (_) => const _AppBar6()));
5555
},
5656
),
57+
ZeroListTile(
58+
title: 'Centered Title only',
59+
onTap: () {
60+
Navigator.of(context)
61+
.push(MaterialPageRoute(builder: (_) => const _AppBar7()));
62+
},
63+
),
5764
],
5865
),
5966
);
@@ -122,6 +129,12 @@ class _AppBar3 extends StatelessWidget {
122129
)
123130
],
124131
),
132+
body: Center(
133+
child: ElevatedButton(
134+
onPressed: Navigator.of(context).pop,
135+
child: const Text('Back'),
136+
),
137+
),
125138
);
126139
}
127140
}
@@ -209,3 +222,24 @@ class _AppBar6 extends StatelessWidget {
209222
);
210223
}
211224
}
225+
226+
class _AppBar7 extends StatelessWidget {
227+
const _AppBar7();
228+
229+
@override
230+
Widget build(BuildContext context) {
231+
return Scaffold(
232+
appBar: ZeroAppBar(
233+
style: const ZeroAppBarStyle(centerTitle: true),
234+
automaticallyImplyLeading: false,
235+
title: const Text('Example 7'),
236+
),
237+
body: Center(
238+
child: ElevatedButton(
239+
onPressed: Navigator.of(context).pop,
240+
child: const Text('Back'),
241+
),
242+
),
243+
);
244+
}
245+
}

packages/mobile/lib/components/navigation/zero_app_bar.dart

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ class ZeroAppBar extends StatelessWidget implements PreferredSizeWidget {
9797
statusBarBrightness: adaptiveStyle.statusBarBrightness,
9898
);
9999

100+
final _isNoLeading = !automaticallyImplyLeading && leading == null;
101+
100102
return Semantics(
101103
container: true,
102104
child: AnnotatedRegion<SystemUiOverlayStyle>(
@@ -134,9 +136,19 @@ class ZeroAppBar extends StatelessWidget implements PreferredSizeWidget {
134136
children: [
135137
// Build leading
136138
_Leading(
137-
auto: automaticallyImplyLeading,
139+
automaticallyImplyLeading:
140+
automaticallyImplyLeading,
138141
leading: leading,
139142
),
143+
144+
// Build spacing based on conditions
145+
if (_isNoLeading)
146+
const SizedBox(width: 16)
147+
else if (adaptiveStyle.centerTitle == true)
148+
const SizedBox.shrink()
149+
else
150+
const SizedBox(width: 32),
151+
140152
// Build title small size
141153
Expanded(
142154
child: size == ZeroAppBarSize.small
@@ -146,8 +158,18 @@ class ZeroAppBar extends StatelessWidget implements PreferredSizeWidget {
146158
)
147159
: const SizedBox.shrink(),
148160
),
161+
149162
// Build actions
150-
Row(children: actions ?? [])
163+
Row(
164+
children: actions ??
165+
(adaptiveStyle.centerTitle == true
166+
? [
167+
SizedBox.square(
168+
dimension:
169+
_isNoLeading ? 16 : 48)
170+
]
171+
: []),
172+
)
151173
],
152174
),
153175
),
@@ -158,7 +180,9 @@ class ZeroAppBar extends StatelessWidget implements PreferredSizeWidget {
158180
const Spacer(),
159181
Padding(
160182
padding: EdgeInsets.only(
161-
bottom: size == ZeroAppBarSize.large ? 20 : 16),
183+
bottom: size == ZeroAppBarSize.large ? 20 : 16,
184+
left: 16,
185+
),
162186
child: _Title(style: adaptiveStyle, title: title),
163187
),
164188
],
@@ -202,10 +226,7 @@ class _Title extends StatelessWidget {
202226
style: titleStyle,
203227
maxLines: 1,
204228
overflow: TextOverflow.ellipsis,
205-
child: Padding(
206-
padding: const EdgeInsets.only(left: 16),
207-
child: title ?? const SizedBox(),
208-
),
229+
child: title ?? const SizedBox.shrink(),
209230
);
210231

211232
/// If centerTitle, title will be wrap with [Center] widget
@@ -216,19 +237,22 @@ class _Title extends StatelessWidget {
216237
/// A widget for building leading of [ZeroAppBar]
217238
class _Leading extends StatelessWidget {
218239
const _Leading({
219-
required this.auto,
240+
required this.automaticallyImplyLeading,
220241
required this.leading,
221242
});
222243

223244
/// Set automatically leading or not
224-
final bool auto;
245+
final bool automaticallyImplyLeading;
225246

226-
/// Customize leading, if null and [auto] is true leading will auto set
247+
/// Customize leading, if null and [automaticallyImplyLeading] is true leading will auto set
227248
final Widget? leading;
228249

229250
@override
230251
Widget build(BuildContext context) {
231-
if (leading != null) return leading ?? const SizedBox();
252+
if (automaticallyImplyLeading == false || leading != null) {
253+
return leading ?? const SizedBox.shrink();
254+
}
255+
232256
final scaffold = Scaffold.maybeOf(context);
233257
final parentRoute = ModalRoute.of(context);
234258
final hasDrawer = scaffold?.hasDrawer ?? false;
@@ -253,11 +277,11 @@ class _Leading extends StatelessWidget {
253277
onPressed: () {
254278
Navigator.of(context).pop();
255279
},
256-
tooltip: localization.backButtonTooltip,
257280
icon: const Icon(ZeroIcons.arrowLeft),
281+
tooltip: localization.backButtonTooltip,
258282
);
259283
}
260284

261-
return const SizedBox();
285+
return const SizedBox.shrink();
262286
}
263287
}

0 commit comments

Comments
 (0)