flexible_space_bar.dart 15.1 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math' as math;
6
import 'dart:ui' as ui;
7

8
import 'package:flutter/foundation.dart' show clampDouble;
9 10
import 'package:flutter/widgets.dart';

11
import 'colors.dart';
12 13 14
import 'constants.dart';
import 'theme.dart';

15
/// The collapsing effect while the space bar collapses from its full size.
16 17 18 19 20 21 22 23 24 25 26
enum CollapseMode {
  /// The background widget will scroll in a parallax fashion.
  parallax,

  /// The background widget pin in place until it reaches the min extent.
  pin,

  /// The background widget will act as normal with no collapsing effect.
  none,
}

27 28 29 30 31 32 33 34 35 36 37 38
/// The stretching effect while the space bar stretches beyond its full size.
enum StretchMode {
  /// The background widget will expand to fill the extra space.
  zoomBackground,

  /// The background will blur using a [ImageFilter.blur] effect.
  blurBackground,

  /// The title will fade away as the user over-scrolls.
  fadeTitle,
}

39
/// The part of a Material Design [AppBar] that expands, collapses, and
40
/// stretches.
41
///
42 43
/// {@youtube 560 315 https://www.youtube.com/watch?v=mSc7qFzxHDw}
///
44
/// Most commonly used in the [SliverAppBar.flexibleSpace] field, a flexible
45 46
/// space bar expands and contracts as the app scrolls so that the [AppBar]
/// reaches from the top of the app to the top of the scrolling contents of the
47 48 49 50
/// app. When using [SliverAppBar.flexibleSpace], the [SliverAppBar.expandedHeight]
/// must be large enough to accommodate the [SliverAppBar.flexibleSpace] widget.
///
/// Furthermore is included functionality for stretch behavior. When
51 52
/// [SliverAppBar.stretch] is true, and your [ScrollPhysics] allow for
/// overscroll, this space will stretch with the overscroll.
53
///
54 55 56
/// The widget that sizes the [AppBar] must wrap it in the widget returned by
/// [FlexibleSpaceBar.createSettings], to convey sizing information down to the
/// [FlexibleSpaceBar].
57
///
58
/// {@tool dartpad}
59 60 61 62 63 64 65
/// This sample application demonstrates the different features of the
/// [FlexibleSpaceBar] when used in a [SliverAppBar]. This app bar is configured
/// to stretch into the overscroll space, and uses the
/// [FlexibleSpaceBar.stretchModes] to apply `fadeTitle`, `blurBackground` and
/// `zoomBackground`. The app bar also makes use of [CollapseMode.parallax] by
/// default.
///
66
/// ** See code in examples/api/lib/material/flexible_space_bar/flexible_space_bar.0.dart **
67 68
/// {@end-tool}
///
69
/// See also:
Ian Hickson's avatar
Ian Hickson committed
70
///
71 72
///  * [SliverAppBar], which implements the expanding and contracting.
///  * [AppBar], which is used by [SliverAppBar].
73
///  * <https://material.io/design/components/app-bars-top.html#behavior>
74
class FlexibleSpaceBar extends StatefulWidget {
75 76
  /// Creates a flexible space bar.
  ///
77
  /// Most commonly used in the [AppBar.flexibleSpace] field.
78
  const FlexibleSpaceBar({
79
    super.key,
80 81
    this.title,
    this.background,
82
    this.centerTitle,
83
    this.titlePadding,
84
    this.collapseMode = CollapseMode.parallax,
85
    this.stretchModes = const <StretchMode>[StretchMode.zoomBackground],
86
    this.expandedTitleScale = 1.5,
87
  }) : assert(expandedTitleScale >= 1);
88

89 90 91
  /// The primary contents of the flexible space bar when expanded.
  ///
  /// Typically a [Text] widget.
92
  final Widget? title;
93 94 95

  /// Shown behind the [title] when expanded.
  ///
96
  /// Typically an [Image] widget with [Image.fit] set to [BoxFit.cover].
97
  final Widget? background;
98

99 100
  /// Whether the title should be centered.
  ///
101
  /// By default this property is true if the current target platform
102
  /// is [TargetPlatform.iOS] or [TargetPlatform.macOS], false otherwise.
103
  final bool? centerTitle;
104

105 106 107 108 109
  /// Collapse effect while scrolling.
  ///
  /// Defaults to [CollapseMode.parallax].
  final CollapseMode collapseMode;

110
  /// Stretch effect while over-scrolling.
111 112 113 114
  ///
  /// Defaults to include [StretchMode.zoomBackground].
  final List<StretchMode> stretchModes;

115 116 117 118
  /// Defines how far the [title] is inset from either the widget's
  /// bottom-left or its center.
  ///
  /// Typically this property is used to adjust how far the title is
119
  /// inset from the bottom-left and it is specified along with
120 121 122 123
  /// [centerTitle] false.
  ///
  /// By default the value of this property is
  /// `EdgeInsetsDirectional.only(start: 72, bottom: 16)` if the title is
124
  /// not centered, `EdgeInsetsDirectional.only(start: 0, bottom: 16)` otherwise.
125
  final EdgeInsetsGeometry? titlePadding;
126

127 128 129 130 131 132 133 134
  /// Defines how much the title is scaled when the FlexibleSpaceBar is expanded
  /// due to the user scrolling downwards. The title is scaled uniformly on the
  /// x and y axes while maintaining its bottom-left position (bottom-center if
  /// [centerTitle] is true).
  ///
  /// Defaults to 1.5 and must be greater than 1.
  final double expandedTitleScale;

135 136 137 138
  /// Wraps a widget that contains an [AppBar] to convey sizing information down
  /// to the [FlexibleSpaceBar].
  ///
  /// Used by [Scaffold] and [SliverAppBar].
139 140 141 142 143 144 145
  ///
  /// `toolbarOpacity` affects how transparent the text within the toolbar
  /// appears. `minExtent` sets the minimum height of the resulting
  /// [FlexibleSpaceBar] when fully collapsed. `maxExtent` sets the maximum
  /// height of the resulting [FlexibleSpaceBar] when fully expanded.
  /// `currentExtent` sets the scale of the [FlexibleSpaceBar.background] and
  /// [FlexibleSpaceBar.title] widgets of [FlexibleSpaceBar] upon
146
  /// initialization. `scrolledUnder` is true if the [FlexibleSpaceBar]
147 148
  /// overlaps the app's primary scrollable, false if it does not, and null
  /// if the caller has not determined as much.
149 150
  /// See also:
  ///
151 152
  ///  * [FlexibleSpaceBarSettings] which creates a settings object that can be
  ///    used to specify these settings to a [FlexibleSpaceBar].
153
  static Widget createSettings({
154 155 156
    double? toolbarOpacity,
    double? minExtent,
    double? maxExtent,
157
    bool? isScrolledUnder,
158 159
    required double currentExtent,
    required Widget child,
160
  }) {
161
    return FlexibleSpaceBarSettings(
162 163 164
      toolbarOpacity: toolbarOpacity ?? 1.0,
      minExtent: minExtent ?? currentExtent,
      maxExtent: maxExtent ?? currentExtent,
165
      isScrolledUnder: isScrolledUnder,
166 167 168 169 170
      currentExtent: currentExtent,
      child: child,
    );
  }

171
  @override
172
  State<FlexibleSpaceBar> createState() => _FlexibleSpaceBarState();
173 174 175
}

class _FlexibleSpaceBarState extends State<FlexibleSpaceBar> {
176
  bool _getEffectiveCenterTitle(ThemeData theme) {
177
    if (widget.centerTitle != null) {
178
      return widget.centerTitle!;
179
    }
180 181
    switch (theme.platform) {
      case TargetPlatform.android:
182
      case TargetPlatform.fuchsia:
183 184
      case TargetPlatform.linux:
      case TargetPlatform.windows:
185 186
        return false;
      case TargetPlatform.iOS:
187
      case TargetPlatform.macOS:
188 189 190 191
        return true;
    }
  }

192
  Alignment _getTitleAlignment(bool effectiveCenterTitle) {
193
    if (effectiveCenterTitle) {
194
      return Alignment.bottomCenter;
195
    }
196
    final TextDirection textDirection = Directionality.of(context);
197 198
    switch (textDirection) {
      case TextDirection.rtl:
199
        return Alignment.bottomRight;
200
      case TextDirection.ltr:
201
        return Alignment.bottomLeft;
202 203 204
    }
  }

205
  double _getCollapsePadding(double t, FlexibleSpaceBarSettings settings) {
206 207 208 209 210 211 212
    switch (widget.collapseMode) {
      case CollapseMode.pin:
        return -(settings.maxExtent - settings.currentExtent);
      case CollapseMode.none:
        return 0.0;
      case CollapseMode.parallax:
        final double deltaExtent = settings.maxExtent - settings.minExtent;
213
        return -Tween<double>(begin: 0.0, end: deltaExtent / 4.0).transform(t);
214 215 216
    }
  }

217 218
  @override
  Widget build(BuildContext context) {
219 220
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
221
        final FlexibleSpaceBarSettings settings = context.dependOnInheritedWidgetOfExactType<FlexibleSpaceBarSettings>()!;
222

223
        final List<Widget> children = <Widget>[];
224

225 226 227 228
        final double deltaExtent = settings.maxExtent - settings.minExtent;

        // 0.0 -> Expanded
        // 1.0 -> Collapsed to toolbar
229
        final double t = clampDouble(1.0 - (settings.currentExtent - settings.minExtent) / deltaExtent, 0.0, 1.0);
230 231 232 233 234 235

        // background
        if (widget.background != null) {
          final double fadeStart = math.max(0.0, 1.0 - kToolbarHeight / deltaExtent);
          const double fadeEnd = 1.0;
          assert(fadeStart <= fadeEnd);
236 237 238 239 240
          // If the min and max extent are the same, the app bar cannot collapse
          // and the content should be visible, so opacity = 1.
          final double opacity = settings.maxExtent == settings.minExtent
              ? 1.0
              : 1.0 - Interval(fadeStart, fadeEnd).transform(t);
241
          double height = settings.maxExtent;
242

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
          // StretchMode.zoomBackground
          if (widget.stretchModes.contains(StretchMode.zoomBackground) &&
            constraints.maxHeight > height) {
            height = constraints.maxHeight;
          }
          children.add(Positioned(
            top: _getCollapsePadding(t, settings),
            left: 0.0,
            right: 0.0,
            height: height,
            child: Opacity(
              // IOS is relying on this semantics node to correctly traverse
              // through the app bar when it is collapsed.
              alwaysIncludeSemantics: true,
              opacity: opacity,
              child: widget.background,
            ),
          ));

          // StretchMode.blurBackground
          if (widget.stretchModes.contains(StretchMode.blurBackground) &&
            constraints.maxHeight > settings.maxExtent) {
            final double blurAmount = (constraints.maxHeight - settings.maxExtent) / 10;
            children.add(Positioned.fill(
              child: BackdropFilter(
                filter: ui.ImageFilter.blur(
                  sigmaX: blurAmount,
                  sigmaY: blurAmount,
271
                ),
272 273 274
                child: Container(
                  color: Colors.transparent,
                ),
275
              ),
276
            ));
277 278
          }
        }
279

280 281
        // title
        if (widget.title != null) {
282
          final ThemeData theme = Theme.of(context);
283

284
          Widget? title;
285 286
          switch (theme.platform) {
            case TargetPlatform.iOS:
287
            case TargetPlatform.macOS:
288 289 290
              title = widget.title;
              break;
            case TargetPlatform.android:
291 292 293
            case TargetPlatform.fuchsia:
            case TargetPlatform.linux:
            case TargetPlatform.windows:
294 295 296 297
              title = Semantics(
                namesRoute: true,
                child: widget.title,
              );
298
              break;
299 300 301 302 303 304
          }

          // StretchMode.fadeTitle
          if (widget.stretchModes.contains(StretchMode.fadeTitle) &&
            constraints.maxHeight > settings.maxExtent) {
            final double stretchOpacity = 1 -
305 306 307 308
                clampDouble(
                    (constraints.maxHeight - settings.maxExtent) / 100,
                    0.0,
                    1.0);
309 310 311 312 313 314 315 316
            title = Opacity(
              opacity: stretchOpacity,
              child: title,
            );
          }

          final double opacity = settings.toolbarOpacity;
          if (opacity > 0.0) {
317
            TextStyle titleStyle = theme.primaryTextTheme.titleLarge!;
318
            titleStyle = titleStyle.copyWith(
319
              color: titleStyle.color!.withOpacity(opacity),
320 321 322 323 324 325 326
            );
            final bool effectiveCenterTitle = _getEffectiveCenterTitle(theme);
            final EdgeInsetsGeometry padding = widget.titlePadding ??
              EdgeInsetsDirectional.only(
                start: effectiveCenterTitle ? 0.0 : 72.0,
                bottom: 16.0,
              );
327
            final double scaleValue = Tween<double>(begin: widget.expandedTitleScale, end: 1.0).transform(t);
328 329 330 331 332 333 334 335 336 337 338 339
            final Matrix4 scaleTransform = Matrix4.identity()
              ..scale(scaleValue, scaleValue, 1.0);
            final Alignment titleAlignment = _getTitleAlignment(effectiveCenterTitle);
            children.add(Container(
              padding: padding,
              child: Transform(
                alignment: titleAlignment,
                transform: scaleTransform,
                child: Align(
                  alignment: titleAlignment,
                  child: DefaultTextStyle(
                    style: titleStyle,
340 341 342 343 344 345 346
                    child: LayoutBuilder(
                      builder: (BuildContext context, BoxConstraints constraints) {
                        return Container(
                          width: constraints.maxWidth / scaleValue,
                          alignment: titleAlignment,
                          child: title,
                        );
347
                      },
348
                    ),
349 350 351 352 353 354 355 356
                  ),
                ),
              ),
            ));
          }
        }

        return ClipRect(child: Stack(children: children));
357
      },
358
    );
359
  }
360 361
}

362 363 364 365
/// Provides sizing and opacity information to a [FlexibleSpaceBar].
///
/// See also:
///
366
///  * [FlexibleSpaceBar] which creates a flexible space bar.
367 368 369 370 371
class FlexibleSpaceBarSettings extends InheritedWidget {
  /// Creates a Flexible Space Bar Settings widget.
  ///
  /// Used by [Scaffold] and [SliverAppBar]. [child] must have a
  /// [FlexibleSpaceBar] widget in its tree for the settings to take affect.
372 373 374
  ///
  /// The required [toolbarOpacity], [minExtent], [maxExtent], [currentExtent],
  /// and [child] parameters must not be null.
375
  const FlexibleSpaceBarSettings({
376
    super.key,
377 378 379 380
    required this.toolbarOpacity,
    required this.minExtent,
    required this.maxExtent,
    required this.currentExtent,
381
    required super.child,
382
    this.isScrolledUnder,
383 384 385
  }) : assert(minExtent >= 0),
       assert(maxExtent >= 0),
       assert(currentExtent >= 0),
386 387 388
       assert(toolbarOpacity >= 0.0),
       assert(minExtent <= maxExtent),
       assert(minExtent <= currentExtent),
389
       assert(currentExtent <= maxExtent);
390

391
  /// Affects how transparent the text within the toolbar appears.
392
  final double toolbarOpacity;
393 394

  /// Minimum height of the resulting [FlexibleSpaceBar] when fully collapsed.
395
  final double minExtent;
396 397

  /// Maximum height of the resulting [FlexibleSpaceBar] when fully expanded.
398
  final double maxExtent;
399 400 401 402

  /// If the [FlexibleSpaceBar.title] or the [FlexibleSpaceBar.background] is
  /// not null, then this value is used to calculate the relative scale of
  /// these elements upon initialization.
403
  final double currentExtent;
404

405 406 407 408
  /// True if the FlexibleSpaceBar overlaps the primary scrollable's contents.
  ///
  /// This value is used by the [AppBar] to resolve
  /// [AppBar.backgroundColor] against [MaterialState.scrolledUnder],
409
  /// i.e. to enable apps to specify different colors when content
410 411 412 413 414 415
  /// has been scrolled up and behind the app bar.
  ///
  /// Null if the caller hasn't determined if the FlexibleSpaceBar
  /// overlaps the primary scrollable's contents.
  final bool? isScrolledUnder;

416
  @override
417
  bool updateShouldNotify(FlexibleSpaceBarSettings oldWidget) {
418 419 420
    return toolbarOpacity != oldWidget.toolbarOpacity
        || minExtent != oldWidget.minExtent
        || maxExtent != oldWidget.maxExtent
421 422
        || currentExtent != oldWidget.currentExtent
        || isScrolledUnder != oldWidget.isScrolledUnder;
423
  }
424
}