Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Change TextField Autocomplete Position

I'm using Flutter's Autocomplete but I would like to have it display above the TextField. Is this possible?

Here's the base setup:

Autocomplete<String>(
     optionsBuilder: (TextEditingValue textEditingValue) {
         if (textEditingValue.text == '') {
             return const Iterable<String>.empty();
         }

         if (textEditingValue.text.contains('...')) {
               return widget.myBloc.names
                   .where((String option) {
               return option.contains(
                    textEditingValue.text.toLowerCase());
               });
          } else {
             return const Iterable<String>.empty();
          }
      },
      onSelected: (String selection) {
         log('selection -> $selection');
      },
      optionsMaxHeight: 40,
      fieldViewBuilder: (context, controller, focusNode,
                              onEditingComplete) {
           return TextField();
      }
  )
like image 821
Luke Irvin Avatar asked Aug 13 '26 07:08

Luke Irvin


1 Answers

I figured it out, but you're not gonna like the answer...

In the Flutter library, on line 367 of the RawAutocomplete implementation, they hardcoded its alignment to track the bottom left of the TextField. I was able to circumvent this only after copying their entire implementation, and doing the following:

  1. change line 367 to targetAnchor: Alignment.topLeft
  2. add parameter followerAnchor: Alignment.bottomLeft
  3. in your OptionViewsBuilder wherever you decide to implement it, shown here in the material Autocomplete implementation, change to alignment: Alignment.bottomLeft.
like image 77
Edward Perpich Avatar answered Aug 19 '26 23:08

Edward Perpich