Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReactiveDropdownMenuField #465

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

BenjiFarquhar
Copy link

Adds reactive version of DropdownMenu.

DropdownMenu is an updated version of DropdownButton that follows the Material 3 specification.

@vasilich6107
Copy link
Contributor

vasilich6107 commented Dec 7, 2024

Hey @BenjiFarquhar
There is already a separate package for that
https://pub.dev/packages/reactive_dropdown_menu

@BenjiFarquhar
Copy link
Author

BenjiFarquhar commented Dec 7, 2024

Hi @vasilich6107

Oh, I see. I think Flutter might have a bug in their one. It correctly gives an error when entering random text the first time, then after a selection, you can type anything without error

The example at https://pub.dev/packages/reactive_dropdown_menu:

I added a validator to demonstrate the behaviour:

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

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  FormGroup buildForm() => fb.group({
        'input': FormControl<ColorLabel>(value: null, validators: [Validators.required]),
      });

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      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: [
                    ReactiveDropdownMenu<ColorLabel, ColorLabel>(
                      formControlName: 'input',
                      
                      showErrors: (control) => control.invalid,
                      validationMessages: {
                        'required': (error) =>
                            'Please select the difficulty of preparing this dish from the dropdown menu',
                      },
                      dropdownMenuEntries: ColorLabel.values
                          .map<DropdownMenuEntry<ColorLabel>>(
                              (ColorLabel color) {
                        return DropdownMenuEntry<ColorLabel>(
                          value: color,
                          label: color.label,
                          enabled: color.label != 'Grey',
                          style: MenuItemButton.styleFrom(
                            foregroundColor: color.color,
                          ),
                        );
                      }).toList(),
                    ),
                    ElevatedButton(
                      child: const Text('Submit'),
                      onPressed: () {
                        if (form.valid) {
                          debugPrint(form.value.toString());
                        }
                      },
                    ),
                  ],
                );
              },
            ),
          ),
        ),
      ),
    );
  }
}

enum ColorLabel {
  blue('Blue', Colors.blue),
  pink('Pink', Colors.pink),
  green('Green', Colors.green),
  yellow('Orange', Colors.orange),
  grey('Grey', Colors.grey);

  const ColorLabel(this.label, this.color);
  final String label;
  final Color color;
}

// DropdownMenuEntry labels and values for the second dropdown menu.
enum IconLabel {
  smile('Smile', Icons.sentiment_satisfied_outlined),
  cloud(
    'Cloud',
    Icons.cloud_outlined,
  ),
  brush('Brush', Icons.brush_outlined),
  heart('Heart', Icons.favorite);

  const IconLabel(this.label, this.icon);
  final String label;
  final IconData icon;
}

Screen.Recording.2024-12-08.at.11.42.17.am.mov

With _ensureValueIsInEntries:

Screen.Recording.2024-12-08.at.11.56.30.am.mov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants