flutter_form_builder icon indicating copy to clipboard operation
flutter_form_builder copied to clipboard

[FormBuilderDropdown] selectedItemBuilder property is broken

Open socialviolation opened this issue 3 years ago • 3 comments

Bug in FormBuilderDropdown when using the selectedItemBuilder property.

Example Dropdown component:

return FormBuilderDropdown(
      name: 'groupBy',
      decoration: InputDecoration(labelText: 'Group By'),
      onChanged: (dynamic value) => this.onChanged(value as HoldingGroupOption),
      initialValue: grouping,
      hint: Text('Group By'),
      // validator: FormBuilderValidators.compose([FormBuilderValidators.required(context)]),
      items: items,
      selectedItemBuilder: (_) => [
        Text('test')
      ],
    );

Error thrown:

The following assertion was thrown during paint():
'package:flutter/src/rendering/stack.dart': Failed assertion: line 716 pos 12: 'i == index': is not true.

Error does not appear when property is not present.

socialviolation avatar Jan 07 '22 04:01 socialviolation

@WilliamCunhaCardoso, try to do something like this.

   selectedItemBuilder: (_) => items
          .map((e) => Text(
                e.toString(),
              ))
          .toList(),

Docs got example: /// This sample shows a DropdownButton with a dropdown button text style /// that is different than its menu items. ///

List<String> options = <String>['One', 'Two', 'Free', 'Four'];
String dropdownValue = 'One';

@override
Widget build(BuildContext context) {
  return Container(
    alignment: Alignment.center,
    color: Colors.blue,
    child: DropdownButton<String>(
     value: dropdownValue,
     onChanged: (String newValue) {
       setState(() {
        dropdownValue = newValue;
        });
      },
      style: TextStyle(color: Colors.blue),
      selectedItemBuilder: (BuildContext context) {
        return options.map((String value) {
          return Text(
            dropdownValue,
            style: TextStyle(color: Colors.white),
          );
        }).toList();
      },
      items: options.map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(value),
        );
      }).toList(),
    ),
  );
}

feduke-nukem avatar Apr 13 '22 14:04 feduke-nukem

Hi @socialviolation The solution of @coccyx-cyst works for you?

deandreamatias avatar Jul 14 '22 15:07 deandreamatias

Due to lack of response, I will close this issue. If the bug still exists, feel free to open a new issue

deandreamatias avatar Sep 05 '22 08:09 deandreamatias