132 lines
3.9 KiB
Dart
132 lines
3.9 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../adaptive/host.dart';
|
|
import 'cupertino_web_search_field.dart';
|
|
|
|
/// The app's one search field per platform.
|
|
///
|
|
/// Cupertino uses the stock [CupertinoSearchTextField]. Material uses a
|
|
/// [TextField] shaped like it: a stadium with a leading search icon, a clear
|
|
/// button once there is text, no underline and no elevation. On a page it is
|
|
/// filled with the host's raised surface; [inBar] drops the fill (and the
|
|
/// Cupertino grey) because a bar's glass pill is already its background.
|
|
class AppSearchField extends StatelessWidget {
|
|
const AppSearchField({
|
|
super.key,
|
|
required this.controller,
|
|
required this.placeholder,
|
|
this.fieldKey,
|
|
this.clearKey,
|
|
this.onChanged,
|
|
this.onSubmitted,
|
|
this.onCleared,
|
|
this.autofocus = false,
|
|
this.focusNode,
|
|
this.inBar = false,
|
|
this.cupertino,
|
|
});
|
|
|
|
final TextEditingController controller;
|
|
final String placeholder;
|
|
|
|
/// The key of the text field itself, for finders.
|
|
final Key? fieldKey;
|
|
|
|
/// The key of Material's clear button.
|
|
final Key? clearKey;
|
|
final ValueChanged<String>? onChanged;
|
|
final ValueChanged<String>? onSubmitted;
|
|
|
|
/// Runs after the clear control emptied the field; without it the clear
|
|
/// reports an empty [onChanged].
|
|
final VoidCallback? onCleared;
|
|
final bool autofocus;
|
|
final FocusNode? focusNode;
|
|
final bool inBar;
|
|
final bool? cupertino;
|
|
|
|
void _clear() {
|
|
controller.clear();
|
|
if (onCleared case final cleared?) {
|
|
cleared();
|
|
} else {
|
|
onChanged?.call('');
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (cupertino ?? usesCupertinoHost(context)) {
|
|
if (kIsWeb) {
|
|
return CupertinoWebSearchField(
|
|
controller: controller,
|
|
placeholder: placeholder,
|
|
fieldKey: fieldKey,
|
|
focusNode: focusNode,
|
|
autofocus: autofocus,
|
|
inBar: inBar,
|
|
onChanged: onChanged,
|
|
onSubmitted: onSubmitted,
|
|
onCleared: _clear,
|
|
);
|
|
}
|
|
return CupertinoSearchTextField(
|
|
key: fieldKey,
|
|
controller: controller,
|
|
focusNode: focusNode,
|
|
autofocus: autofocus,
|
|
placeholder: placeholder,
|
|
backgroundColor: inBar ? const Color(0x00000000) : null,
|
|
onChanged: onChanged,
|
|
onSubmitted: onSubmitted,
|
|
onSuffixTap: _clear,
|
|
);
|
|
}
|
|
final scheme = Theme.of(context).colorScheme;
|
|
const radius = BorderRadius.all(Radius.circular(28));
|
|
const stadium = OutlineInputBorder(
|
|
borderRadius: radius,
|
|
borderSide: BorderSide.none,
|
|
);
|
|
return ValueListenableBuilder<TextEditingValue>(
|
|
valueListenable: controller,
|
|
builder: (context, value, _) => TextField(
|
|
key: fieldKey,
|
|
controller: controller,
|
|
focusNode: focusNode,
|
|
autofocus: autofocus,
|
|
textInputAction: TextInputAction.search,
|
|
onChanged: onChanged,
|
|
onSubmitted: onSubmitted,
|
|
decoration: InputDecoration(
|
|
hintText: placeholder,
|
|
prefixIcon: const Icon(Icons.search),
|
|
suffixIcon: value.text.isEmpty
|
|
? null
|
|
: IconButton(
|
|
key: clearKey,
|
|
tooltip: 'Clear search',
|
|
onPressed: _clear,
|
|
icon: const Icon(Icons.close),
|
|
),
|
|
filled: !inBar,
|
|
fillColor: scheme.surfaceContainerHigh,
|
|
isDense: true,
|
|
border: stadium,
|
|
enabledBorder: stadium,
|
|
// In the bar the pill is the field's outline; on a page the focused
|
|
// field shows the host's focus ring.
|
|
focusedBorder: inBar
|
|
? stadium
|
|
: OutlineInputBorder(
|
|
borderRadius: radius,
|
|
borderSide: BorderSide(color: scheme.primary, width: 2),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|