buildSearchBar method

AppBar buildSearchBar(
  1. BuildContext context
)

Builds the search bar!

The leading will always be a back button. backgroundColor is determined by the value of inBar title is always a TextField with the key 'SearchBarTextField', and various text stylings based on inBar. This is also where onSubmitted has its listener registered.

Implementation

AppBar buildSearchBar(BuildContext context) {
  ThemeData theme = Theme.of(context);
  Color? buttonColor = inBar ? null : theme.iconTheme.color;

  return AppBar(
    leading: IconButton(
        icon: const BackButtonIcon(),
        color: buttonColor,
        tooltip: MaterialLocalizations.of(context).backButtonTooltip,
        onPressed: () {
          onClosed?.call();
          controller!.clear();
          Navigator.maybePop(context);
        }),
    backgroundColor: inBar ? null : theme.canvasColor,
    title: Directionality(
      textDirection: Directionality.of(context),
      child: TextField(
        key: Key('SearchBarTextField'),
        keyboardType: TextInputType.text,
        decoration: InputDecoration(
            hintText: hintText,
            hintStyle: inBar
                ? null
                : TextStyle(
              color: theme.textTheme.headline4!.color,
            ),
            enabledBorder: InputBorder.none,
            focusedBorder: InputBorder.none,
            border: InputBorder.none),
        onChanged: this.onChanged,
        onSubmitted: (String val) async {
          if (closeOnSubmit) {
            await Navigator.maybePop(context);
          }

          if (clearOnSubmit) {
            controller!.clear();
          }
          onSubmitted?.call(val);
        },
        autofocus: true,
        controller: controller,
      ),
    ),
    actions: !showClearButton
        ? null
        : <Widget>[
      // Show an icon if clear is not active, so there's no ripple on tap
      IconButton(
          icon: Icon(Icons.clear),
          color: inBar ? null : buttonColor,
          disabledColor: inBar ? null : theme.disabledColor,
          onPressed: !_clearActive
              ? null
              : () {
            onCleared?.call();
            controller!.clear();
          }),
    ],
  );
}