我在下拉菜单中遇到值选择错误

I'm getting a value selection error in the dropdownmenu

当我点击应用中的添加新文档按钮时,我希望下拉菜单始终被重置。但是下拉项的价值是同步工作。当我单击添加新文档按钮时,如下图所示。

通常情况下,它应该在创建新容器时自行重置。

  //@dart=2.9

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

void main() => runApp(MainPage());

class MainPage extends StatelessWidget {
  const MainPage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: SwearPage());
  }
}

class SwearPage extends StatefulWidget {
  const SwearPage({Key key}) : super(key: key);

  @override
  _SwearPageState createState() => _SwearPageState();
}

class _SwearPageState extends State<SwearPage> {
  TextEditingController _phoneController = TextEditingController();
  bool basildi = false;
  int index = 0;

  // we initialize the list with same elements
  List<Map<String, dynamic>> variables = List.generate(
      5,
      (index) => {
            "rdValue": [0, 0, 0],
            "cbValue": [false, false, false],
          });
  List<bool> dosyaDili = [false, false, false, false, false];

  List<String> dropdownValue = [null, null, null, null, null];
  final List<String> items = [
    "passport conversion",
    "family wallet conversion",
    "University diploma conversion",
    "Conversion of high school diploma",
    "Vize conversation"
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    setState(() {
      dropdownValue;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "Translation Page",
          style: TextStyle(color: Colors.white),
        ),
        backgroundColor: Colors.orange,
      ),
      body: ListView(
        children: <Widget>[
          SizedBox(
            height: 20,
          ),
          SizedBox(
            height: 20,
          ),
          addContainer(0),
          dosyaDili[0] == true ? addContainer(1) : SizedBox(),
          dosyaDili[1] == true ? addContainer(2) : SizedBox(),
          dosyaDili[2] == true ? addContainer(3) : SizedBox(),
          dosyaDili[3] == true ? addContainer(4) : SizedBox(),
          SizedBox(
            height: 20,
          ),
          GestureDetector(
            onTap: () {
              setState(() {
                index++;

                if (index == 0) {
                  dosyaDili[0] = true;
                } else if (index == 1) {
                  dosyaDili[1] = true;
                } else if (index == 2) {
                  dosyaDili[2] = true;
                } else if (index == 3) {
                  dosyaDili[3] = true;
                } else if (index == 4) {
                  dosyaDili[4] = true;
                }
              });
            },
            child: Container(
              margin: EdgeInsets.fromLTRB(30, 0, 30, 0),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(20),
                color: Colors.orange,
              ),
              width: 100,
              height: 40,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  SizedBox(
                    width: 20,
                  ),
                  Icon(
                    Icons.add_circle,
                    color: Colors.black,
                    size: 30,
                  ),
                  SizedBox(
                    width: 70,
                  ),
                  Text(
                    "Add New Document",
                    style: TextStyle(
                        fontSize: 20,
                        fontWeight: FontWeight.bold,
                        color: Colors.white),
                  )
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget addContainer(int indx) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          SizedBox(
            height: 10,
          ),
          adddropDown(index)
        ],
      ),
    );
  }

  Row adddropDown(int i) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: <Widget>[
        SizedBox(
          width: 5,
        ),
        Text(
          "Document Type",
          style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
        ),
        SizedBox(
          width: 20,
        ),
        Container(
          width: 270,
          height: 45,
          decoration:
              BoxDecoration(border: Border.all(color: Colors.orange, width: 2)),
          child: DropdownButton<String>(
            iconSize: 30,
            icon: Icon(Icons.arrow_drop_down, color: Colors.black),
            isExpanded: true,
            value: dropdownValue[i],
            items: items.map(buildMenuItem).toList(),
            onChanged: (value) {
              this.dropdownValue[i] = value;
              setState(() {});
            },
          ),
        )
      ],
    );
  }

  DropdownMenuItem<String> buildMenuItem(String item) => DropdownMenuItem(
      value: item,
      child: Text(
        item,
        style: TextStyle(fontSize: 20),
      ));
}

此问题来自

上的拼写错误
  Widget addContainer(int indx) { ///< here passing `indx`
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          SizedBox(
            height: 10,
          ),
          adddropDown(index) //< but using class level index
        ],
      ),
    );
  }

只需将 int indx 重命名为 int index 就像 Widget addContainer(int index) {}

我更喜欢方法参数的不同名称,这样我们就可以避免这种情况。