简体   繁体   中英

how to get selected index of dropdownbutton in flutter

How to get selectedIndex of dropdown in flutter,

In dropdownbutton there is no property to get selected index, if there how to get the selected index and my code look like this:

new DropdownButton( hint:new Text("Select a users"),value: selectedUser,

              onChanged: (String newValue) {
                setState(() {
                  selectedUser = newValue;
                });
              },
              items: userInfoToMap.map((ListOfUsers value) {
                return new DropdownMenuItem<String>(
                    value: value.name,
                    child:new Text(value.name,style: new TextStyle(color: Colors.black))
                );
              })
                  .toList(),
            ),

          ),),

You should probably use a custom model object (eg User ) as the type for DropdownButton .

视频

import 'package:flutter/material.dart';

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

class User {
  const User(this.name);    
  final String name;
}

class MyApp extends StatefulWidget {
  State createState() => new MyAppState();
}

class MyAppState extends State<MyApp> {
  User selectedUser;
  List<User> users = <User>[User('Foo'), User('Bar')];    
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        body: new Center(
          child: new DropdownButton<User>(
            hint: new Text("Select a user"),
            value: selectedUser,
            onChanged: (User newValue) {
              setState(() {
                selectedUser = newValue;
              });
            },
            items: users.map((User user) {
              return new DropdownMenuItem<User>(
                value: user,
                child: new Text(
                  user.name,
                  style: new TextStyle(color: Colors.black),
                ),
              );
            }).toList(),
          ),
        ),
      ),
    );
  }
}

Similar to Collin Jackson's answer, you can simply use a List of Strings and check the indexOf to set the value, which might be preferable in some situations rather than making a User class. If you want to set an initial value set _user to an integer value when defined.

int _user;
...

var users = <String>[
  'Bob',
  'Allie',
  'Jason',
];

return new DropdownButton<String>(
  hint: new Text('Pickup on every'),
  value: _user == null ? null : users[_user],
  items: users.map((String value) {
    return new DropdownMenuItem<String>(
      value: value,
      child: new Text(value),
    );
  }).toList(),
  onChanged: (value) {
    setState(() {
      _user = users.indexOf(value);
    });
  },
);

This gives you selected index of your dropdown

userInfoToMap.indexOf(selectedUser);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM