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

How to change DropdownButton's selected index in Flutter

我想在页面加载时更改 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(),
                                                      )
                                                  )
                                              )

如果您希望 select DropDown 页面加载时的默认值,您可以全局声明列表,然后设置默认值 在如下调用小部件之前:

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:

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