From e467289fb808fcab8c8e8996bd11643bc079b736 Mon Sep 17 00:00:00 2001 From: huycozy <104349824+huycozy@users.noreply.github.com> Date: Thu, 23 May 2024 15:52:15 +0700 Subject: [PATCH] Fix DecoratedSliver sample code to reflect the description (#148621) ### Demo screenshot ![Screenshot 2024-05-19 at 05 42 35](https://github.com/flutter/flutter/assets/104349824/6b4aec1f-32ab-496e-ab20-458c2051055d) ### Related issue - Fixes https://github.com/flutter/flutter/issues/145935 - Add test for `examples/api/test/widgets/sliver/decorated_sliver.0_test.dart` as a part of https://github.com/flutter/flutter/issues/130459. --- dev/bots/check_code_samples.dart | 1 - .../widgets/sliver/decorated_sliver.0.dart | 66 ++++++++++++++--- .../sliver/decorated_sliver.0_test.dart | 74 +++++++++++++++++++ 3 files changed, 131 insertions(+), 10 deletions(-) create mode 100644 examples/api/test/widgets/sliver/decorated_sliver.0_test.dart diff --git a/dev/bots/check_code_samples.dart b/dev/bots/check_code_samples.dart index deb9d7e0097e..70d96a6a828c 100644 --- a/dev/bots/check_code_samples.dart +++ b/dev/bots/check_code_samples.dart @@ -376,7 +376,6 @@ final Set _knownMissingTests = { 'examples/api/test/widgets/framework/build_owner.0_test.dart', 'examples/api/test/widgets/framework/error_widget.0_test.dart', 'examples/api/test/widgets/inherited_theme/inherited_theme.0_test.dart', - 'examples/api/test/widgets/sliver/decorated_sliver.0_test.dart', 'examples/api/test/widgets/autofill/autofill_group.0_test.dart', 'examples/api/test/widgets/nested_scroll_view/nested_scroll_view_state.0_test.dart', 'examples/api/test/widgets/scroll_position/scroll_metrics_notification.0_test.dart', diff --git a/examples/api/lib/widgets/sliver/decorated_sliver.0.dart b/examples/api/lib/widgets/sliver/decorated_sliver.0.dart index 5ccd9b93cfb0..74489c76c5d2 100644 --- a/examples/api/lib/widgets/sliver/decorated_sliver.0.dart +++ b/examples/api/lib/widgets/sliver/decorated_sliver.0.dart @@ -12,6 +12,14 @@ class SliverDecorationExampleApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( + theme: ThemeData( + textTheme: const TextTheme( + titleLarge: TextStyle( + fontSize: 24, + color: Colors.white30, + ), + ), + ), home: Scaffold( appBar: AppBar(title: const Text('SliverDecoration Sample')), body: const SliverDecorationExample(), @@ -28,6 +36,7 @@ class SliverDecorationExample extends StatelessWidget { return CustomScrollView( slivers: [ DecoratedSliver( + key: const ValueKey('radial-gradient'), decoration: const BoxDecoration( gradient: RadialGradient( center: Alignment(-0.5, -0.6), @@ -36,21 +45,60 @@ class SliverDecorationExample extends StatelessWidget { Color(0xFFEEEEEE), Color(0xFF111133), ], - stops: [0.9, 1.0], + stops: [0.4, 0.8], ), ), sliver: SliverList( - delegate: SliverChildListDelegate([ - const Text('Goodnight Moon'), - ]), + delegate: SliverChildListDelegate( + [ + SizedBox( + height: 200.0, + child: Center( + child: Text( + 'A moon on a night sky', + style: Theme.of(context).textTheme.titleLarge, + ), + ), + ), + ], + ), ), ), - const DecoratedSliver( - decoration: BoxDecoration( - color: Colors.amber, - borderRadius: BorderRadius.all(Radius.circular(50)) + DecoratedSliver( + key: const ValueKey('linear-gradient'), + decoration: const BoxDecoration( + gradient: LinearGradient( + begin: Alignment.topCenter, + end: Alignment.bottomCenter, + colors: [ + Color(0xFF111133), + Color(0xFF1A237E), + Color(0xFF283593), + Color(0xFF3949AB), + Color(0xFF3F51B5), + Color(0xFF1976D2), + Color(0xFF1E88E5), + Color(0xFF42A5F5), + ], + ), + ), + sliver: SliverList( + delegate: SliverChildListDelegate( + [ + SizedBox( + height: 500.0, + child: Container( + alignment: Alignment.topCenter, + padding: const EdgeInsets.only(top: 56.0), + child: Text( + 'A blue sky', + style: Theme.of(context).textTheme.titleLarge, + ), + ), + ), + ], + ), ), - sliver: SliverToBoxAdapter(child: SizedBox(height: 300)), ), ], ); diff --git a/examples/api/test/widgets/sliver/decorated_sliver.0_test.dart b/examples/api/test/widgets/sliver/decorated_sliver.0_test.dart new file mode 100644 index 000000000000..6298215aca67 --- /dev/null +++ b/examples/api/test/widgets/sliver/decorated_sliver.0_test.dart @@ -0,0 +1,74 @@ +// 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_api_samples/widgets/sliver/decorated_sliver.0.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('Verify the texts are displayed', (WidgetTester tester) async { + await tester.pumpWidget( + const example.SliverDecorationExampleApp(), + ); + + final Finder moonText = find.text('A moon on a night sky'); + expect(moonText, findsOneWidget); + + final Finder blueSkyText = find.text('A blue sky'); + expect(blueSkyText, findsOneWidget); + }); + + testWidgets('Verify the sliver with key `radial-gradient` has a RadialGradient', (WidgetTester tester) async { + await tester.pumpWidget( + const example.SliverDecorationExampleApp(), + ); + + final DecoratedSliver radialDecoratedSliver = tester.widget( + find.byKey(const ValueKey('radial-gradient')), + ); + expect( + radialDecoratedSliver.decoration, + const BoxDecoration( + gradient: RadialGradient( + center: Alignment(-0.5, -0.6), + radius: 0.15, + colors: [ + Color(0xFFEEEEEE), + Color(0xFF111133), + ], + stops: [0.4, 0.8], + ), + ), + ); + }); + + testWidgets('Verify that the sliver with key `linear-gradient` has a LinearGradient', (WidgetTester tester) async { + await tester.pumpWidget( + const example.SliverDecorationExampleApp(), + ); + + final DecoratedSliver linearDecoratedSliver = tester.widget( + find.byKey(const ValueKey('linear-gradient')), + ); + expect( + linearDecoratedSliver.decoration, + const BoxDecoration( + gradient: LinearGradient( + begin: Alignment.topCenter, + end: Alignment.bottomCenter, + colors: [ + Color(0xFF111133), + Color(0xFF1A237E), + Color(0xFF283593), + Color(0xFF3949AB), + Color(0xFF3F51B5), + Color(0xFF1976D2), + Color(0xFF1E88E5), + Color(0xFF42A5F5), + ], + ), + ), + ); + }); +}