SliverAppBar这个控件有个问题就是设置了标题之后一直显示,参考这篇文章
import 'package:flutter/material.dart';class SABT extends StatefulWidget {
final Widget child; const SABT({
Key key,
@required this.child,
}) : super(key: key); @override
_SABTState createState() {
return new _SABTState();
}
}class _SABTState extends State<SABT> {
ScrollPosition _position;
bool _visible; @override
void dispose() {
_removeListener();
super.dispose();
} @override
void didChangeDependencies() {
super.didChangeDependencies();
_removeListener();
_addListener();
} void _addListener() {
_position = Scrollable.of(context)?.position;
_position?.addListener(_positionListener);
_positionListener();
} void _removeListener() {
_position?.removeListener(_positionListener);
} void _positionListener() {
final FlexibleSpaceBarSettings settings =
context.inheritFromWidgetOfExactType(FlexibleSpaceBarSettings);
bool visible = settings == null || settings.currentExtent <= settings.minExtent;
if (_visible != visible) {
setState(() {
_visible = visible;
});
}
} @override
Widget build(BuildContext context) {
return Visibility(
visible: _visible,
child: widget.child,
);
}
}
使用方式
SliverAppBar(
primary: true,
pinned: true,
expandedHeight: 500,
title: SABT(child: Text("The title")),
flexibleSpace: FlexibleSpaceBar(
background: Container(
color: Colors.purple,
),
),
),