如何在Flutter中更改DropdownButton的选定索引

问题描述 投票:0回答:1

我想在页面加载时更改我的DropdownButton选择索引,我该怎么做? 我的DropdownButtonPlateType对象列表,我需要将所选索引更改为某个索引,这意味着旧用户选择。以下是我的代码:

DropdownButtonHideUnderline(
                                                  child: ButtonTheme(
                                                      alignedDropdown: false,
                                                      child: new DropdownButton<PlateType>(
                                                        hint: new Text(
                                                          "حرف",
                                                          textAlign: TextAlign.center,
                                                          style: TextStyle(color: Colors.white),
                                                        ),
                                                        value: selectedPlate,
                                                        isExpanded: false,
                                                        iconSize: 30,
                                                        style: new TextStyle(
                                                            color: Colors.white,fontFamily: 'iransans',
                                                        ),
                                                        onChanged: (PlateType pt) {
                                                          setState(() {
                                                            selectedPlate = pt;
                                                          });
                                                        },
                                                        items: plates.map((PlateType p) {
                                                          return new DropdownMenuItem<PlateType>(
                                                            value: p,
                                                            child: new Text(
                                                              p.name,
                                                              textAlign: TextAlign.center,
                                                              style: new TextStyle(color: Colors.black),
                                                            ),
                                                          );
                                                        }).toList(),
                                                      )
                                                  )
                                              )
flutter selectedindex dropdownbutton
1个回答
1
投票

如果要为DropDown选择页面加载的默认值,可以全局声明列表,然后在调用widget之前设置默认值,如下所示:

import 'package:flutter/material.dart';
import 'package:flutter_app/models/User.dart';

List<User> userList = new List<User>();
User selectedUser;

class MyDropDownForm extends StatefulWidget {
  createState() {
    userList.add(new User(name: 'ABC', age: '25'));
    userList.add(new User(name: 'SDF', age: '24'));
    userList.add(new User(name: 'FGDG', age: '23'));
    userList.add(new User(name: 'PQR', age: '28'));
    userList.add(new User(name: 'XFGDF', age: '29'));
    userList.add(new User(name: 'WWW', age: '26'));
    userList.add(new User(name: 'HHH', age: '30'));
    userList.add(new User(name: 'RFD', age: '35'));
    selectedUser = userList[0];

    return StateKeeper();
  }
}

class StateKeeper extends State<MyDropDownForm> {
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {

    return Container(
        color: Colors.white,
        child: Center(
            child: Form(

              key: _formKey,
              child: Column(

                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[

                  DropdownButtonHideUnderline(
                      child: ButtonTheme(
                          alignedDropdown: false,
                          child: new DropdownButton<User>(
                            hint: new Text(
                              "حرف",
                              textAlign: TextAlign.center,
                              style: TextStyle(color: Colors.white),
                            ),
                            value: selectedUser,
                            isExpanded: false,
                            iconSize: 30,
                            style: new TextStyle(
                              color: Colors.white,fontFamily: 'iransans',
                            ),
                            onChanged: (User pt) {
                              setState(() {
                                selectedUser = pt;
                                print("Selected user " + selectedUser.name);
                              });
                            },
                            items: userList.map((User p) {
                              return new DropdownMenuItem<User>(
                                value: p,
                                child: new Text(
                                  p.name,
                                  textAlign: TextAlign.center,
                                  style: new TextStyle(color: Colors.black),
                                ),
                              );
                            }).toList(),
                          )
                      )
                  )


                ],

              ),

            ),
        ),

    );
  }

}

用户模型类:

class User {
  String name;
  String age;

  User({
    this.name,
    this.age,
  });

  factory User.fromJson(Map<String, dynamic> json) => new User(
    name: json["name"],
    age: json["age"],
  );

  Map<String, dynamic> toJson() => {
    "name": name,
    "age": age,
  };
}
© www.soinside.com 2019 - 2024. All rights reserved.