Skip to content

Instantly share code, notes, and snippets.

@nonameden
Created July 28, 2017 04:22
Show Gist options
  • Save nonameden/5ac7b8c129e8ab9ede17fef2f8f0106b to your computer and use it in GitHub Desktop.
Save nonameden/5ac7b8c129e8ab9ede17fef2f8f0106b to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('SliverPersistentHeader - floating+pinned - inner scroll should not be overlapped by header', (WidgetTester tester) async {
final GlobalKey _headerKey = new GlobalKey();
final GlobalKey _subHeaderKey = new GlobalKey();
final HeaderDelegate _headerDelegate = new HeaderDelegate();
await tester.pumpWidget(
new NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return [
new SliverPersistentHeader(
key: _headerKey,
delegate: _headerDelegate,
pinned: true,
floating: true,
)
];
},
body: new Stack(children: <Widget>[
new ListView.builder(
itemBuilder: (BuildContext context, int index) {
return new Container(
color: Colors.red,
child: new Text("aaa${index}"),
);
},
itemCount: 100,
),
new Container(
key: _subHeaderKey,
color: Colors.black26,
child: const Text('hhh'),
)
])),
);
// Verify position first, offset should be same as maxExtent from persistent header
expect(tester.getTopLeft(find.text('hhh')), new Offset(0.0, _headerDelegate.maxExtent));
// Start scrolling
final Offset point = tester.getCenter(find.text('aaa3'));
// Creating gesture
final TestGesture gesture = await tester.startGesture(point);
await tester.pump(const Duration(milliseconds: 5000));
// Scroll on 100 px down
await gesture.moveBy(const Offset(0.0, -100.0));
await tester.pump(const Duration(milliseconds: 10));
// Offset should be as minExtent from persistent header
expect(tester.getTopLeft(find.text('hhh')), new Offset(0.0, _headerDelegate.minExtent));
// Scroll 100 px down more
await gesture.moveBy(const Offset(0.0, 100.0));
await tester.pump(const Duration(milliseconds: 10));
// Offset should kepp staying same as minExtent from persistent header
expect(tester.getTopLeft(find.text('hhh')), new Offset(0.0, _headerDelegate.minExtent));
});
}
class HeaderDelegate extends SliverPersistentHeaderDelegate {
@override
double get maxExtent => 200.0;
@override
double get minExtent => 100.0;
@override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
return new Container(
height: maxExtent,
color: Colors.black26,
);
}
@override
bool shouldRebuild(HeaderDelegate oldDelegate) => false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment