diff --git a/packages/flutter/lib/src/cupertino/search_field.dart b/packages/flutter/lib/src/cupertino/search_field.dart index be24e3a622..94cdd2a978 100644 --- a/packages/flutter/lib/src/cupertino/search_field.dart +++ b/packages/flutter/lib/src/cupertino/search_field.dart @@ -13,6 +13,12 @@ import 'text_field.dart'; export 'package:flutter/services.dart' show SmartDashesType, SmartQuotesType; +// The fraction of the height of the search text field after which its contents +// completely fade out when resized on scroll. +// +// Eyeballed on an iPhone 15 simulator running iOS 17.5. +const double _kMinHeightBeforeTotalTransparency = 4 / 5; + /// A [CupertinoTextField] that mimics the look and behavior of UIKit's /// `UISearchTextField`. /// @@ -337,6 +343,10 @@ class _CupertinoSearchTextFieldState extends State TextEditingController get _effectiveController => widget.controller ?? _controller!.value; + ScrollNotificationObserverState? _scrollNotificationObserver; + double _fadeExtent = 0.0; + double? _maxHeight; + @override void initState() { super.initState(); @@ -345,6 +355,14 @@ class _CupertinoSearchTextFieldState extends State } } + @override + void didChangeDependencies() { + super.didChangeDependencies(); + _scrollNotificationObserver?.removeListener(_handleScrollNotification); + _scrollNotificationObserver = ScrollNotificationObserver.maybeOf(context); + _scrollNotificationObserver?.addListener(_handleScrollNotification); + } + @override void didUpdateWidget(CupertinoSearchTextField oldWidget) { super.didUpdateWidget(oldWidget); @@ -366,6 +384,10 @@ class _CupertinoSearchTextFieldState extends State @override void dispose() { + if (_scrollNotificationObserver != null) { + _scrollNotificationObserver!.removeListener(_handleScrollNotification); + _scrollNotificationObserver = null; + } super.dispose(); if (widget.controller == null) { _controller?.dispose(); @@ -398,13 +420,37 @@ class _CupertinoSearchTextFieldState extends State } } + void _handleScrollNotification(ScrollNotification notification) { + if (_maxHeight == null) { + _maxHeight ??= (context.findRenderObject() as RenderBox?)?.size.height; + } + else { + final RenderBox? renderBox = context.findRenderObject() as RenderBox?; + final double currentHeight = renderBox?.size.height ?? 0.0; + setState(() { _fadeExtent = _calculateScrollOpacity(currentHeight, _maxHeight!); }); + } + } + + static double _calculateScrollOpacity(double currentHeight, double maxHeight) { + final double thresholdHeight = maxHeight * _kMinHeightBeforeTotalTransparency; + if (currentHeight >= maxHeight) { + return 0.0; + } else if (currentHeight <= thresholdHeight) { + return 1.0; + } else { + final double range = maxHeight - thresholdHeight; + final double progress = (currentHeight - thresholdHeight) / range; + return 1.0 - progress; + } + } + @override Widget build(BuildContext context) { final String placeholder = widget.placeholder ?? CupertinoLocalizations.of(context).searchTextFieldPlaceholderLabel; final TextStyle placeholderStyle = widget.placeholderStyle ?? - const TextStyle(color: CupertinoColors.systemGrey); + TextStyle(color: CupertinoColors.systemGrey.withOpacity(1.0 - _fadeExtent)); // The icon size will be scaled by a factor of the accessibility text scale, // to follow the behavior of `UISearchTextField`. @@ -423,23 +469,38 @@ class _CupertinoSearchTextFieldState extends State size: scaledIconSize, ); - final Widget prefix = Padding( - padding: widget.prefixInsets, - child: IconTheme( - data: iconThemeData, - child: widget.prefixIcon, + // Animate the top padding so that the placeholder and editable text + // move when the search text field is resized on scroll. + final EdgeInsets currentInsets = widget.padding.resolve(Directionality.of(context)); + final EdgeInsetsGeometry? padding = EdgeInsetsGeometry.lerp( + widget.padding, + widget.padding.resolve(Directionality.of(context)).copyWith(top: currentInsets.top / 2), + _fadeExtent, + ); + + final Widget prefix = Opacity( + opacity: 1.0 - _fadeExtent, + child: Padding( + padding: widget.prefixInsets, + child: IconTheme( + data: iconThemeData, + child: widget.prefixIcon, + ), ), ); - final Widget suffix = Padding( - padding: widget.suffixInsets, - child: CupertinoButton( - onPressed: widget.onSuffixTap ?? _defaultOnSuffixTap, - minSize: 0, - padding: EdgeInsets.zero, - child: IconTheme( - data: iconThemeData, - child: widget.suffixIcon, + final Widget suffix = Opacity( + opacity: 1.0 - _fadeExtent, + child: Padding( + padding: widget.suffixInsets, + child: CupertinoButton( + onPressed: widget.onSuffixTap ?? _defaultOnSuffixTap, + minSize: 0, + padding: EdgeInsets.zero, + child: IconTheme( + data: iconThemeData, + child: widget.suffixIcon, + ), ), ), ); @@ -456,7 +517,7 @@ class _CupertinoSearchTextFieldState extends State suffixMode: widget.suffixMode, placeholder: placeholder, placeholderStyle: placeholderStyle, - padding: widget.padding, + padding: padding ?? widget.padding, onChanged: widget.onChanged, onSubmitted: widget.onSubmitted, focusNode: widget.focusNode, diff --git a/packages/flutter/test/cupertino/search_field_test.dart b/packages/flutter/test/cupertino/search_field_test.dart index ec21764ea9..9615d125f4 100644 --- a/packages/flutter/test/cupertino/search_field_test.dart +++ b/packages/flutter/test/cupertino/search_field_test.dart @@ -654,4 +654,155 @@ void main() { final CupertinoTextField textField = tester.widget(find.byType(CupertinoTextField)); expect(textField.enableIMEPersonalizedLearning, false); }); + + testWidgets('Icons and placeholder fade while resizing on scroll', (WidgetTester tester) async { + await tester.pumpWidget( + const CupertinoApp( + home: CupertinoPageScaffold( + child: CustomScrollView( + slivers: [ + SliverResizingHeader( + child: CupertinoSearchTextField( + suffixMode: OverlayVisibilityMode.always, + ), + ), + SliverFillRemaining(), + ], + ), + ), + ), + ); + + final Finder searchTextFieldFinder = find.byType(CupertinoSearchTextField); + expect(searchTextFieldFinder, findsOneWidget); + + final Finder prefixIconFinder = find.descendant( + of: searchTextFieldFinder, + matching: find.byIcon(CupertinoIcons.search), + ); + final Finder suffixIconFinder = find.descendant( + of: searchTextFieldFinder, + matching: find.byIcon(CupertinoIcons.xmark_circle_fill), + ); + final Finder placeholderFinder = find.descendant( + of: searchTextFieldFinder, + matching: find.text('Search'), + ); + expect(prefixIconFinder, findsOneWidget); + expect(suffixIconFinder, findsOneWidget); + expect(placeholderFinder, findsOneWidget); + + // Initially, the icons and placeholder text are fully opaque. + expect( + tester.widget(find.ancestor(of: prefixIconFinder, matching: find.byType(Opacity))).opacity, + equals(1.0), + ); + expect( + tester.widget(find.ancestor(of: suffixIconFinder, matching: find.byType(Opacity))).opacity, + equals(1.0), + ); + expect( + tester.widget(placeholderFinder).style?.color?.a, + equals(1.0), + ); + + final double searchTextFieldHeight = tester.getSize(searchTextFieldFinder).height; + + final TestGesture scrollGesture1 = await tester.startGesture(tester.getCenter(find.byType(CustomScrollView))); + await scrollGesture1.moveBy(Offset(0, -searchTextFieldHeight / 5)); + await scrollGesture1.up(); + await tester.pumpAndSettle(); + + // The icons and placeholder text start to fade. + expect( + tester.widget(find.ancestor(of: prefixIconFinder, matching: find.byType(Opacity))).opacity, + greaterThan(0.0), + ); + expect( + tester.widget(find.ancestor(of: prefixIconFinder, matching: find.byType(Opacity))).opacity, + lessThan(1.0), + ); + expect( + tester.widget(find.ancestor(of: suffixIconFinder, matching: find.byType(Opacity))).opacity, + greaterThan(0.0), + ); + expect( + tester.widget(find.ancestor(of: suffixIconFinder, matching: find.byType(Opacity))).opacity, + lessThan(1.0), + ); + expect( + tester.widget(placeholderFinder).style?.color?.a, + greaterThan(0.0), + ); + expect( + tester.widget(placeholderFinder).style?.color?.a, + lessThan(1.0), + ); + + final TestGesture scrollGesture2 = await tester.startGesture(tester.getCenter(find.byType(CustomScrollView))); + await scrollGesture2.moveBy(Offset(0, -4 * searchTextFieldHeight / 5)); + await scrollGesture2.up(); + await tester.pumpAndSettle(); + + // The icons and placeholder text have faded completely. + expect( + tester.widget(find.ancestor(of: prefixIconFinder, matching: find.byType(Opacity))).opacity, + equals(0.0), + ); + expect( + tester.widget(find.ancestor(of: suffixIconFinder, matching: find.byType(Opacity))).opacity, + equals(0.0), + ); + expect( + tester.widget(placeholderFinder).style?.color?.a, + equals(0.0), + ); + }); + + testWidgets('Top padding animates while resizing on scroll', (WidgetTester tester) async { + const TextDirection direction = TextDirection.ltr; + await tester.pumpWidget( + const Directionality( + textDirection: direction, + child: CupertinoApp( + home: CupertinoPageScaffold( + child: CustomScrollView( + slivers: [ + SliverResizingHeader(child: CupertinoSearchTextField()), + SliverFillRemaining(), + ], + ), + ), + ), + ), + ); + + final Finder searchTextFieldFinder = find.byType(CupertinoSearchTextField); + expect(searchTextFieldFinder, findsOneWidget); + + final double initialPadding = tester.widget( + find.descendant( + of: searchTextFieldFinder, + matching: find.byType(CupertinoTextField), + ), + ).padding.resolve(direction).top; + expect(initialPadding, equals(8.0)); + + final double searchTextFieldHeight = tester.getSize(searchTextFieldFinder).height; + + final TestGesture scrollGesture = await tester.startGesture(tester.getCenter(find.byType(CustomScrollView))); + await scrollGesture.moveBy(Offset(0, -searchTextFieldHeight / 5)); + await scrollGesture.up(); + await tester.pumpAndSettle(); + + expect( + tester.widget( + find.descendant( + of: searchTextFieldFinder, + matching: find.byType(CupertinoTextField), + ), + ).padding.resolve(direction).top, + lessThan(initialPadding), + ); + }); }