app_bar.dart 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. // Copyright 2014 The Flutter Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. import 'dart:math' as math;
  5. import 'package:flutter/foundation.dart';
  6. import 'package:flutter/material.dart' hide BackButtonIcon, kToolbarHeight;
  7. import 'package:flutter/rendering.dart';
  8. import 'package:flutter/services.dart';
  9. const double kToolbarHeight = 44;
  10. const double _kLeadingWidth =
  11. kToolbarHeight; // So the leading button is square.
  12. const double _kMaxTitleTextScaleFactor = 1.34;
  13. // Bottom justify the toolbarHeight child which may overflow the top.
  14. class _ToolbarContainerLayout extends SingleChildLayoutDelegate {
  15. const _ToolbarContainerLayout(this.toolbarHeight);
  16. final double toolbarHeight;
  17. @override
  18. BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
  19. return constraints.tighten(height: toolbarHeight);
  20. }
  21. @override
  22. Size getSize(BoxConstraints constraints) {
  23. return Size(constraints.maxWidth, toolbarHeight);
  24. }
  25. @override
  26. Offset getPositionForChild(Size size, Size childSize) {
  27. return Offset(0.0, size.height - childSize.height);
  28. }
  29. @override
  30. bool shouldRelayout(_ToolbarContainerLayout oldDelegate) =>
  31. toolbarHeight != oldDelegate.toolbarHeight;
  32. }
  33. class WAppBar extends StatefulWidget implements PreferredSizeWidget {
  34. WAppBar({
  35. Key? key,
  36. this.leading,
  37. this.automaticallyImplyLeading = true,
  38. this.title,
  39. this.actions,
  40. this.flexibleSpace,
  41. this.bottom,
  42. this.elevation,
  43. this.shadowColor,
  44. this.shape,
  45. this.backgroundColor,
  46. this.brightness,
  47. this.iconTheme,
  48. this.actionsIconTheme,
  49. this.textTheme,
  50. this.primary = true,
  51. this.centerTitle = true,
  52. this.excludeHeaderSemantics = false,
  53. this.titleSpacing = NavigationToolbar.kMiddleSpacing,
  54. this.toolbarOpacity = 1.0,
  55. this.bottomOpacity = 1.0,
  56. this.toolbarHeight,
  57. this.leadingWidth,
  58. }) : assert(elevation == null || elevation >= 0.0),
  59. preferredSize = Size.fromHeight(toolbarHeight ??
  60. kToolbarHeight + (bottom?.preferredSize.height ?? 0.0)),
  61. super(key: key);
  62. final Widget? leading;
  63. final bool automaticallyImplyLeading;
  64. final Widget? title;
  65. final List<Widget>? actions;
  66. final Widget? flexibleSpace;
  67. final PreferredSizeWidget? bottom;
  68. final double? elevation;
  69. final Color? shadowColor;
  70. final ShapeBorder? shape;
  71. final Color? backgroundColor;
  72. final Brightness? brightness;
  73. /// The color, opacity, and size to use for app bar icons. Typically this
  74. /// is set along with [backgroundColor], [brightness], [textTheme].
  75. ///
  76. /// If this property is null, then [AppBarTheme.iconTheme] of
  77. /// [ThemeData.appBarTheme] is used. If that is also null, then
  78. /// [ThemeData.primaryIconTheme] is used.
  79. final IconThemeData? iconTheme;
  80. /// The color, opacity, and size to use for the icons that appear in the app
  81. /// bar's [actions]. This should only be used when the [actions] should be
  82. /// themed differently than the icon that appears in the app bar's [leading]
  83. /// widget.
  84. ///
  85. /// If this property is null, then [AppBarTheme.actionsIconTheme] of
  86. /// [ThemeData.appBarTheme] is used. If that is also null, then this falls
  87. /// back to [iconTheme].
  88. final IconThemeData? actionsIconTheme;
  89. /// The typographic styles to use for text in the app bar. Typically this is
  90. /// set along with [brightness] [backgroundColor], [iconTheme].
  91. ///
  92. /// If this property is null, then [AppBarTheme.textTheme] of
  93. /// [ThemeData.appBarTheme] is used. If that is also null, then
  94. /// [ThemeData.primaryTextTheme] is used.
  95. final TextTheme? textTheme;
  96. /// Whether this app bar is being displayed at the top of the screen.
  97. ///
  98. /// If true, the app bar's toolbar elements and [bottom] widget will be
  99. /// padded on top by the height of the system status bar. The layout
  100. /// of the [flexibleSpace] is not affected by the [primary] property.
  101. final bool primary;
  102. /// Whether the title should be centered.
  103. ///
  104. /// If this property is null, then [AppBarTheme.centerTitle] of
  105. /// [ThemeData.appBarTheme] is used. If that is also null, then value is
  106. /// adapted to the current [TargetPlatform].
  107. final bool? centerTitle;
  108. /// Whether the title should be wrapped with header [Semantics].
  109. ///
  110. /// Defaults to false.
  111. final bool excludeHeaderSemantics;
  112. /// The spacing around [title] content on the horizontal axis. This spacing is
  113. /// applied even if there is no [leading] content or [actions]. If you want
  114. /// [title] to take all the space available, set this value to 0.0.
  115. ///
  116. /// Defaults to [NavigationToolbar.kMiddleSpacing].
  117. final double titleSpacing;
  118. /// How opaque the toolbar part of the app bar is.
  119. ///
  120. /// A value of 1.0 is fully opaque, and a value of 0.0 is fully transparent.
  121. ///
  122. /// Typically, this value is not changed from its default value (1.0). It is
  123. /// used by [SliverWAppBar] to animate the opacity of the toolbar when the app
  124. /// bar is scrolled.
  125. final double toolbarOpacity;
  126. /// How opaque the bottom part of the app bar is.
  127. ///
  128. /// A value of 1.0 is fully opaque, and a value of 0.0 is fully transparent.
  129. ///
  130. /// Typically, this value is not changed from its default value (1.0). It is
  131. /// used by [SliverWAppBar] to animate the opacity of the toolbar when the app
  132. /// bar is scrolled.
  133. final double bottomOpacity;
  134. /// A size whose height is the sum of [toolbarHeight] and the [bottom] widget's
  135. /// preferred height.
  136. ///
  137. /// [Scaffold] uses this size to set its app bar's height.
  138. @override
  139. final Size preferredSize;
  140. /// Defines the height of the toolbar component of an [WAppBar].
  141. ///
  142. /// By default, the value of `toolbarHeight` is [kToolbarHeight].
  143. final double? toolbarHeight;
  144. /// Defines the width of [leading] widget.
  145. ///
  146. /// By default, the value of `leadingWidth` is 56.0.
  147. final double? leadingWidth;
  148. bool? _getEffectiveCenterTitle(ThemeData theme) {
  149. if (centerTitle != null) return centerTitle;
  150. if (theme.appBarTheme.centerTitle != null)
  151. return theme.appBarTheme.centerTitle;
  152. switch (theme.platform) {
  153. case TargetPlatform.android:
  154. case TargetPlatform.fuchsia:
  155. case TargetPlatform.linux:
  156. case TargetPlatform.windows:
  157. return false;
  158. case TargetPlatform.iOS:
  159. case TargetPlatform.macOS:
  160. return actions == null || actions!.length < 2;
  161. }
  162. }
  163. @override
  164. _WAppBarState createState() => _WAppBarState();
  165. }
  166. class _WAppBarState extends State<WAppBar> {
  167. static const double _defaultElevation = 4.0;
  168. static const Color _defaultShadowColor = Color(0xFF000000);
  169. void _handleDrawerButton() {
  170. Scaffold.of(context).openDrawer();
  171. }
  172. void _handleDrawerButtonEnd() {
  173. Scaffold.of(context).openEndDrawer();
  174. }
  175. @override
  176. Widget build(BuildContext context) {
  177. assert(!widget.primary || debugCheckHasMediaQuery(context));
  178. assert(debugCheckHasMaterialLocalizations(context));
  179. final ThemeData theme = Theme.of(context);
  180. final AppBarTheme appBarTheme = AppBarTheme.of(context);
  181. final ScaffoldState scaffold = Scaffold.of(context);
  182. final ModalRoute<dynamic>? parentRoute = ModalRoute.of(context);
  183. final bool hasDrawer = scaffold.hasDrawer;
  184. final bool hasEndDrawer = scaffold.hasEndDrawer;
  185. final bool canPop = parentRoute?.canPop ?? false;
  186. final bool useCloseButton =
  187. parentRoute is PageRoute<dynamic> && parentRoute.fullscreenDialog;
  188. final double toolbarHeight = widget.toolbarHeight ?? kToolbarHeight;
  189. IconThemeData overallIconTheme =
  190. widget.iconTheme ?? appBarTheme.iconTheme ?? theme.primaryIconTheme;
  191. IconThemeData actionsIconTheme = widget.actionsIconTheme ??
  192. appBarTheme.actionsIconTheme ??
  193. overallIconTheme;
  194. TextStyle? centerStyle = widget.textTheme?.titleLarge ??
  195. appBarTheme.toolbarTextStyle ??
  196. theme.primaryTextTheme.titleLarge;
  197. TextStyle? sideStyle = widget.textTheme?.bodyMedium ??
  198. appBarTheme.toolbarTextStyle ??
  199. theme.primaryTextTheme.bodyMedium;
  200. if (widget.toolbarOpacity != 1.0) {
  201. final double opacity =
  202. const Interval(0.25, 1.0, curve: Curves.fastOutSlowIn)
  203. .transform(widget.toolbarOpacity);
  204. if (centerStyle?.color != null)
  205. centerStyle = centerStyle!
  206. .copyWith(color: centerStyle.color!.withOpacity(opacity));
  207. if (sideStyle?.color != null)
  208. sideStyle =
  209. sideStyle!.copyWith(color: sideStyle.color!.withOpacity(opacity));
  210. overallIconTheme = overallIconTheme.copyWith(
  211. opacity: opacity * (overallIconTheme.opacity ?? 1.0));
  212. actionsIconTheme = actionsIconTheme.copyWith(
  213. opacity: opacity * (actionsIconTheme.opacity ?? 1.0));
  214. }
  215. Widget? leading = widget.leading;
  216. if (leading == null && widget.automaticallyImplyLeading) {
  217. if (hasDrawer) {
  218. leading = IconButton(
  219. icon: const Icon(Icons.menu),
  220. onPressed: _handleDrawerButton,
  221. tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
  222. );
  223. } else {
  224. if (canPop)
  225. leading = useCloseButton ? const CloseButton() : const BackButton();
  226. }
  227. }
  228. if (leading != null) {
  229. leading = ConstrainedBox(
  230. constraints: BoxConstraints.tightFor(
  231. width: widget.leadingWidth ?? _kLeadingWidth),
  232. child: leading,
  233. );
  234. }
  235. Widget? title = widget.title;
  236. if (title != null) {
  237. bool? namesRoute;
  238. switch (theme.platform) {
  239. case TargetPlatform.android:
  240. case TargetPlatform.fuchsia:
  241. case TargetPlatform.linux:
  242. case TargetPlatform.windows:
  243. namesRoute = true;
  244. break;
  245. case TargetPlatform.iOS:
  246. case TargetPlatform.macOS:
  247. break;
  248. }
  249. title = _WAppBarTitleBox(child: title);
  250. if (!widget.excludeHeaderSemantics) {
  251. title = Semantics(
  252. namesRoute: namesRoute,
  253. child: title,
  254. header: true,
  255. );
  256. }
  257. title = DefaultTextStyle(
  258. style: centerStyle!,
  259. softWrap: false,
  260. overflow: TextOverflow.ellipsis,
  261. child: title,
  262. );
  263. // Set maximum text scale factor to [_kMaxTitleTextScaleFactor] for the
  264. // title to keep the visual hierarchy the same even with larger font
  265. // sizes. To opt out, wrap the [title] widget in a [MediaQuery] widget
  266. // with [MediaQueryData.textScaleFactor] set to
  267. // `MediaQuery.textScaleFactorOf(context)`.
  268. final MediaQueryData mediaQueryData = MediaQuery.of(context);
  269. title = MediaQuery(
  270. data: mediaQueryData.copyWith(
  271. textScaleFactor: math.min(
  272. mediaQueryData.textScaleFactor,
  273. _kMaxTitleTextScaleFactor,
  274. ),
  275. ),
  276. child: title,
  277. );
  278. }
  279. Widget? actions;
  280. if (widget.actions != null && widget.actions!.isNotEmpty) {
  281. actions = Row(
  282. mainAxisSize: MainAxisSize.min,
  283. crossAxisAlignment: CrossAxisAlignment.stretch,
  284. children: widget.actions!,
  285. );
  286. } else if (hasEndDrawer) {
  287. actions = IconButton(
  288. icon: const Icon(Icons.menu),
  289. onPressed: _handleDrawerButtonEnd,
  290. tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
  291. );
  292. }
  293. // Allow the trailing actions to have their own theme if necessary.
  294. if (actions != null) {
  295. actions = IconTheme.merge(
  296. data: actionsIconTheme,
  297. child: actions,
  298. );
  299. }
  300. final Widget toolbar = NavigationToolbar(
  301. leading: leading,
  302. middle: title,
  303. trailing: actions,
  304. centerMiddle: widget._getEffectiveCenterTitle(theme)!,
  305. middleSpacing: widget.titleSpacing,
  306. );
  307. // If the toolbar is allocated less than toolbarHeight make it
  308. // appear to scroll upwards within its shrinking container.
  309. Widget appBar = ClipRect(
  310. child: CustomSingleChildLayout(
  311. delegate: _ToolbarContainerLayout(toolbarHeight),
  312. child: IconTheme.merge(
  313. data: overallIconTheme,
  314. child: DefaultTextStyle(
  315. style: sideStyle!,
  316. child: toolbar,
  317. ),
  318. ),
  319. ),
  320. );
  321. if (widget.bottom != null) {
  322. appBar = Column(
  323. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  324. children: <Widget>[
  325. Flexible(
  326. child: ConstrainedBox(
  327. constraints: BoxConstraints(maxHeight: toolbarHeight),
  328. child: appBar,
  329. ),
  330. ),
  331. if (widget.bottomOpacity == 1.0)
  332. widget.bottom!
  333. else
  334. Opacity(
  335. opacity: const Interval(0.25, 1.0, curve: Curves.fastOutSlowIn)
  336. .transform(widget.bottomOpacity),
  337. child: widget.bottom,
  338. ),
  339. ],
  340. );
  341. }
  342. // The padding applies to the toolbar and tabbar, not the flexible space.
  343. if (widget.primary) {
  344. appBar = SafeArea(
  345. bottom: false,
  346. top: true,
  347. child: appBar,
  348. );
  349. }
  350. appBar = Align(
  351. alignment: Alignment.topCenter,
  352. child: appBar,
  353. );
  354. if (widget.flexibleSpace != null) {
  355. appBar = Stack(
  356. fit: StackFit.passthrough,
  357. children: <Widget>[
  358. Semantics(
  359. sortKey: const OrdinalSortKey(1.0),
  360. explicitChildNodes: true,
  361. child: widget.flexibleSpace,
  362. ),
  363. Semantics(
  364. sortKey: const OrdinalSortKey(0.0),
  365. explicitChildNodes: true,
  366. // Creates a material widget to prevent the flexibleSpace from
  367. // obscuring the ink splashes produced by appBar children.
  368. child: Material(
  369. type: MaterialType.transparency,
  370. child: appBar,
  371. ),
  372. ),
  373. ],
  374. );
  375. }
  376. final Brightness brightness = widget.brightness ?? Brightness.light;
  377. final SystemUiOverlayStyle overlayStyle = brightness == Brightness.dark
  378. ? SystemUiOverlayStyle.light
  379. : SystemUiOverlayStyle.dark;
  380. return Semantics(
  381. container: true,
  382. child: AnnotatedRegion<SystemUiOverlayStyle>(
  383. value: overlayStyle,
  384. child: Material(
  385. color:
  386. widget.backgroundColor ?? appBarTheme.backgroundColor ?? theme.primaryColor,
  387. elevation:
  388. widget.elevation ?? appBarTheme.elevation ?? _defaultElevation,
  389. shadowColor: widget.shadowColor ??
  390. appBarTheme.shadowColor ??
  391. _defaultShadowColor,
  392. shape: widget.shape,
  393. child: Semantics(
  394. explicitChildNodes: true,
  395. child: appBar,
  396. ),
  397. ),
  398. ),
  399. );
  400. }
  401. }
  402. class _FloatingWAppBar extends StatefulWidget {
  403. const _FloatingWAppBar({Key? key, this.child}) : super(key: key);
  404. final Widget? child;
  405. @override
  406. _FloatingWAppBarState createState() => _FloatingWAppBarState();
  407. }
  408. // A wrapper for the widget created by _SliverWAppBarDelegate that starts and
  409. // stops the floating app bar's snap-into-view or snap-out-of-view animation.
  410. class _FloatingWAppBarState extends State<_FloatingWAppBar> {
  411. ScrollPosition? _position;
  412. @override
  413. void didChangeDependencies() {
  414. super.didChangeDependencies();
  415. if (_position != null)
  416. _position!.isScrollingNotifier.removeListener(_isScrollingListener);
  417. _position = Scrollable.of(context).position;
  418. if (_position != null)
  419. _position!.isScrollingNotifier.addListener(_isScrollingListener);
  420. }
  421. @override
  422. void dispose() {
  423. if (_position != null)
  424. _position!.isScrollingNotifier.removeListener(_isScrollingListener);
  425. super.dispose();
  426. }
  427. RenderSliverFloatingPersistentHeader? _headerRenderer() {
  428. return context
  429. .findAncestorRenderObjectOfType<RenderSliverFloatingPersistentHeader>();
  430. }
  431. void _isScrollingListener() {
  432. if (_position == null) return;
  433. // When a scroll stops, then maybe snap the appbar into view.
  434. // Similarly, when a scroll starts, then maybe stop the snap animation.
  435. final RenderSliverFloatingPersistentHeader? header = _headerRenderer();
  436. if (_position!.isScrollingNotifier.value)
  437. header?.maybeStopSnapAnimation(_position!.userScrollDirection);
  438. else
  439. header?.maybeStartSnapAnimation(_position!.userScrollDirection);
  440. }
  441. @override
  442. Widget build(BuildContext context) => widget.child!;
  443. }
  444. class _SliverWAppBarDelegate extends SliverPersistentHeaderDelegate {
  445. _SliverWAppBarDelegate({
  446. required this.leading,
  447. required this.automaticallyImplyLeading,
  448. required this.title,
  449. required this.actions,
  450. required this.flexibleSpace,
  451. required this.bottom,
  452. required this.elevation,
  453. required this.shadowColor,
  454. required this.forceElevated,
  455. required this.backgroundColor,
  456. required this.brightness,
  457. required this.iconTheme,
  458. required this.actionsIconTheme,
  459. required this.textTheme,
  460. required this.primary,
  461. required this.centerTitle,
  462. required this.excludeHeaderSemantics,
  463. required this.titleSpacing,
  464. required this.expandedHeight,
  465. required this.collapsedHeight,
  466. required this.topPadding,
  467. required this.floating,
  468. required this.pinned,
  469. required this.vsync,
  470. required this.snapConfiguration,
  471. required this.stretchConfiguration,
  472. required this.showOnScreenConfiguration,
  473. required this.shape,
  474. required this.toolbarHeight,
  475. required this.leadingWidth,
  476. }) : assert(primary! || topPadding == 0.0),
  477. assert(
  478. !floating! ||
  479. (snapConfiguration == null &&
  480. showOnScreenConfiguration == null),
  481. 'vsync cannot be null when snapConfiguration or showOnScreenConfiguration is not null, and floating is true',
  482. ),
  483. _bottomHeight = bottom?.preferredSize.height ?? 0.0;
  484. final Widget? leading;
  485. final bool automaticallyImplyLeading;
  486. final Widget? title;
  487. final List<Widget>? actions;
  488. final Widget? flexibleSpace;
  489. final PreferredSizeWidget? bottom;
  490. final double? elevation;
  491. final Color? shadowColor;
  492. final bool forceElevated;
  493. final Color? backgroundColor;
  494. final Brightness? brightness;
  495. final IconThemeData? iconTheme;
  496. final IconThemeData? actionsIconTheme;
  497. final TextTheme? textTheme;
  498. final bool? primary;
  499. final bool? centerTitle;
  500. final bool? excludeHeaderSemantics;
  501. final double? titleSpacing;
  502. final double? expandedHeight;
  503. final double? collapsedHeight;
  504. final double? topPadding;
  505. final bool? floating;
  506. final bool? pinned;
  507. final ShapeBorder? shape;
  508. final double? toolbarHeight;
  509. final double? leadingWidth;
  510. final double _bottomHeight;
  511. @override
  512. double get minExtent => collapsedHeight!;
  513. @override
  514. double get maxExtent => math.max(
  515. topPadding! +
  516. (expandedHeight ?? (toolbarHeight ?? kToolbarHeight) + _bottomHeight),
  517. minExtent);
  518. @override
  519. final TickerProvider vsync;
  520. @override
  521. final FloatingHeaderSnapConfiguration? snapConfiguration;
  522. @override
  523. final OverScrollHeaderStretchConfiguration? stretchConfiguration;
  524. @override
  525. final PersistentHeaderShowOnScreenConfiguration? showOnScreenConfiguration;
  526. @override
  527. Widget build(
  528. BuildContext context, double shrinkOffset, bool overlapsContent) {
  529. final double visibleMainHeight = maxExtent - shrinkOffset - topPadding!;
  530. final double extraToolbarHeight = math.max(
  531. minExtent -
  532. _bottomHeight -
  533. topPadding! -
  534. (toolbarHeight ?? kToolbarHeight),
  535. 0.0);
  536. final double visibleToolbarHeight =
  537. visibleMainHeight - _bottomHeight - extraToolbarHeight;
  538. final bool isPinnedWithOpacityFade =
  539. pinned! && floating! && bottom != null && extraToolbarHeight == 0.0;
  540. final double toolbarOpacity = !pinned! || isPinnedWithOpacityFade
  541. ? (visibleToolbarHeight / (toolbarHeight ?? kToolbarHeight))
  542. .clamp(0.0, 1.0)
  543. : 1.0;
  544. final Widget appBar = FlexibleSpaceBar.createSettings(
  545. minExtent: minExtent,
  546. maxExtent: maxExtent,
  547. currentExtent: math.max(minExtent, maxExtent - shrinkOffset),
  548. toolbarOpacity: toolbarOpacity,
  549. child: WAppBar(
  550. leading: leading,
  551. automaticallyImplyLeading: automaticallyImplyLeading,
  552. title: title,
  553. actions: actions,
  554. flexibleSpace:
  555. (title == null && flexibleSpace != null && !excludeHeaderSemantics!)
  556. ? Semantics(child: flexibleSpace, header: true)
  557. : flexibleSpace,
  558. bottom: bottom,
  559. elevation: forceElevated ||
  560. overlapsContent ||
  561. (pinned! && shrinkOffset > maxExtent - minExtent)
  562. ? elevation ?? 4.0
  563. : 0.0,
  564. shadowColor: shadowColor,
  565. backgroundColor: backgroundColor,
  566. brightness: brightness,
  567. iconTheme: iconTheme,
  568. actionsIconTheme: actionsIconTheme,
  569. textTheme: textTheme,
  570. primary: primary!,
  571. centerTitle: centerTitle!,
  572. excludeHeaderSemantics: excludeHeaderSemantics!,
  573. titleSpacing: titleSpacing!,
  574. shape: shape,
  575. toolbarOpacity: toolbarOpacity,
  576. bottomOpacity: pinned == true
  577. ? 1.0
  578. : (visibleMainHeight / _bottomHeight).clamp(0.0, 1.0),
  579. toolbarHeight: toolbarHeight,
  580. leadingWidth: leadingWidth,
  581. ),
  582. );
  583. return floating == true ? _FloatingWAppBar(child: appBar) : appBar;
  584. }
  585. @override
  586. bool shouldRebuild(covariant _SliverWAppBarDelegate oldDelegate) {
  587. return leading != oldDelegate.leading ||
  588. automaticallyImplyLeading != oldDelegate.automaticallyImplyLeading ||
  589. title != oldDelegate.title ||
  590. actions != oldDelegate.actions ||
  591. flexibleSpace != oldDelegate.flexibleSpace ||
  592. bottom != oldDelegate.bottom ||
  593. _bottomHeight != oldDelegate._bottomHeight ||
  594. elevation != oldDelegate.elevation ||
  595. shadowColor != oldDelegate.shadowColor ||
  596. backgroundColor != oldDelegate.backgroundColor ||
  597. brightness != oldDelegate.brightness ||
  598. iconTheme != oldDelegate.iconTheme ||
  599. actionsIconTheme != oldDelegate.actionsIconTheme ||
  600. textTheme != oldDelegate.textTheme ||
  601. primary != oldDelegate.primary ||
  602. centerTitle != oldDelegate.centerTitle ||
  603. titleSpacing != oldDelegate.titleSpacing ||
  604. expandedHeight != oldDelegate.expandedHeight ||
  605. topPadding != oldDelegate.topPadding ||
  606. pinned != oldDelegate.pinned ||
  607. floating != oldDelegate.floating ||
  608. vsync != oldDelegate.vsync ||
  609. snapConfiguration != oldDelegate.snapConfiguration ||
  610. stretchConfiguration != oldDelegate.stretchConfiguration ||
  611. showOnScreenConfiguration != oldDelegate.showOnScreenConfiguration ||
  612. forceElevated != oldDelegate.forceElevated ||
  613. toolbarHeight != oldDelegate.toolbarHeight ||
  614. leadingWidth != leadingWidth;
  615. }
  616. @override
  617. String toString() {
  618. return '${describeIdentity(this)}(topPadding: ${topPadding!.toStringAsFixed(1)}, bottomHeight: ${_bottomHeight.toStringAsFixed(1)}, ...)';
  619. }
  620. }
  621. /// A material design app bar that integrates with a [CustomScrollView].
  622. ///
  623. /// An app bar consists of a toolbar and potentially other widgets, such as a
  624. /// [TabBar] and a [FlexibleSpaceBar]. App bars typically expose one or more
  625. /// common actions with [IconButton]s which are optionally followed by a
  626. /// [PopupMenuButton] for less common operations.
  627. ///
  628. /// {@youtube 560 315 https://www.youtube.com/watch?v=R9C5KMJKluE}
  629. ///
  630. /// Sliver app bars are typically used as the first child of a
  631. /// [CustomScrollView], which lets the app bar integrate with the scroll view so
  632. /// that it can vary in height according to the scroll offset or float above the
  633. /// other content in the scroll view. For a fixed-height app bar at the top of
  634. /// the screen see [WAppBar], which is used in the [Scaffold.appBar] slot.
  635. ///
  636. /// The WAppBar displays the toolbar widgets, [leading], [title], and
  637. /// [actions], above the [bottom] (if any). If a [flexibleSpace] widget is
  638. /// specified then it is stacked behind the toolbar and the bottom widget.
  639. ///
  640. /// {@tool snippet}
  641. ///
  642. /// This is an example that could be included in a [CustomScrollView]'s
  643. /// [CustomScrollView.slivers] list:
  644. ///
  645. /// ```dart
  646. /// SliverWAppBar(
  647. /// expandedHeight: 150.0,
  648. /// flexibleSpace: const FlexibleSpaceBar(
  649. /// title: Text('Available seats'),
  650. /// ),
  651. /// actions: <Widget>[
  652. /// IconButton(
  653. /// icon: const Icon(Icons.add_circle),
  654. /// tooltip: 'Add new entry',
  655. /// onPressed: () { /* ... */ },
  656. /// ),
  657. /// ]
  658. /// )
  659. /// ```
  660. /// {@end-tool}
  661. ///
  662. /// ## Animated Examples
  663. ///
  664. /// The following animations show how app bars with different configurations
  665. /// behave when a user scrolls up and then down again.
  666. ///
  667. /// * App bar with [floating]: false, [pinned]: false, [snap]: false:
  668. /// {@animation 476 400 https://flutter.github.io/assets-for-api-docs/assets/material/app_bar.mp4}
  669. ///
  670. /// * App bar with [floating]: true, [pinned]: false, [snap]: false:
  671. /// {@animation 476 400 https://flutter.github.io/assets-for-api-docs/assets/material/app_bar_floating.mp4}
  672. ///
  673. /// * App bar with [floating]: true, [pinned]: false, [snap]: true:
  674. /// {@animation 476 400 https://flutter.github.io/assets-for-api-docs/assets/material/app_bar_floating_snap.mp4}
  675. ///
  676. /// * App bar with [floating]: true, [pinned]: true, [snap]: false:
  677. /// {@animation 476 400 https://flutter.github.io/assets-for-api-docs/assets/material/app_bar_pinned_floating.mp4}
  678. ///
  679. /// * App bar with [floating]: true, [pinned]: true, [snap]: true:
  680. /// {@animation 476 400 https://flutter.github.io/assets-for-api-docs/assets/material/app_bar_pinned_floating_snap.mp4}
  681. ///
  682. /// * App bar with [floating]: false, [pinned]: true, [snap]: false:
  683. /// {@animation 476 400 https://flutter.github.io/assets-for-api-docs/assets/material/app_bar_pinned.mp4}
  684. ///
  685. /// The property [snap] can only be set to true if [floating] is also true.
  686. ///
  687. /// See also:
  688. ///
  689. /// * [CustomScrollView], which integrates the [SliverWAppBar] into its
  690. /// scrolling.
  691. /// * [WAppBar], which is a fixed-height app bar for use in [Scaffold.appBar].
  692. /// * [TabBar], which is typically placed in the [bottom] slot of the [WAppBar]
  693. /// if the screen has multiple pages arranged in tabs.
  694. /// * [IconButton], which is used with [actions] to show buttons on the app bar.
  695. /// * [PopupMenuButton], to show a popup menu on the app bar, via [actions].
  696. /// * [FlexibleSpaceBar], which is used with [flexibleSpace] when the app bar
  697. /// can expand and collapse.
  698. /// * <https://material.io/design/components/app-bars-top.html>
  699. class SliverWAppBar extends StatefulWidget {
  700. /// Creates a material design app bar that can be placed in a [CustomScrollView].
  701. ///
  702. /// The arguments [forceElevated], [primary], [floating], [pinned], [snap]
  703. /// and [automaticallyImplyLeading] must not be null.
  704. const SliverWAppBar({
  705. Key? key,
  706. this.leading,
  707. this.automaticallyImplyLeading = true,
  708. this.title,
  709. this.actions,
  710. this.flexibleSpace,
  711. this.bottom,
  712. this.elevation,
  713. this.shadowColor,
  714. this.forceElevated = false,
  715. this.backgroundColor,
  716. this.brightness,
  717. this.iconTheme,
  718. this.actionsIconTheme,
  719. this.textTheme,
  720. this.primary = true,
  721. this.centerTitle = true,
  722. this.excludeHeaderSemantics = false,
  723. this.titleSpacing = NavigationToolbar.kMiddleSpacing,
  724. this.collapsedHeight,
  725. this.expandedHeight,
  726. this.floating = false,
  727. this.pinned = false,
  728. this.snap = false,
  729. this.stretch = false,
  730. this.stretchTriggerOffset = 100.0,
  731. this.onStretchTrigger,
  732. this.shape,
  733. this.toolbarHeight = kToolbarHeight,
  734. this.leadingWidth,
  735. }) : assert(floating == true || snap == false,
  736. 'The "snap" argument only makes sense for floating app bars.'),
  737. // assert(collapsedHeight == null || collapsedHeight > toolbarHeight!,
  738. // 'The "collapsedHeight" argument has to be larger than [toolbarHeight].'),
  739. super(key: key);
  740. /// A widget to display before the [title].
  741. ///
  742. /// If this is null and [automaticallyImplyLeading] is set to true, the [WAppBar] will
  743. /// imply an appropriate widget. For example, if the [WAppBar] is in a [Scaffold]
  744. /// that also has a [Drawer], the [Scaffold] will fill this widget with an
  745. /// [IconButton] that opens the drawer. If there's no [Drawer] and the parent
  746. /// [Navigator] can go back, the [WAppBar] will use a [BackButton] that calls
  747. /// [Navigator.maybePop].
  748. final Widget? leading;
  749. /// Controls whether we should try to imply the leading widget if null.
  750. ///
  751. /// If true and [leading] is null, automatically try to deduce what the leading
  752. /// widget should be. If false and [leading] is null, leading space is given to [title].
  753. /// If leading widget is not null, this parameter has no effect.
  754. final bool? automaticallyImplyLeading;
  755. /// The primary widget displayed in the app bar.
  756. ///
  757. /// Typically a [Text] widget containing a description of the current contents
  758. /// of the app.
  759. final Widget? title;
  760. /// Widgets to display after the [title] widget.
  761. ///
  762. /// Typically these widgets are [IconButton]s representing common operations.
  763. /// For less common operations, consider using a [PopupMenuButton] as the
  764. /// last action.
  765. ///
  766. /// {@tool snippet}
  767. ///
  768. /// ```dart
  769. /// Scaffold(
  770. /// body: CustomScrollView(
  771. /// primary: true,
  772. /// slivers: <Widget>[
  773. /// SliverWAppBar(
  774. /// title: Text('Hello World'),
  775. /// actions: <Widget>[
  776. /// IconButton(
  777. /// icon: Icon(Icons.shopping_cart),
  778. /// tooltip: 'Open shopping cart',
  779. /// onPressed: () {
  780. /// // handle the press
  781. /// },
  782. /// ),
  783. /// ],
  784. /// ),
  785. /// // ...rest of body...
  786. /// ],
  787. /// ),
  788. /// )
  789. /// ```
  790. /// {@end-tool}
  791. final List<Widget>? actions;
  792. /// This widget is stacked behind the toolbar and the tab bar. It's height will
  793. /// be the same as the app bar's overall height.
  794. ///
  795. /// When using [SliverWAppBar.flexibleSpace], the [SliverWAppBar.expandedHeight]
  796. /// must be large enough to accommodate the [SliverWAppBar.flexibleSpace] widget.
  797. ///
  798. /// Typically a [FlexibleSpaceBar]. See [FlexibleSpaceBar] for details.
  799. final Widget? flexibleSpace;
  800. /// This widget appears across the bottom of the app bar.
  801. ///
  802. /// Typically a [TabBar]. Only widgets that implement [PreferredSizeWidget] can
  803. /// be used at the bottom of an app bar.
  804. ///
  805. /// See also:
  806. ///
  807. /// * [PreferredSize], which can be used to give an arbitrary widget a preferred size.
  808. final PreferredSizeWidget? bottom;
  809. /// The z-coordinate at which to place this app bar when it is above other
  810. /// content. This controls the size of the shadow below the app bar.
  811. ///
  812. /// If this property is null, then [AppBarTheme.elevation] of
  813. /// [ThemeData.appBarTheme] is used, if that is also null, the default value
  814. /// is 4.
  815. ///
  816. /// If [forceElevated] is false, the elevation is ignored when the app bar has
  817. /// no content underneath it. For example, if the app bar is [pinned] but no
  818. /// content is scrolled under it, or if it scrolls with the content, then no
  819. /// shadow is drawn, regardless of the value of [elevation].
  820. final double? elevation;
  821. /// The color to paint the shadow below the app bar. Typically this should be set
  822. /// along with [elevation].
  823. ///
  824. /// If this property is null, then [AppBarTheme.shadowColor] of
  825. /// [ThemeData.appBarTheme] is used, if that is also null, the default value
  826. /// is fully opaque black.
  827. final Color? shadowColor;
  828. /// Whether to show the shadow appropriate for the [elevation] even if the
  829. /// content is not scrolled under the [WAppBar].
  830. ///
  831. /// Defaults to false, meaning that the [elevation] is only applied when the
  832. /// [WAppBar] is being displayed over content that is scrolled under it.
  833. ///
  834. /// When set to true, the [elevation] is applied regardless.
  835. ///
  836. /// Ignored when [elevation] is zero.
  837. final bool? forceElevated;
  838. /// The color to use for the app bar's material. Typically this should be set
  839. /// along with [brightness], [iconTheme], [textTheme].
  840. ///
  841. /// If this property is null, then [AppBarTheme.backgroundColor] of
  842. /// [ThemeData.appBarTheme] is used. If that is also null, then
  843. /// [ThemeData.primaryColor] is used.
  844. final Color? backgroundColor;
  845. /// The brightness of the app bar's material. Typically this is set along
  846. /// with [backgroundColor], [iconTheme], [textTheme].
  847. ///
  848. final Brightness? brightness;
  849. /// The color, opacity, and size to use for app bar icons. Typically this
  850. /// is set along with [backgroundColor], [brightness], [textTheme].
  851. ///
  852. /// If this property is null, then [AppBarTheme.iconTheme] of
  853. /// [ThemeData.appBarTheme] is used, if that is also null, then
  854. /// [ThemeData.primaryIconTheme] is used.
  855. final IconThemeData? iconTheme;
  856. /// The color, opacity, and size to use for trailing app bar icons. This
  857. /// should only be used when the trailing icons should be themed differently
  858. /// than the leading icons.
  859. ///
  860. /// If this property is null, then [AppBarTheme.actionsIconTheme] of
  861. /// [ThemeData.appBarTheme] is used, if that is also null, then this falls
  862. /// back to [iconTheme].
  863. final IconThemeData? actionsIconTheme;
  864. /// The typographic styles to use for text in the app bar. Typically this is
  865. /// set along with [brightness] [backgroundColor], [iconTheme].
  866. ///
  867. /// If this property is null, then [AppBarTheme.textTheme] of
  868. /// [ThemeData.appBarTheme] is used, if that is also null, then
  869. /// [ThemeData.primaryTextTheme] is used.
  870. final TextTheme? textTheme;
  871. /// Whether this app bar is being displayed at the top of the screen.
  872. ///
  873. /// If this is true, the top padding specified by the [MediaQuery] will be
  874. /// added to the top of the toolbar.
  875. final bool? primary;
  876. /// Whether the title should be centered.
  877. ///
  878. /// Defaults to being adapted to the current [TargetPlatform].
  879. final bool? centerTitle;
  880. /// Whether the title should be wrapped with header [Semantics].
  881. ///
  882. /// Defaults to false.
  883. final bool? excludeHeaderSemantics;
  884. /// The spacing around [title] content on the horizontal axis. This spacing is
  885. /// applied even if there is no [leading] content or [actions]. If you want
  886. /// [title] to take all the space available, set this value to 0.0.
  887. ///
  888. /// Defaults to [NavigationToolbar.kMiddleSpacing].
  889. final double? titleSpacing;
  890. /// Defines the height of the app bar when it is collapsed.
  891. ///
  892. /// By default, the collapsed height is [toolbarHeight]. If [bottom] widget is
  893. /// specified, then its height from [PreferredSizeWidget.preferredSize] is
  894. /// added to the height. If [primary] is true, then the [MediaQuery] top
  895. /// padding, [EdgeInsets.top] of [MediaQueryData.padding], is added as well.
  896. ///
  897. /// If [pinned] and [floating] are true, with [bottom] set, the default
  898. /// collapsed height is only the height of [PreferredSizeWidget.preferredSize]
  899. /// with the [MediaQuery] top padding.
  900. final double? collapsedHeight;
  901. /// The size of the app bar when it is fully expanded.
  902. ///
  903. /// By default, the total height of the toolbar and the bottom widget (if
  904. /// any). If a [flexibleSpace] widget is specified this height should be big
  905. /// enough to accommodate whatever that widget contains.
  906. ///
  907. /// This does not include the status bar height (which will be automatically
  908. /// included if [primary] is true).
  909. final double? expandedHeight;
  910. /// Whether the app bar should become visible as soon as the user scrolls
  911. /// towards the app bar.
  912. ///
  913. /// Otherwise, the user will need to scroll near the top of the scroll view to
  914. /// reveal the app bar.
  915. ///
  916. /// If [snap] is true then a scroll that exposes the app bar will trigger an
  917. /// animation that slides the entire app bar into view. Similarly if a scroll
  918. /// dismisses the app bar, the animation will slide it completely out of view.
  919. ///
  920. /// ## Animated Examples
  921. ///
  922. /// The following animations show how the app bar changes its scrolling
  923. /// behavior based on the value of this property.
  924. ///
  925. /// * App bar with [floating] set to false:
  926. /// {@animation 476 400 https://flutter.github.io/assets-for-api-docs/assets/material/app_bar.mp4}
  927. /// * App bar with [floating] set to true:
  928. /// {@animation 476 400 https://flutter.github.io/assets-for-api-docs/assets/material/app_bar_floating.mp4}
  929. ///
  930. /// See also:
  931. ///
  932. /// * [SliverWAppBar] for more animated examples of how this property changes the
  933. /// behavior of the app bar in combination with [pinned] and [snap].
  934. final bool? floating;
  935. /// Whether the app bar should remain visible at the start of the scroll view.
  936. ///
  937. /// The app bar can still expand and contract as the user scrolls, but it will
  938. /// remain visible rather than being scrolled out of view.
  939. ///
  940. /// ## Animated Examples
  941. ///
  942. /// The following animations show how the app bar changes its scrolling
  943. /// behavior based on the value of this property.
  944. ///
  945. /// * App bar with [pinned] set to false:
  946. /// {@animation 476 400 https://flutter.github.io/assets-for-api-docs/assets/material/app_bar.mp4}
  947. /// * App bar with [pinned] set to true:
  948. /// {@animation 476 400 https://flutter.github.io/assets-for-api-docs/assets/material/app_bar_pinned.mp4}
  949. ///
  950. /// See also:
  951. ///
  952. /// * [SliverWAppBar] for more animated examples of how this property changes the
  953. /// behavior of the app bar in combination with [floating].
  954. final bool? pinned;
  955. /// The material's shape as well as its shadow.
  956. ///
  957. /// A shadow is only displayed if the [elevation] is greater than zero.
  958. final ShapeBorder? shape;
  959. /// If [snap] and [floating] are true then the floating app bar will "snap"
  960. /// into view.
  961. ///
  962. /// If [snap] is true then a scroll that exposes the floating app bar will
  963. /// trigger an animation that slides the entire app bar into view. Similarly
  964. /// if a scroll dismisses the app bar, the animation will slide the app bar
  965. /// completely out of view. Additionally, setting [snap] to true will fully
  966. /// expand the floating app bar when the framework tries to reveal the
  967. /// contents of the app bar by calling [RenderObject.showOnScreen]. For
  968. /// example, when a [TextField] in the floating app bar gains focus, if [snap]
  969. /// is true, the framework will always fully expand the floating app bar, in
  970. /// order to reveal the focused [TextField].
  971. ///
  972. /// Snapping only applies when the app bar is floating, not when the app bar
  973. /// appears at the top of its scroll view.
  974. ///
  975. /// ## Animated Examples
  976. ///
  977. /// The following animations show how the app bar changes its scrolling
  978. /// behavior based on the value of this property.
  979. ///
  980. /// * App bar with [snap] set to false:
  981. /// {@animation 476 400 https://flutter.github.io/assets-for-api-docs/assets/material/app_bar_floating.mp4}
  982. /// * App bar with [snap] set to true:
  983. /// {@animation 476 400 https://flutter.github.io/assets-for-api-docs/assets/material/app_bar_floating_snap.mp4}
  984. ///
  985. /// See also:
  986. ///
  987. /// * [SliverWAppBar] for more animated examples of how this property changes the
  988. /// behavior of the app bar in combination with [pinned] and [floating].
  989. final bool? snap;
  990. /// Whether the app bar should stretch to fill the over-scroll area.
  991. ///
  992. /// The app bar can still expand and contract as the user scrolls, but it will
  993. /// also stretch when the user over-scrolls.
  994. final bool? stretch;
  995. /// The offset of overscroll required to activate [onStretchTrigger].
  996. ///
  997. /// This defaults to 100.0.
  998. final double? stretchTriggerOffset;
  999. /// The callback function to be executed when a user over-scrolls to the
  1000. /// offset specified by [stretchTriggerOffset].
  1001. final AsyncCallback? onStretchTrigger;
  1002. /// Defines the height of the toolbar component of an [WAppBar].
  1003. ///
  1004. /// By default, the value of `toolbarHeight` is [kToolbarHeight].
  1005. final double? toolbarHeight;
  1006. /// Defines the width of [leading] widget.
  1007. ///
  1008. /// By default, the value of `leadingWidth` is 56.0.
  1009. final double? leadingWidth;
  1010. @override
  1011. _SliverWAppBarState createState() => _SliverWAppBarState();
  1012. }
  1013. // This class is only Stateful because it owns the TickerProvider used
  1014. // by the floating appbar snap animation (via FloatingHeaderSnapConfiguration).
  1015. class _SliverWAppBarState extends State<SliverWAppBar>
  1016. with TickerProviderStateMixin {
  1017. FloatingHeaderSnapConfiguration? _snapConfiguration;
  1018. OverScrollHeaderStretchConfiguration? _stretchConfiguration;
  1019. PersistentHeaderShowOnScreenConfiguration? _showOnScreenConfiguration;
  1020. void _updateSnapConfiguration() {
  1021. if (widget.snap == true && widget.floating == true) {
  1022. _snapConfiguration = FloatingHeaderSnapConfiguration(
  1023. curve: Curves.easeOut,
  1024. duration: const Duration(milliseconds: 200),
  1025. );
  1026. } else {
  1027. _snapConfiguration = null;
  1028. }
  1029. _showOnScreenConfiguration = widget.floating == true && widget.snap == true
  1030. ? const PersistentHeaderShowOnScreenConfiguration(
  1031. minShowOnScreenExtent: double.infinity)
  1032. : null;
  1033. }
  1034. void _updateStretchConfiguration() {
  1035. if (widget.stretch == true) {
  1036. _stretchConfiguration = OverScrollHeaderStretchConfiguration(
  1037. stretchTriggerOffset: widget.stretchTriggerOffset!,
  1038. onStretchTrigger: widget.onStretchTrigger,
  1039. );
  1040. } else {
  1041. _stretchConfiguration = null;
  1042. }
  1043. }
  1044. @override
  1045. void initState() {
  1046. super.initState();
  1047. _updateSnapConfiguration();
  1048. _updateStretchConfiguration();
  1049. }
  1050. @override
  1051. void didUpdateWidget(SliverWAppBar oldWidget) {
  1052. super.didUpdateWidget(oldWidget);
  1053. if (widget.snap != oldWidget.snap || widget.floating != oldWidget.floating)
  1054. _updateSnapConfiguration();
  1055. if (widget.stretch != oldWidget.stretch) _updateStretchConfiguration();
  1056. }
  1057. @override
  1058. Widget build(BuildContext context) {
  1059. assert(!widget.primary! || debugCheckHasMediaQuery(context));
  1060. final double bottomHeight = widget.bottom?.preferredSize.height ?? 0.0;
  1061. final double topPadding =
  1062. widget.primary == true ? MediaQuery.of(context).padding.top : 0.0;
  1063. final double collapsedHeight = (widget.pinned == true &&
  1064. widget.floating == true &&
  1065. widget.bottom != null)
  1066. ? (widget.collapsedHeight ?? 0.0) + bottomHeight + topPadding
  1067. : (widget.collapsedHeight!) + bottomHeight + topPadding;
  1068. return MediaQuery.removePadding(
  1069. context: context,
  1070. removeBottom: true,
  1071. child: SliverPersistentHeader(
  1072. floating: widget.floating!,
  1073. pinned: widget.pinned!,
  1074. delegate: _SliverWAppBarDelegate(
  1075. vsync: this,
  1076. leading: widget.leading,
  1077. automaticallyImplyLeading: widget.automaticallyImplyLeading!,
  1078. title: widget.title,
  1079. actions: widget.actions,
  1080. flexibleSpace: widget.flexibleSpace,
  1081. bottom: widget.bottom,
  1082. elevation: widget.elevation,
  1083. shadowColor: widget.shadowColor,
  1084. forceElevated: widget.forceElevated!,
  1085. backgroundColor: widget.backgroundColor!,
  1086. brightness: widget.brightness,
  1087. iconTheme: widget.iconTheme,
  1088. actionsIconTheme: widget.actionsIconTheme,
  1089. textTheme: widget.textTheme,
  1090. primary: widget.primary,
  1091. centerTitle: widget.centerTitle,
  1092. excludeHeaderSemantics: widget.excludeHeaderSemantics,
  1093. titleSpacing: widget.titleSpacing,
  1094. expandedHeight: widget.expandedHeight,
  1095. collapsedHeight: collapsedHeight,
  1096. topPadding: topPadding,
  1097. floating: widget.floating,
  1098. pinned: widget.pinned,
  1099. shape: widget.shape,
  1100. snapConfiguration: _snapConfiguration,
  1101. stretchConfiguration: _stretchConfiguration,
  1102. showOnScreenConfiguration: _showOnScreenConfiguration,
  1103. toolbarHeight: widget.toolbarHeight,
  1104. leadingWidth: widget.leadingWidth,
  1105. ),
  1106. ),
  1107. );
  1108. }
  1109. }
  1110. // Layout the WAppBar's title with unconstrained height, vertically
  1111. // center it within its (NavigationToolbar) parent, and allow the
  1112. // parent to constrain the title's actual height.
  1113. class _WAppBarTitleBox extends SingleChildRenderObjectWidget {
  1114. const _WAppBarTitleBox({Key? key, required Widget child})
  1115. : super(key: key, child: child);
  1116. @override
  1117. _RenderWAppBarTitleBox createRenderObject(BuildContext context) {
  1118. return _RenderWAppBarTitleBox(
  1119. textDirection: Directionality.of(context),
  1120. );
  1121. }
  1122. @override
  1123. void updateRenderObject(
  1124. BuildContext context, _RenderWAppBarTitleBox renderObject) {
  1125. renderObject.textDirection = Directionality.of(context);
  1126. }
  1127. }
  1128. class _RenderWAppBarTitleBox extends RenderAligningShiftedBox {
  1129. _RenderWAppBarTitleBox({
  1130. RenderBox? child,
  1131. TextDirection? textDirection,
  1132. }) : super(
  1133. child: child,
  1134. alignment: Alignment.center,
  1135. textDirection: textDirection);
  1136. @override
  1137. void performLayout() {
  1138. final BoxConstraints constraints = this.constraints;
  1139. final BoxConstraints innerConstraints =
  1140. constraints.copyWith(maxHeight: double.infinity);
  1141. child!.layout(innerConstraints, parentUsesSize: true);
  1142. size = constraints.constrain(child!.size);
  1143. alignChild();
  1144. }
  1145. }