Flutter实现可折叠标题栏

Flutter实现可折叠标题栏

先看效果图:

实现方案:

在ScrollView里面添加两个头布局(展开布局 + 收缩布局),展开布局开始显示,收缩布局开始隐藏。监听页面滑动,当滑动到指定位置时,展开布局隐藏,收缩布局显示。

具体代码:

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
import 'package:flutter/material.dart';
import 'search.dart';

class HomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _HomePage();
}
}

class _HomePage extends State<HomePage> {
var _showNarrow = false;
var _oldShowState = false;
var _scrollController = ScrollController();

bool _onNotification(ScrollNotification notification) {

bool show = _scrollController.offset > 100;
if (show != _oldShowState) {
_oldShowState = show;
setState(() {
_showNarrow = show;
});
}
return true;
}

void _functionTap(int index) {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => new SearchPage(
type: index,
)),
);
}

@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
body: Stack(
children: <Widget>[
NotificationListener(
onNotification: _onNotification,
child: CustomScrollView(
slivers: <Widget>[
SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 1, childAspectRatio: 1.8),
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return _ExpandWidget(show: !_showNarrow);
},
childCount: 1,
),
),
SliverFixedExtentList(
itemExtent: 50.0,
delegate: new SliverChildBuilderDelegate(
(context, index) => new ListTile(
title: new Text("List item $index"),
subtitle: new Text("this is subtitle"),
)),
)
],
controller: _scrollController,
)),
_NarrowWidget(
show: _showNarrow,
),
],
));
}
}

// ignore: must_be_immutable
class _ExpandWidget extends StatefulWidget {
var show = true;

_ExpandWidget({this.show});

@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _ExpandWidgetState();
}
}

class _ExpandWidgetState extends State<_ExpandWidget> {
@override
Widget build(BuildContext context) {
// TODO: implement build
if (widget.show) {
return Stack(
children: <Widget>[
Image.asset('images/bg_home_header1.jpg',
fit: BoxFit.fill, height: 100),
Positioned(
bottom: 15.0,
left: 20,
right: 20,
child: Container(
child: Row(
children: <Widget>[
Icon(Icons.search),
Text("请输入关键字查询"),
],
),
decoration: new BoxDecoration(
color: Color(0xFFFFFFFF),
borderRadius: new BorderRadius.circular((5.0)),
),
padding: EdgeInsets.all(8.0),
),
),
],
fit: StackFit.expand,
alignment: Alignment.bottomCenter,
);
} else {
return Container();
}
}
}

// ignore: must_be_immutable
class _NarrowWidget extends StatefulWidget {
var show = false;

_NarrowWidget({this.show});

@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _NarrowWidgetState();
}
}

class _NarrowWidgetState extends State<_NarrowWidget> {
@override
Widget build(BuildContext context) {
// TODO: implement build
if (widget.show) {
return Stack(
children: <Widget>[
Positioned(
top: 0.0,
left: 0.0,
right: 0.0,
child: Container(
padding: EdgeInsets.fromLTRB(20, 48, 20, 15),
color: Color(0xFF52707A),
child: Container(
child: Row(
children: <Widget>[
Icon(Icons.search),
Text("请输入关键字查询"),
],
),
decoration: new BoxDecoration(
color: Color(0xFFFFFFFF),
borderRadius: new BorderRadius.circular((5.0)),
),
padding: EdgeInsets.all(8.0),
),
)),
],
fit: StackFit.expand,
alignment: Alignment.topCenter,
);
} else {
return Container();
}
}
}

本示例仅是提供实现方案,对于代码中写死的一些值,为了更好的UI效果,需要根据具体实现时计算。

SidXu wechat
联系作者