my_flexible_space_bar.dart
8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
/// The part of a material design [AppBar] that expands and collapses.
///
/// Most commonly used in in the [SliverAppBar.flexibleSpace] field, a flexible
/// 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
/// app.
///
/// The widget that sizes the [AppBar] must wrap it in the widget returned by
/// [FlexibleSpaceBar.createSettings], to convey sizing information down to the
/// [FlexibleSpaceBar].
///
/// See also:
///
/// * [SliverAppBar], which implements the expanding and contracting.
/// * [AppBar], which is used by [SliverAppBar].
/// * <https://material.io/design/components/app-bars-top.html#behavior>
class MyFlexibleSpaceBar extends StatefulWidget {
/// Creates a flexible space bar.
///
/// Most commonly used in the [AppBar.flexibleSpace] field.
const MyFlexibleSpaceBar({
Key? key,
this.title,
this.background,
this.centerTitle,
this.titlePadding,
this.collapseMode = CollapseMode.parallax,
}) : super(key: key);
/// The primary contents of the flexible space bar when expanded.
///
/// Typically a [Text] widget.
final Widget? title;
/// Shown behind the [title] when expanded.
///
/// Typically an [Image] widget with [Image.fit] set to [BoxFit.cover].
final Widget? background;
/// Whether the title should be centered.
///
/// By default this property is true if the current target platform
/// is [TargetPlatform.iOS], false otherwise.
final bool? centerTitle;
/// Collapse effect while scrolling.
///
/// Defaults to [MyCollapseMode.parallax].
final CollapseMode collapseMode;
/// 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
/// is inset from the bottom-left and it is specified along with
/// [centerTitle] false.
///
/// By default the value of this property is
/// `EdgeInsetsDirectional.only(start: 72, bottom: 16)` if the title is
/// not centered, `EdgeInsetsDirectional.only(start 0, bottom: 16)` otherwise.
final EdgeInsetsGeometry? titlePadding;
/// Wraps a widget that contains an [AppBar] to convey sizing information down
/// to the [FlexibleSpaceBar].
///
/// Used by [Scaffold] and [SliverAppBar].
///
/// `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
/// initialization.
///
/// See also:
///
/// * [FlexibleSpaceBarSettings] which creates a settings object that can be
/// used to specify these settings to a [FlexibleSpaceBar].
static Widget createSettings({
double? toolbarOpacity,
double? minExtent,
double? maxExtent,
required double currentExtent,
required Widget child,
}) {
return FlexibleSpaceBarSettings(
toolbarOpacity: toolbarOpacity ?? 1.0,
minExtent: minExtent ?? currentExtent,
maxExtent: maxExtent ?? currentExtent,
currentExtent: currentExtent,
child: child,
);
}
@override
_FlexibleSpaceBarState createState() => _FlexibleSpaceBarState();
}
class _FlexibleSpaceBarState extends State<MyFlexibleSpaceBar> {
bool _getEffectiveCenterTitle(ThemeData theme) {
if (widget.centerTitle != null)
return widget.centerTitle!;
switch (theme.platform) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
return false;
case TargetPlatform.iOS:
case TargetPlatform.macOS:
return true;
}
}
Alignment _getTitleAlignment(bool effectiveCenterTitle) {
if (effectiveCenterTitle)
return Alignment.bottomCenter;
final TextDirection textDirection = Directionality.of(context);
switch (textDirection) {
case TextDirection.rtl:
return Alignment.bottomRight;
case TextDirection.ltr:
return Alignment.bottomLeft;
}
}
double? _getCollapsePadding(double t, FlexibleSpaceBarSettings settings) {
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;
return -Tween<double>(begin: 0.0, end: deltaExtent / 4.0).transform(t);
}
}
final GlobalKey _key = GlobalKey();
double _offset = 0;
@override
void initState() {
//监听Widget是否绘制完毕
WidgetsBinding.instance!.addPostFrameCallback((_) {
final RenderBox? renderBoxRed = _key.currentContext!.findRenderObject() as RenderBox?;
_offset = renderBoxRed!.size.width / 2;
});
super.initState();
}
@override
Widget build(BuildContext context) {
final Size size = MediaQuery.of(context).size;
final FlexibleSpaceBarSettings settings = context.dependOnInheritedWidgetOfExactType<FlexibleSpaceBarSettings>()!;
final List<Widget> children = <Widget>[];
final double deltaExtent = settings.maxExtent - settings.minExtent;
// 0.0 -> Expanded
// 1.0 -> Collapsed to toolbar
final double t = (1.0 - (settings.currentExtent - settings.minExtent) / deltaExtent).clamp(0.0, 1.0);
// background image
if (widget.background != null) {
children.add(Positioned(
top: _getCollapsePadding(t, settings),
left: 0.0,
right: 0.0,
height: settings.maxExtent,
child: widget.background!,
));
}
if (widget.title != null) {
final ThemeData theme = Theme.of(context);
Widget title;
switch (theme.platform) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
title = widget.title!;
break;
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
title = Semantics(
namesRoute: true,
child: widget.title,
);
break;
}
title = Container(
key: _key,
child: title,
);
final double opacity = settings.toolbarOpacity;
if (opacity > 0.0) {
TextStyle titleStyle = theme.primaryTextTheme.headline6!;
titleStyle = titleStyle.copyWith(
color: titleStyle.color!.withOpacity(opacity),
fontWeight: t != 0 ? FontWeight.normal : FontWeight.bold
);
final bool effectiveCenterTitle = _getEffectiveCenterTitle(theme);
final EdgeInsetsGeometry padding = widget.titlePadding ??
EdgeInsetsDirectional.only(
start: effectiveCenterTitle ? 0.0 : 72.0,
bottom: 16.0,
);
final double scaleValue = Tween<double>(begin: 1.5, end: 1.0).transform(t);
final double width = (size.width - 32.0) / 2 - _offset;
final Matrix4 scaleTransform = Matrix4.identity()
..scale(scaleValue, scaleValue, 1.0)..translate(t * width);
final Alignment titleAlignment = _getTitleAlignment(false);
children.add(Container(
padding: padding,
child: Transform(
alignment: titleAlignment,
transform: scaleTransform,
child: Align(
alignment: titleAlignment,
child: DefaultTextStyle(
style: titleStyle,
child: title,
),
),
),
));
}
}
return ClipRect(child: Stack(children: children));
}
}