flutter_flutter/packages/flutter/test/widgets/slivers_appbar_stretch_test.dart
Kate Lovett 9d96df2364
Modernize framework lints (#179089)
WIP

Commits separated as follows:
- Update lints in analysis_options files
- Run `dart fix --apply`
- Clean up leftover analysis issues 
- Run `dart format .` in the right places.

Local analysis and testing passes. Checking CI now.

Part of https://github.com/flutter/flutter/issues/178827
- Adoption of flutter_lints in examples/api coming in a separate change
(cc @loic-sharma)

## Pre-launch Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2025-11-26 01:10:39 +00:00

401 lines
15 KiB
Dart

// Copyright 2014 The Flutter 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/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('SliverAppBar - Stretch', () {
testWidgets('fills overscroll', (WidgetTester tester) async {
const anchor = Key('drag');
await tester.pumpWidget(
MaterialApp(
home: CustomScrollView(
physics: const BouncingScrollPhysics(),
slivers: <Widget>[
const SliverAppBar(stretch: true, expandedHeight: 100.0),
SliverToBoxAdapter(child: Container(key: anchor, height: 800)),
SliverToBoxAdapter(child: Container(height: 800)),
],
),
),
);
final RenderSliverScrollingPersistentHeader header = tester.renderObject(
find.byType(SliverAppBar),
);
expect(header.child!.size.height, equals(100.0));
await slowDrag(tester, anchor, const Offset(0.0, 100));
expect(header.child!.size.height, equals(200.0));
});
testWidgets('fills overscroll after reverse direction input - scrolling header', (
WidgetTester tester,
) async {
const anchor = Key('drag');
await tester.pumpWidget(
MaterialApp(
home: CustomScrollView(
physics: const BouncingScrollPhysics(),
slivers: <Widget>[
const SliverAppBar(title: Text('Test'), stretch: true, expandedHeight: 100.0),
SliverToBoxAdapter(child: Container(key: anchor, height: 800)),
SliverToBoxAdapter(child: Container(height: 800)),
],
),
),
);
final RenderSliverScrollingPersistentHeader header = tester.renderObject(
find.byType(SliverAppBar),
);
expect(header.child!.size.height, equals(100.0));
expect(tester.getCenter(find.text('Test')).dy, 28.0);
// First scroll the header away
final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byKey(anchor)));
await gesture.moveBy(const Offset(0.0, -100.0));
await tester.pump(const Duration(milliseconds: 10));
expect(header.child!.size.height, equals(56.0));
expect(tester.getCenter(find.text('Test', skipOffstage: false)).dy, -28.0);
// With the same gesture, scroll back and into overscroll
await gesture.moveBy(const Offset(0.0, 200.0));
await tester.pump(const Duration(milliseconds: 10));
// Header should stretch in overscroll
expect(header.child!.size.height, equals(200.0));
expect(tester.getCenter(find.text('Test')).dy, 28.0);
await gesture.up();
await tester.pumpAndSettle();
});
testWidgets('fills overscroll after reverse direction input - floating header', (
WidgetTester tester,
) async {
const anchor = Key('drag');
await tester.pumpWidget(
MaterialApp(
home: CustomScrollView(
physics: const BouncingScrollPhysics(),
slivers: <Widget>[
const SliverAppBar(
title: Text('Test'),
stretch: true,
floating: true,
expandedHeight: 100.0,
),
SliverToBoxAdapter(child: Container(key: anchor, height: 800)),
SliverToBoxAdapter(child: Container(height: 800)),
],
),
),
);
final RenderSliverFloatingPersistentHeader header = tester.renderObject(
find.byType(SliverAppBar),
);
expect(header.child!.size.height, equals(100.0));
expect(tester.getCenter(find.text('Test')).dy, 28.0);
// First scroll the header away
final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byKey(anchor)));
await gesture.moveBy(const Offset(0.0, -100.0));
await tester.pump(const Duration(milliseconds: 10));
expect(header.child!.size.height, equals(56.0));
expect(tester.getCenter(find.text('Test', skipOffstage: false)).dy, -28.0);
// With the same gesture, scroll back and into overscroll
await gesture.moveBy(const Offset(0.0, 200.0));
await tester.pump(const Duration(milliseconds: 10));
// Header should stretch in overscroll
expect(header.child!.size.height, equals(200.0));
expect(tester.getCenter(find.text('Test')).dy, 28.0);
await gesture.up();
await tester.pumpAndSettle();
});
testWidgets('does not stretch without overscroll physics', (WidgetTester tester) async {
const anchor = Key('drag');
await tester.pumpWidget(
MaterialApp(
home: CustomScrollView(
physics: const ClampingScrollPhysics(),
slivers: <Widget>[
const SliverAppBar(stretch: true, expandedHeight: 100.0),
SliverToBoxAdapter(child: Container(key: anchor, height: 800)),
SliverToBoxAdapter(child: Container(height: 800)),
],
),
),
);
final RenderSliverScrollingPersistentHeader header = tester.renderObject(
find.byType(SliverAppBar),
);
expect(header.child!.size.height, equals(100.0));
await slowDrag(tester, anchor, const Offset(0.0, 100.0));
expect(header.child!.size.height, equals(100.0));
});
testWidgets('default trigger offset', (WidgetTester tester) async {
var didTrigger = false;
const anchor = Key('drag');
await tester.pumpWidget(
MaterialApp(
home: CustomScrollView(
physics: const BouncingScrollPhysics(),
slivers: <Widget>[
SliverAppBar(
stretch: true,
expandedHeight: 100.0,
onStretchTrigger: () async {
didTrigger = true;
},
),
SliverToBoxAdapter(child: Container(key: anchor, height: 800)),
SliverToBoxAdapter(child: Container(height: 800)),
],
),
),
);
await slowDrag(tester, anchor, const Offset(0.0, 50.0));
expect(didTrigger, isFalse);
await tester.pumpAndSettle();
await slowDrag(tester, anchor, const Offset(0.0, 150.0));
expect(didTrigger, isTrue);
});
testWidgets('custom trigger offset', (WidgetTester tester) async {
var didTrigger = false;
const anchor = Key('drag');
await tester.pumpWidget(
MaterialApp(
home: CustomScrollView(
physics: const BouncingScrollPhysics(),
slivers: <Widget>[
SliverAppBar(
stretch: true,
expandedHeight: 100.0,
stretchTriggerOffset: 150.0,
onStretchTrigger: () async {
didTrigger = true;
},
),
SliverToBoxAdapter(child: Container(key: anchor, height: 800)),
SliverToBoxAdapter(child: Container(height: 800)),
],
),
),
);
await slowDrag(tester, anchor, const Offset(0.0, 100.0));
await tester.pumpAndSettle();
expect(didTrigger, isFalse);
await slowDrag(tester, anchor, const Offset(0.0, 300.0));
expect(didTrigger, isTrue);
});
testWidgets('stretch callback not triggered without overscroll physics', (
WidgetTester tester,
) async {
var didTrigger = false;
const anchor = Key('drag');
await tester.pumpWidget(
MaterialApp(
home: CustomScrollView(
physics: const ClampingScrollPhysics(),
slivers: <Widget>[
SliverAppBar(
stretch: true,
expandedHeight: 100.0,
stretchTriggerOffset: 150.0,
onStretchTrigger: () async {
didTrigger = true;
},
),
SliverToBoxAdapter(child: Container(key: anchor, height: 800)),
SliverToBoxAdapter(child: Container(height: 800)),
],
),
),
);
await slowDrag(tester, anchor, const Offset(0.0, 100.0));
await tester.pumpAndSettle();
expect(didTrigger, isFalse);
await slowDrag(tester, anchor, const Offset(0.0, 300.0));
expect(didTrigger, isFalse);
});
testWidgets('asserts reasonable trigger offset', (WidgetTester tester) async {
expect(() {
return MaterialApp(
home: CustomScrollView(
physics: const ClampingScrollPhysics(),
slivers: <Widget>[
SliverAppBar(stretch: true, expandedHeight: 100.0, stretchTriggerOffset: -150.0),
SliverToBoxAdapter(child: Container(height: 800)),
SliverToBoxAdapter(child: Container(height: 800)),
],
),
);
}, throwsAssertionError);
});
});
group('SliverAppBar - Stretch, Pinned', () {
testWidgets('fills overscroll', (WidgetTester tester) async {
const anchor = Key('drag');
await tester.pumpWidget(
MaterialApp(
home: CustomScrollView(
physics: const BouncingScrollPhysics(),
slivers: <Widget>[
const SliverAppBar(pinned: true, stretch: true, expandedHeight: 100.0),
SliverToBoxAdapter(child: Container(key: anchor, height: 800)),
SliverToBoxAdapter(child: Container(height: 800)),
],
),
),
);
final RenderSliverPinnedPersistentHeader header = tester.renderObject(
find.byType(SliverAppBar),
);
expect(header.child!.size.height, equals(100.0));
await slowDrag(tester, anchor, const Offset(0.0, 100));
expect(header.child!.size.height, equals(200.0));
});
testWidgets('does not stretch without overscroll physics', (WidgetTester tester) async {
const anchor = Key('drag');
await tester.pumpWidget(
MaterialApp(
home: CustomScrollView(
physics: const ClampingScrollPhysics(),
slivers: <Widget>[
const SliverAppBar(pinned: true, stretch: true, expandedHeight: 100.0),
SliverToBoxAdapter(child: Container(key: anchor, height: 800)),
SliverToBoxAdapter(child: Container(height: 800)),
],
),
),
);
final RenderSliverPinnedPersistentHeader header = tester.renderObject(
find.byType(SliverAppBar),
);
expect(header.child!.size.height, equals(100.0));
await slowDrag(tester, anchor, const Offset(0.0, 100));
expect(header.child!.size.height, equals(100.0));
});
});
group('SliverAppBar - Stretch, Floating', () {
testWidgets('fills overscroll', (WidgetTester tester) async {
const anchor = Key('drag');
await tester.pumpWidget(
MaterialApp(
home: CustomScrollView(
physics: const BouncingScrollPhysics(),
slivers: <Widget>[
const SliverAppBar(floating: true, stretch: true, expandedHeight: 100.0),
SliverToBoxAdapter(child: Container(key: anchor, height: 800)),
SliverToBoxAdapter(child: Container(height: 800)),
],
),
),
);
final RenderSliverFloatingPersistentHeader header = tester.renderObject(
find.byType(SliverAppBar),
);
expect(header.child!.size.height, equals(100.0));
await slowDrag(tester, anchor, const Offset(0.0, 100));
expect(header.child!.size.height, equals(200.0));
});
testWidgets('does not fill overscroll without proper physics', (WidgetTester tester) async {
const anchor = Key('drag');
await tester.pumpWidget(
MaterialApp(
home: CustomScrollView(
physics: const ClampingScrollPhysics(),
slivers: <Widget>[
const SliverAppBar(floating: true, stretch: true, expandedHeight: 100.0),
SliverToBoxAdapter(child: Container(key: anchor, height: 800)),
SliverToBoxAdapter(child: Container(height: 800)),
],
),
),
);
final RenderSliverFloatingPersistentHeader header = tester.renderObject(
find.byType(SliverAppBar),
);
expect(header.child!.size.height, equals(100.0));
await slowDrag(tester, anchor, const Offset(0.0, 100));
expect(header.child!.size.height, equals(100.0));
});
});
group('SliverAppBar - Stretch, Floating, Pinned', () {
testWidgets('fills overscroll', (WidgetTester tester) async {
const anchor = Key('drag');
await tester.pumpWidget(
MaterialApp(
home: CustomScrollView(
physics: const BouncingScrollPhysics(),
slivers: <Widget>[
const SliverAppBar(
floating: true,
pinned: true,
stretch: true,
expandedHeight: 100.0,
),
SliverToBoxAdapter(child: Container(key: anchor, height: 800)),
SliverToBoxAdapter(child: Container(height: 800)),
],
),
),
);
final RenderSliverFloatingPinnedPersistentHeader header = tester.renderObject(
find.byType(SliverAppBar),
);
expect(header.child!.size.height, equals(100.0));
await slowDrag(tester, anchor, const Offset(0.0, 100));
expect(header.child!.size.height, equals(200.0));
});
testWidgets('does not fill overscroll without proper physics', (WidgetTester tester) async {
const anchor = Key('drag');
await tester.pumpWidget(
MaterialApp(
home: CustomScrollView(
physics: const ClampingScrollPhysics(),
slivers: <Widget>[
const SliverAppBar(
pinned: true,
floating: true,
stretch: true,
expandedHeight: 100.0,
),
SliverToBoxAdapter(child: Container(key: anchor, height: 800)),
SliverToBoxAdapter(child: Container(height: 800)),
],
),
),
);
final RenderSliverFloatingPinnedPersistentHeader header = tester.renderObject(
find.byType(SliverAppBar),
);
expect(header.child!.size.height, equals(100.0));
await slowDrag(tester, anchor, const Offset(0.0, 100));
expect(header.child!.size.height, equals(100.0));
});
});
}
Future<void> slowDrag(WidgetTester tester, Key widget, Offset offset) async {
final Offset target = tester.getCenter(find.byKey(widget));
final TestGesture gesture = await tester.startGesture(target);
await gesture.moveBy(offset);
await tester.pump(const Duration(milliseconds: 10));
await gesture.up();
}