【问题标题】:how to get selected index of dropdownbutton in flutter如何在颤动中获取下拉按钮的选定索引
【发布时间】:2017-09-20 13:40:29
【问题描述】:

如何在flutter中获取下拉列表的selectedIndex,

在下拉按钮中没有获取选定索引的属性,如果有如何获取选定索引,我的代码如下所示:

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(),
            ),

          ),),

【问题讨论】:

  • 不应该 selectedUser 是一个全局变量。当您选择一个项目时,会调用 onChange 吗?
  • 你能给我举个例子吗谢谢你的回复
  • userInfoToMap.indexOf(newValue) 呢?
  • 如果我有多个相同的元素,那么它将失败。
  • 您可以将它们包装在对象中,然后每个都会不同(如果对象没有自定义== 实现)

标签: dart flutter


【解决方案1】:

与 Collin Jackson 的回答类似,您可以简单地使用字符串列表并检查 indexOf 来设置值,这在某些情况下可能比创建 User 类更可取。 如果要设置初始值,请将 _user 设置为定义时的整数值。

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);
    });
  },
);

【讨论】:

  • 我正在为知识体系做出贡献,以便其他人受益
  • 这行代码解决了我的问题 'value: _user == null ? null : users[_user],' 谢谢 :)。
  • 当我们有多个具有相同字符串的值时,这可能不起作用。假设我们有 var users = [ 'Bob', 'Bob Martins', 'Allie', 'Jason', 'Jason Derulo'];该方法将是错误的
  • 当我尝试做同样的事情,从下拉列表项中获取索引时,我得到 RangeError (index): Invalid value: Not inclusive range 0..5: -1.
  • 对于文本值未知的本地化很有用。
【解决方案2】:

您可能应该使用自定义模型对象(例如User)作为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(),
          ),
        ),
      ),
    );
  }
}

【讨论】:

  • 如何设置初始值?
  • 供将来参考,如果人们正在寻找解决方案。您可以通过在DropdownButton 中提供value 参数中的值来设置初始值。您需要确保您的 value 参数与列表匹配。
  • 那么如何获取索引呢?我还是没听懂。
  • 回答你的问题@KamrulHasanJony,它是一个自定义对象的列表。这就是我需要的,因为我使用的是本地化,而且不同语言的列表不同。
猜你喜欢
  • 2021-02-08
  • 2020-12-16
  • 2021-03-21
  • 2021-09-17
  • 2021-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多