Victor Sanni
2024-10-15 10:03:25 -07:00
committed by GitHub
parent ad370c8fa7
commit 915985ad52
2 changed files with 228 additions and 16 deletions

View File

@@ -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<CupertinoSearchTextField>
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<CupertinoSearchTextField>
}
}
@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<CupertinoSearchTextField>
@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<CupertinoSearchTextField>
}
}
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<CupertinoSearchTextField>
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<CupertinoSearchTextField>
suffixMode: widget.suffixMode,
placeholder: placeholder,
placeholderStyle: placeholderStyle,
padding: widget.padding,
padding: padding ?? widget.padding,
onChanged: widget.onChanged,
onSubmitted: widget.onSubmitted,
focusNode: widget.focusNode,

View File

@@ -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: <Widget>[
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<Opacity>(find.ancestor(of: prefixIconFinder, matching: find.byType(Opacity))).opacity,
equals(1.0),
);
expect(
tester.widget<Opacity>(find.ancestor(of: suffixIconFinder, matching: find.byType(Opacity))).opacity,
equals(1.0),
);
expect(
tester.widget<Text>(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<Opacity>(find.ancestor(of: prefixIconFinder, matching: find.byType(Opacity))).opacity,
greaterThan(0.0),
);
expect(
tester.widget<Opacity>(find.ancestor(of: prefixIconFinder, matching: find.byType(Opacity))).opacity,
lessThan(1.0),
);
expect(
tester.widget<Opacity>(find.ancestor(of: suffixIconFinder, matching: find.byType(Opacity))).opacity,
greaterThan(0.0),
);
expect(
tester.widget<Opacity>(find.ancestor(of: suffixIconFinder, matching: find.byType(Opacity))).opacity,
lessThan(1.0),
);
expect(
tester.widget<Text>(placeholderFinder).style?.color?.a,
greaterThan(0.0),
);
expect(
tester.widget<Text>(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<Opacity>(find.ancestor(of: prefixIconFinder, matching: find.byType(Opacity))).opacity,
equals(0.0),
);
expect(
tester.widget<Opacity>(find.ancestor(of: suffixIconFinder, matching: find.byType(Opacity))).opacity,
equals(0.0),
);
expect(
tester.widget<Text>(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: <Widget>[
SliverResizingHeader(child: CupertinoSearchTextField()),
SliverFillRemaining(),
],
),
),
),
),
);
final Finder searchTextFieldFinder = find.byType(CupertinoSearchTextField);
expect(searchTextFieldFinder, findsOneWidget);
final double initialPadding = tester.widget<CupertinoTextField>(
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<CupertinoTextField>(
find.descendant(
of: searchTextFieldFinder,
matching: find.byType(CupertinoTextField),
),
).padding.resolve(direction).top,
lessThan(initialPadding),
);
});
}