Add test for flexible_space_bar.0.dart (#157107)

Contributes to https://github.com/flutter/flutter/issues/130459

It adds a test for
- `examples/api/test/material/flexible_space_bar/flexible_space_bar.0_test.dart`
This commit is contained in:
Valentin Vignal 2024-10-21 23:17:14 +08:00 committed by GitHub
parent bcc3146185
commit 3405f11e5c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 103 additions and 56 deletions

View File

@ -312,7 +312,6 @@ final Set<String> _knownMissingTests = <String>{
'examples/api/test/material/selectable_region/selectable_region.0_test.dart',
'examples/api/test/material/color_scheme/dynamic_content_color.0_test.dart',
'examples/api/test/material/platform_menu_bar/platform_menu_bar.0_test.dart',
'examples/api/test/material/flexible_space_bar/flexible_space_bar.0_test.dart',
'examples/api/test/material/navigation_rail/navigation_rail.extended_animation.0_test.dart',
'examples/api/test/painting/star_border/star_border.0_test.dart',
'examples/api/test/widgets/navigator/navigator.restorable_push_and_remove_until.0_test.dart',

View File

@ -2,77 +2,83 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
/// Flutter code sample for [FlexibleSpaceBar].
void main() => runApp(const MaterialApp(home: FlexibleSpaceBarExampleApp()));
void main() => runApp(const FlexibleSpaceBarExampleApp());
class FlexibleSpaceBarExampleApp extends StatelessWidget {
const FlexibleSpaceBarExampleApp({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
physics: const BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
slivers: <Widget>[
SliverAppBar(
stretch: true,
onStretchTrigger: () {
// Function callback for stretch
return Future<void>.value();
},
expandedHeight: 300.0,
flexibleSpace: FlexibleSpaceBar(
stretchModes: const <StretchMode>[
StretchMode.zoomBackground,
StretchMode.blurBackground,
StretchMode.fadeTitle,
],
centerTitle: true,
title: const Text('Flight Report'),
background: Stack(
fit: StackFit.expand,
children: <Widget>[
Image.network(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg',
fit: BoxFit.cover,
),
const DecoratedBox(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment(0.0, 0.5),
end: Alignment.center,
colors: <Color>[
Color(0x60000000),
Color(0x00000000),
],
return MaterialApp(
scrollBehavior: const MaterialScrollBehavior().copyWith(
dragDevices: PointerDeviceKind.values.toSet(),
),
home: Scaffold(
body: CustomScrollView(
physics: const BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
slivers: <Widget>[
SliverAppBar(
stretch: true,
onStretchTrigger: () {
// Function callback for stretch
return Future<void>.value();
},
expandedHeight: 300.0,
flexibleSpace: FlexibleSpaceBar(
stretchModes: const <StretchMode>[
StretchMode.zoomBackground,
StretchMode.blurBackground,
StretchMode.fadeTitle,
],
centerTitle: true,
title: const Text('Flight Report'),
background: Stack(
fit: StackFit.expand,
children: <Widget>[
Image.network(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg',
fit: BoxFit.cover,
),
const DecoratedBox(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment(0.0, 0.5),
end: Alignment.center,
colors: <Color>[
Color(0x60000000),
Color(0x00000000),
],
),
),
),
],
),
),
),
SliverList(
delegate: SliverChildListDelegate(
const <Widget>[
ListTile(
leading: Icon(Icons.wb_sunny),
title: Text('Sunday'),
subtitle: Text('sunny, h: 80, l: 65'),
),
ListTile(
leading: Icon(Icons.wb_sunny),
title: Text('Monday'),
subtitle: Text('sunny, h: 80, l: 65'),
),
// ListTiles++
],
),
),
),
SliverList(
delegate: SliverChildListDelegate(
const <Widget>[
ListTile(
leading: Icon(Icons.wb_sunny),
title: Text('Sunday'),
subtitle: Text('sunny, h: 80, l: 65'),
),
ListTile(
leading: Icon(Icons.wb_sunny),
title: Text('Monday'),
subtitle: Text('sunny, h: 80, l: 65'),
),
// ListTiles++
],
),
),
],
],
),
),
);
}

View File

@ -0,0 +1,42 @@
// 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 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_api_samples/material/flexible_space_bar/flexible_space_bar.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
// The app being tested loads images via HTTP which the test
// framework defeats by default.
setUpAll(() {
HttpOverrides.global = null;
});
testWidgets('The app bar stretches when over-scrolled', (WidgetTester tester) async {
await tester.pumpWidget(
const example.FlexibleSpaceBarExampleApp(),
);
expect(find.text('Flight Report'), findsOne);
expect(find.widgetWithText(ListTile, 'Sunday'), findsOne);
expect(find.widgetWithText(ListTile, 'Monday'), findsOne);
expect(find.text('sunny, h: 80, l: 65'), findsExactly(2));
expect(find.byIcon(Icons.wb_sunny), findsExactly(2));
final Finder appBarContainer = find.byType(Image);
final Size sizeBeforeScroll = tester.getSize(appBarContainer);
final Offset target = tester.getCenter(find.byType(ListTile).first);
final TestGesture gesture = await tester.startGesture(target);
await gesture.moveBy(const Offset(0.0, 100.0));
await tester.pump(const Duration(milliseconds: 10));
await gesture.up();
final Size sizeAfterScroll = tester.getSize(appBarContainer);
expect(sizeBeforeScroll.height, lessThan(sizeAfterScroll.height));
// Verifies ScrollBehavior.dragDevices is correctly set.
}, variant: TargetPlatformVariant.all());
}