AutoComplete Widget and Attributes

AutoComplete Widget and Attributes

ยท

3 min read

Efficient data input is a key aspect of user interaction in any Flutter application. The AutoComplete widget provides a seamless way to implement auto-complete functionality, offering users suggestions as they input text. In this blog post, we'll explore the attributes of the AutoComplete widget and provide a detailed example to illustrate its implementation.

AutoComplete Widget in Flutter

The AutoComplete widget simplifies the process of providing suggestions to users as they type, enhancing the overall user experience. It's particularly useful in scenarios where users need assistance or suggestions while entering text.

Attributes of AutoComplete Widget:

  1. options: A list of options to be displayed as suggestions. These can be strings or custom objects representing the suggestions.

  2. onSelected: A callback function triggered when a suggestion is selected. Developers can use this callback to perform actions based on the selected suggestion.

  3. fieldViewBuilder: A function defining the appearance of the text input field. This allows developers to customize the input field's appearance and behavior.

Full Example of AutoComplete Widget:

Let's dive into a comprehensive example where we create a Flutter app with an AutoComplete widget. We'll use a simple list of fruits as suggestions, and when a user selects a fruit, we'll print the selected fruit to the console.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  TextEditingController _textEditingController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter AutoComplete Example'),
      ),
      body: Center(
        child: AutoComplete(
          options: ['Apple', 'Banana', 'Orange', 'Mango', 'Grapes'],
          onSelected: (value) {
            // Perform actions based on the selected suggestion
            print('Selected: $value');
          },
          fieldViewBuilder: (context, textEditingController, focusNode, onFieldSubmitted) {
            return TextField(
              controller: textEditingController,
              focusNode: focusNode,
              onSubmitted: onFieldSubmitted,
              decoration: InputDecoration(
                labelText: 'Fruit',
                hintText: 'Type a fruit name',
              ),
            );
          },
        ),
      ),
    );
  }
}

class AutoComplete extends StatefulWidget {
  final List<String> options;
  final Function(String) onSelected;
  final Function(BuildContext, TextEditingController, FocusNode, Function(String)) fieldViewBuilder;

  AutoComplete({required this.options, required this.onSelected, required this.fieldViewBuilder});

  @override
  _AutoCompleteState createState() => _AutoCompleteState();
}

class _AutoCompleteState extends State<AutoComplete> {
  late TextEditingController _textEditingController;
  late FocusNode _focusNode;

  @override
  void initState() {
    super.initState();
    _textEditingController = TextEditingController();
    _focusNode = FocusNode();
  }

  @override
  Widget build(BuildContext context) {
    return widget.fieldViewBuilder(context, _textEditingController, _focusNode, (value) {
      // Handle submitted value (optional)
    });
  }

  @override
  void dispose() {
    _textEditingController.dispose();
    _focusNode.dispose();
    super.dispose();
  }
}

In this example:

  • We create a Flutter app with an AppBar and a Center widget hosting an AutoComplete widget.

  • The AutoComplete widget has a list of fruit names as options and a callback function (onSelected) to handle the selected suggestion.

  • The appearance and behavior of the text input field are customized using the fieldViewBuilder attribute.

By incorporating the AutoComplete widget, developers can streamline text input for users and provide helpful suggestions, enhancing the overall usability of their Flutter applications. Feel free to customize the options, appearance, and behavior to suit the design and requirements of your app.

Did you find this article valuable?

Support Vinit Mepani (Flutter Developer) by becoming a sponsor. Any amount is appreciated!

ย