flutter / flutter/flutter

RawAutocomplete widget does not allow to invoke its intents/actions

Open
#145,758 1 comment 0 reactions 0 assignees View on GitHub
found in release: 3.19 found in release: 3.21 framework has reproducible steps P2 team-text-input triaged-text-input
Dominant language
Dart
Stars
179k
Forks
31.1k
PR merge metrics
PR metrics pending

Description

### Steps to reproduce

When the `RawAutocomplete` widget is created with the `fieldViewBuilder` parameter. The context passed to the `fieldViewBuilder` does not have access to the previous/next item intents registered by the `RawAutocomplete`, so the following call fail to find requested intent: `Actions.invoke(context, const AutocompleteNextOptionIntent());`

### Expected results

It would be really really useful if the `Actions.invoke(context, const AutocompleteNextOptionIntent());` call worked, so we could use it to activate the autocomplete dropdown using an UI action (e.g. IconButton) or map own keyboard shortcuts to these actions.

### Actual results

Currently the call is failing with "Unable to find an action for an Intent with type AutocompleteNextOptionIntent in an Actions widget in the given context." error. The complete example below also throws this error in the web-based DartPad.

I'm guessing this is because the parent context is passed to the `fieldViewBuilder`, but to make actions visible to the `fieldViewBuilder` the callback need to be wrapped with another `Builder` widget.

### Code sample

Code sample

```dart
import 'package:flutter/material.dart';

/// Flutter code sample for [RawAutocomplete].

void main() => runApp(const AutocompleteExampleApp());

class AutocompleteExampleApp extends StatelessWidget {
const AutocompleteExampleApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('RawAutocomplete Basic'),
),
body: const Center(
child: AutocompleteBasicExample(),
),
),
);
}
}

class AutocompleteBasicExample extends StatelessWidget {
const AutocompleteBasicExample({super.key});

static const List _options = [
'aardvark',
'bobcat',
'chameleon',
];

@override
Widget build(BuildContext context) {
return RawAutocomplete(
optionsBuilder: (TextEditingValue textEditingValue) {
return _options.where((String option) {
return option.contains(textEditingValue.text.toLowerCase());
});
},
fieldViewBuilder: (
BuildContext context,
TextEditingController textEditingController,
FocusNode focusNode,
VoidCallback onFieldSubmitted,
) {
return Row(children: [
Expanded(
child: TextFormField(
controller: textEditingController,
focusNode: focusNode,
onFieldSubmitted: (String value) {
onFieldSubmitted();
},
)),
IconButton(
icon: Icon(Icons.open_in_browser),
onPressed: () {
Actions.invoke(context, const AutocompleteNextOptionIntent());
},
),
]);
},
optionsViewBuilder: (
BuildContext context,
AutocompleteOnSelected onSelected,
Iterable options,
) {
return Align(
alignment: Alignment.topLeft,
child: Material(
elevation: 4.0,
child: SizedBox(
height: 200.0,
child: ListView.builder(
padding: const EdgeInsets.all(8.0),
itemCount: options.length,
itemBuilder: (BuildContext context, int index) {
final String option = options.elementAt(index);
return GestureDetector(
onTap: () {
onSelected(option);
},
child: ListTile(
title: Text(option),
),
);
},
),
),
),
);
},
);
}
}
```

### Screenshots or Video

N/A

### Logs

Logs

```console
The following assertion was thrown while handling a gesture:
Unable to find an action for an Intent with type AutocompleteNextOptionIntent in an Actions widget
in the given context.
Actions.invoke() was unable to find an Actions widget that contained a mapping for the given intent,
or the intent type isn't the same as the type argument to invoke (which is
AutocompleteNextOptionIntent - try supplying a type argument to invoke if one was not given)
The context used was:
RawAutocomplete(state: _RawAutocompleteState#ef3e7)
The intent type requested was:
AutocompleteNextOptionIntent

When the exception was thrown, this was the stack

Handler: "onTap"
Recognizer:
TapGestureRecognizer#d4e8e
```

### Flutter Doctor output

Doctor output

```console
flutter doctor --verbose
[✓] Flutter (Channel stable, 3.19.4, on macOS 13.6.3 22G436 darwin-arm64, locale en-CA)
• Flutter version 3.19.4 on channel stable at /Users/eu/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 68bfaea224 (6 days ago), 2024-03-20 15:36:31 -0700
• Engine revision a5c24f538d
• Dart version 3.3.2
• DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0-rc3)
• Android SDK at ...
• Platform android-34, build-tools 34.0.0-rc3
• Java binary at: /Applications/Android Studio Giraffe Canary.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.9-11255266)
• All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 14E300c
• CocoaPods version 1.15.2

[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] IntelliJ IDEA Ultimate Edition (version 2023.3.5)
• IntelliJ at /Applications/IntelliJ IDEA.app
• Flutter plugin version 78.4.2
• Dart plugin version 233.14888

[✓] Connected device (2 available)
• macOS (desktop) • macos • darwin-arm64 • macOS 13.6.3 22G436 darwin-arm64
• Chrome (web) • chrome • web-javascript • Google Chrome 123.0.6312.59

[✓] Network resources
• All expected network resources are available.
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.