大家好,我剛剛開始顫抖我正在進行一個小專案,準確地說是一個清單專案我已經創建了一個清單我將文本從串列加載到 ListTile,并且我已經加載了帶有答案的下拉按鈕,但是當我單擊下拉按鈕整個按鈕同時更改以及如何將下拉按鈕放在右側?(檢查 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();
}
}
uj5u.com熱心網友回復:
因為您正在使用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(() {});
},
);
}
其他注意事項:
不要像selectedItemValue.add("Not Done");在ListView.builder.
因為 builder 是在串列項出現在螢屏上時呼叫的,所以selectedItemValue會被添加太多次。initState所以在下面的函式中初始化串列。
@override
void initState() {
super.initState();
selectedItemValue.addAll(List.filled(propositions.length, "Not Done"));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/464482.html
下一篇:如何在顫動中將多個期貨合二為一
