flutter下拉按钮选择问题

发布于 2025-01-23 15:47:18 字数 3053 浏览 0 评论 0原文

大家好,我刚刚开始flutter,我正在一个小项目中单击dropdownbutton,整个按钮同时更改,以及如何将下拉餐点放在右边? (检查GIF)

“在此处输入图像描述”

,我想让文本在单元格内部更可读,这是代码,并且要预先感谢。

   import 'package:flutter/material.dart';

class Check_lists extends StatefulWidget {
  String sug_one;
  String sug_two;
  Check_lists(this.sug_one, this.sug_two);

  @override
  State<Check_lists> createState() => _Check_listsState();
}

class _Check_listsState extends State<Check_lists> {
  String dropdownValue = "Not Done";
  int index = 0;
  List<String> propositions = <String>[
    "Single line spacing",
    "Title set below the 2 top margin",
    "Title not exceeding 15 words (including articles,prepositions and conjunctions)",
    "Title in uppercase",
    "Title arranged in an inverted pyramid",
    "All parts centered, and in boldface",
    "4 line skips (5 enter presses) between parts",
    "First name, middle initial(s), last name format for the author’s names",
    "Leader's name followed by members names",
    "alphabetized by surname",
    "Date of proposal stated as month, day, year",
    "Start of pagination"
  ];
  //List<String> answer = ["Done", "partially Done", "Not Done"];
  List<String> selectedItemValue = <String>[];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: const Color.fromRGBO(82, 113, 255, 25),
          title: Row(
            children: [
              Container(
                  padding: const EdgeInsets.all(10.0),
                  child: const Text('CHEKILI Table of Content'))
            ],
          ),
        ),
        body: Column(
          children: <Widget>[
            Flexible(
              child: ListView.builder(
                itemCount: propositions.length,
                itemExtent: 50.0,
                itemBuilder: (BuildContext context, int index) {
                  for (int i = 0; i < propositions.length; i++) {
                    selectedItemValue.add("Not Done");
                  }
                  return ListTile(
                    title: Text("${propositions[index]}"),
                    leading: _dropdown(),
                  );
                },
              ),
            ),
          ],
        ));
  }

  Widget _dropdown() {
    return DropdownButton<String>(
      isExpanded: false,
      value: selectedItemValue[index].toString(),
      items: _dropDownItem(),
      onChanged: (value) {
        selectedItemValue[index] = value as String;
        setState(() {});
      },
    );
  }

  List<DropdownMenuItem<String>> _dropDownItem() {
    List<String> ddl = ["Not Done", "Partially Done", "Done"];
    return ddl
        .map((value) => DropdownMenuItem(
              value: value,
              child: Text(value),
            ))
        .toList();
  }
}

hello guys i'm just starting flutter i'm on a little project , a checklist project to be precise i have created a checklist i loaded the text from a List to a ListTile and i've loaded the dropdownbutton with answers , but when i click on on dropdownbutton the whole button get changed on the same time and how to put the dropdownbutton on the right ?? (check the gif)

enter image description here

and i want to make the text loaded more readable inside the cells , here is the code , and thanks in advance.

   import 'package:flutter/material.dart';

class Check_lists extends StatefulWidget {
  String sug_one;
  String sug_two;
  Check_lists(this.sug_one, this.sug_two);

  @override
  State<Check_lists> createState() => _Check_listsState();
}

class _Check_listsState extends State<Check_lists> {
  String dropdownValue = "Not Done";
  int index = 0;
  List<String> propositions = <String>[
    "Single line spacing",
    "Title set below the 2 top margin",
    "Title not exceeding 15 words (including articles,prepositions and conjunctions)",
    "Title in uppercase",
    "Title arranged in an inverted pyramid",
    "All parts centered, and in boldface",
    "4 line skips (5 enter presses) between parts",
    "First name, middle initial(s), last name format for the author’s names",
    "Leader's name followed by members names",
    "alphabetized by surname",
    "Date of proposal stated as month, day, year",
    "Start of pagination"
  ];
  //List<String> answer = ["Done", "partially Done", "Not Done"];
  List<String> selectedItemValue = <String>[];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: const Color.fromRGBO(82, 113, 255, 25),
          title: Row(
            children: [
              Container(
                  padding: const EdgeInsets.all(10.0),
                  child: const Text('CHEKILI Table of Content'))
            ],
          ),
        ),
        body: Column(
          children: <Widget>[
            Flexible(
              child: ListView.builder(
                itemCount: propositions.length,
                itemExtent: 50.0,
                itemBuilder: (BuildContext context, int index) {
                  for (int i = 0; i < propositions.length; i++) {
                    selectedItemValue.add("Not Done");
                  }
                  return ListTile(
                    title: Text("${propositions[index]}"),
                    leading: _dropdown(),
                  );
                },
              ),
            ),
          ],
        ));
  }

  Widget _dropdown() {
    return DropdownButton<String>(
      isExpanded: false,
      value: selectedItemValue[index].toString(),
      items: _dropDownItem(),
      onChanged: (value) {
        selectedItemValue[index] = value as String;
        setState(() {});
      },
    );
  }

  List<DropdownMenuItem<String>> _dropDownItem() {
    List<String> ddl = ["Not Done", "Partially Done", "Done"];
    return ddl
        .map((value) => DropdownMenuItem(
              value: value,
              child: Text(value),
            ))
        .toList();
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凶凌 2025-01-30 15:47:18

因为您使用的是index类的变量。

将索引传递到_Dropdown功能如下。

@override
Widget build(BuildContext context) {
  return Scaffold(
      appBar: AppBar(
        backgroundColor: const Color.fromRGBO(82, 113, 255, 25),
        title: Row(
          children: [
            Container(
                padding: const EdgeInsets.all(10.0),
                child: const Text('CHEKILI Table of Content'))
          ],
        ),
      ),
      body: Column(
        children: <Widget>[
          Flexible(
            child: ListView.builder(
              itemCount: propositions.length,
              itemExtent: 50.0,
              itemBuilder: (BuildContext context, int index) {
                for (int i = 0; i < propositions.length; i++) {
                  selectedItemValue.add("Not Done");
                }
                return ListTile(
                  title: Text("${propositions[index]}"),
                  leading: _dropdown(index), // <-- HERE
                );
              },
            ),
          ),
        ],
      ));
}

Widget _dropdown(int index) { // <-- HERE
  return DropdownButton<String>(
    isExpanded: false,
    value: selectedItemValue[index].toString(),
    items: _dropDownItem(),
    onChanged: (value) {
      selectedItemValue[index] = value as String;
      setState(() {});
    },
  );
}

其他注意事项:

不要将循环放置在列表中添加一些值,例如selectionItemvalue.add(“ not done”);listView.builder中。

由于列表项目出现在屏幕上时,因此调用了构建器,因此selectedItemvalue将被添加过多。
因此,在initstate函数下启动列表如下。

@override
void initState() {
  super.initState();
  selectedItemValue.addAll(List.filled(propositions.length, "Not Done"));
}

Because you are using the index variable of the class.

Pass the index to the _dropdown function like below.

@override
Widget build(BuildContext context) {
  return Scaffold(
      appBar: AppBar(
        backgroundColor: const Color.fromRGBO(82, 113, 255, 25),
        title: Row(
          children: [
            Container(
                padding: const EdgeInsets.all(10.0),
                child: const Text('CHEKILI Table of Content'))
          ],
        ),
      ),
      body: Column(
        children: <Widget>[
          Flexible(
            child: ListView.builder(
              itemCount: propositions.length,
              itemExtent: 50.0,
              itemBuilder: (BuildContext context, int index) {
                for (int i = 0; i < propositions.length; i++) {
                  selectedItemValue.add("Not Done");
                }
                return ListTile(
                  title: Text("${propositions[index]}"),
                  leading: _dropdown(index), // <-- HERE
                );
              },
            ),
          ),
        ],
      ));
}

Widget _dropdown(int index) { // <-- HERE
  return DropdownButton<String>(
    isExpanded: false,
    value: selectedItemValue[index].toString(),
    items: _dropDownItem(),
    onChanged: (value) {
      selectedItemValue[index] = value as String;
      setState(() {});
    },
  );
}

Other notes:

Don't place the loop that adding some value to the list like selectedItemValue.add("Not Done"); in the ListView.builder.

Because builder is called when the list item appears on the screen, the selectedItemValue will be added too many times.
So init the list at the initState function like below.

@override
void initState() {
  super.initState();
  selectedItemValue.addAll(List.filled(propositions.length, "Not Done"));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文