0

I am expecting the textfield to display "A" / "B" / etc upon selection, however it is returning the index "0" / "1" / "2" / ... only, why ???

full widget code below:

UPDATED:

  int selectedValue;

  Future showPicker() async {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return CupertinoPicker(
            backgroundColor: Colors.white,
            scrollController: FixedExtentScrollController(initialItem: 3),
            onSelectedItemChanged: (value) {
              setState(() {
                if (value != null) {
                  selectedValue = listTextValues[value];
                  regionController.value = TextEditingValue(text: selectedValue.toString());
                }
              });
            },
            itemExtent: 32.0,
            children: const listTextValues = [
              Text('Eastern'),
              Text('South'),
              Text('West'),
              Text('North'),
              Text('Island'),
            ],
          );
        }
        );

  }

Below code is workable, just wonder how to simplify the code as i am using 2 list, one for picker and one for mapping the index

Solution:

    int selectedValue;
  final List<String> listTextValues = ['Eastern', 'South', 'West', 'North', 'Island'];

  Future showPicker() async {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return CupertinoPicker(
            backgroundColor: Colors.white,
            scrollController: FixedExtentScrollController(initialItem: 3),
            onSelectedItemChanged: (value) {
              setState(() {
                if (value != null) {
                  selectedValue = value;
                  regionController.value = TextEditingValue(text: listTextValues[selectedValue].toString());
                }
              });
            },
            itemExtent: 32.0,
            children: const [
              Text('Eastern'),
              Text('South'),
              Text('West'),
              Text('North'),
              Text('Island'),
            ],
          );
        }
        );

  }
icantcode
  • 142
  • 1
  • 15

1 Answers1

2

With the method onSelectedItemChanged: (value), the "value" is the selected index.
You have to store your list of letters in a variable in your widget

const listTextValues = [
      Text('A'),
      Text('B'),
      Text('C'),
      Text('D'),
      Text('E'),
    ];

and get the item at the selected index in the onSelectedItemChanged implementation

selectedValue = listTextValues[value];

Edit

The final result should look like the following

int selectedValue;
final List<Widget> listTextValues = [
              Text('Eastern'),
              Text('South'),
              Text('West'),
              Text('North'),
              Text('Island'),
            ];

  Future showPicker() async {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return CupertinoPicker(
            backgroundColor: Colors.white,
            scrollController: FixedExtentScrollController(initialItem: 3),
            onSelectedItemChanged: (value) {
              setState(() {
                if (value != null) {
                  selectedValue = listTextValues[value];
                  regionController.value = TextEditingValue(text: selectedValue.toString());
                }
              });
            },
            itemExtent: 32.0,
            children: listTextValues,
          );
        }
        );

  }
FDuhen
  • 4,097
  • 1
  • 15
  • 26
  • now i got another error : Missing selector such as '.identifier' or '[0]'. – icantcode Apr 04 '21 at 09:04
  • Could you edit your answer showing your new implementation ? – FDuhen Apr 04 '21 at 09:12
  • @icantcode updated the answer to help you understand how to fix your problem :) – FDuhen Apr 04 '21 at 09:22
  • Error: Only static fields can be declared as const. Try using 'final' instead of 'const', or adding the keyword 'static'. const List listTextValues = [ ^^^^^ Error: A value of type 'Widget' can't be assigned to a variable of type 'int'. – icantcode Apr 04 '21 at 09:25
  • Your error here is pretty explicit, just replace the keyword const by final. The point is, you have to extract your list of Text Widgets (or juste your list of Strings, which would be cleaner) outside from your widget tree so your callback method onSelectedItemChanged can access this list – FDuhen Apr 04 '21 at 09:32