reactive_raw_autocomplete 2.0.2 copy "reactive_raw_autocomplete: ^2.0.2" to clipboard
reactive_raw_autocomplete: ^2.0.2 copied to clipboard

Wrapper around RawAutocomplete to use with reactive_forms

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:reactive_forms/reactive_forms.dart';
import 'package:reactive_raw_autocomplete/reactive_raw_autocomplete.dart';

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

List<String> _options = <String>[
  'aardvark',
  'bobcat',
  'chameleon',
];

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

  FormGroup buildForm() => fb.group({
        'input': FormControl<String>(value: null),
      });

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: Scaffold(
        appBar: AppBar(),
        body: SafeArea(
          child: SingleChildScrollView(
            physics: const BouncingScrollPhysics(),
            padding: const EdgeInsets.symmetric(
              horizontal: 20.0,
              vertical: 20.0,
            ),
            child: ReactiveFormBuilder(
              form: buildForm,
              builder: (context, form, child) {
                return Column(
                  children: [
                    ReactiveRawAutocomplete<String, String>(
                      formControlName: 'input',
                      // options: _options,
                      optionsBuilder: (TextEditingValue textEditingValue) {
                        return _options.where((String option) {
                          return option
                              .contains(textEditingValue.text.toLowerCase());
                        });
                      },
                      autocompleteOnSubmit: true,
                      optionsViewBuilder: (BuildContext context,
                          AutocompleteOnSelected<String> onSelected,
                          Iterable<String> options) {
                        final selectedIndex =
                            AutocompleteHighlightedOption.of(context);

                        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),
                                      selected: selectedIndex == index,
                                    ),
                                  );
                                },
                              ),
                            ),
                          ),
                        );
                      },
                    ),
                    const SizedBox(height: 16),
                    ElevatedButton(
                      child: const Text('Sign Up'),
                      onPressed: () {
                        if (form.valid) {
                          // ignore: avoid_print
                          print(form.value);
                        } else {
                          form.markAllAsTouched();
                        }
                      },
                    ),
                  ],
                );
              },
            ),
          ),
        ),
      ),
    );
  }
}
6
likes
0
points
4.62k
downloads

Publisher

unverified uploader

Weekly Downloads

Wrapper around RawAutocomplete to use with reactive_forms

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, reactive_forms

More

Packages that depend on reactive_raw_autocomplete