![](/img/trans.png)
[英]why my list not updated after get value from textfield flutter
[英]Why are my checkboxes unchecking themselves after a second on Flutter?
這就是它應該發生的事情:
我有幾個 Month 類的類別,其中有一個 FileCell 類的實例列表,Month 有一個 bool 檢查屬性,當更改時,也會更改其列表中每個 FileCell 的 bool toTransfer 屬性。 這兩個屬性最初都設置為 false。
復選框代表這些 'check' 和 'toTransfer' 屬性,但在單擊它們並更改它們一秒鍾后,它們會變回 false。
我的目標是:我點擊它們,它們就會原地不動,但這不再發生了。 有人幫我解決這個問題嗎?
是的,我是新來的顫振。
PS:(忽略葡萄牙語部分)
@override Widget build(BuildContext context) { SessionTimer sessionTimer = SessionTimer(); sessionTimer.startTimer(); return loading ? Loading(status: statusMessage) : Container( child: Scaffold( appBar: AppBar( title: const Text("Meus ficheiros"), ), backgroundColor: Colors.white, body: Column( children: [ Container( child: ButtonBar( alignment: MainAxisAlignment.spaceEvenly, children: [ TextButton.icon( icon: const Icon(Icons.refresh), label: const Text("Reiniciar"), onPressed: (){ setState(() {}); //setState(() => futureFiles = _storage.getAllUserFiles(user.uid)); }, ), TextButton.icon( icon: const Icon(Icons.add), label: const Text("Enviar"), onPressed: (){ UploadFile(); }, ), TextButton.icon( icon: const Icon(Icons.download), label: const Text("Transferir"), onPressed: (){ setState(() {visCheckBoxes = !visCheckBoxes;}); }, ), ], ), decoration: const BoxDecoration( borderRadius: BorderRadius.only( bottomRight: Radius.circular(18), bottomLeft: Radius.circular(18), ), gradient: LinearGradient( begin: Alignment.topCenter, end: Alignment.bottomCenter, stops: [0.8,1], colors: [ Colors.black, Colors.transparent, ], ) ), ), Visibility( visible: visCheckBoxes, child: Column( children: [ Padding( padding: const EdgeInsets.only(top:8.0), child: const Text("Selecione os ficheiros para transferência", style: TextStyle(fontSize: 18)), ), ColoredBox(color: Colors.black, child: Container(height: 1)), ], ), ), Expanded( child: FutureBuilder<ListResult>( future: _storage.getAllUserFiles(user.uid), builder: (BuildContext context,AsyncSnapshot<ListResult> snapshot) { if (snapshot.hasData ) { //List<Reference> files = List<Reference>.empty(growable: true); if(snapshot.data!=null){ if(loading){ setState(() { loading = false; statusMessage = ""; }); } allUserFiles = snapshot.data!.items; if(allUserFiles.isEmpty){ return const Center( child: Text( "Não existem ficheiros na sua área" ) ); } return FutureBuilder<List<Month>>( future: SeparateLists(allUserFiles), builder: (BuildContext context,AsyncSnapshot<List<Month>> snapshot) { if(snapshot.hasData){ if(loading){ setState(() { loading = false; statusMessage = ""; }); } listsByMonth = snapshot.data!; //List of months return ListView.builder( itemCount: listsByMonth.length, itemBuilder: (context, indexMonth) { //TODO Fix check boxes Month month = listsByMonth[indexMonth]; return ExpansionTile( title: visCheckBoxes? Row( children: [ Text(month.name), Checkbox( value: listsByMonth[indexMonth].checked, onChanged: (value){ if(value!=null) { setState(() => listsByMonth[indexMonth].checked = value); } }), ], ) : Text(month.name), initiallyExpanded: true, children: [ //List of the files uploaded in that month Padding( padding: const EdgeInsets.only(left:10.0), child: ListView.builder( key: new Key("list_${month.name}"), shrinkWrap: true, physics: const NeverScrollableScrollPhysics(), itemCount: month.files.length, itemBuilder: (context, indexFile) { FileCell fileCell = month.files[indexFile]; return ListTile( title: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text("${fileCell.name}"), Padding( padding: const EdgeInsets.only(left:8.0), child: Text(DateFormat('dd-MM-yyyy – kk:mm').format(fileCell.timeCreated!)), ), ], ), trailing: visCheckBoxes? Checkbox( value: month.files[indexFile].toTransfer, onChanged: (value)=>{ if(value!=null) setState(()=>listsByMonth[indexMonth].files[indexFile].toTransfer = value), }, ) : null, subtitle: fileCell.downloadProgress != null ? LinearProgressIndicator( valueColor: const AlwaysStoppedAnimation<Color>(Colors.greenAccent), value: fileCell.downloadProgress, ): null, ); }, ), ) ], ); }, ); } return Loading(status: "A carregar os ficheiros"); }, ); } else{ setState(() { statusMessage = "A carregar os ficheiros"; loading = true; }); //setState(()=> loading = true); return Stack(); } } else if (snapshot.hasError) { return const Center( child: Text("Ocorreu um erro a carregar os ficheiros")); } else { return Stack(); } } ), ), ], ),
sessionTimer.startTimer();
return loading
? Loading(status: statusMessage)
: Container(
child: Scaffold(
appBar: AppBar(
title: const Text("Meus ficheiros"),
),
backgroundColor: Colors.white,
body: Column(
children: [
Container(
child: ButtonBar(
alignment: MainAxisAlignment.spaceEvenly,
children: [
TextButton.icon(
icon: const Icon(Icons.refresh),
label: const Text("Reiniciar"),
onPressed: (){
setState(() {});
//setState(() => futureFiles = _storage.getAllUserFiles(user.uid));
},
),
TextButton.icon(
icon: const Icon(Icons.add),
label: const Text("Enviar"),
onPressed: (){
UploadFile();
},
),
TextButton.icon(
icon: const Icon(Icons.download),
label: const Text("Transferir"),
onPressed: (){
setState(() {visCheckBoxes = !visCheckBoxes;});
},
),
],
),
decoration: const BoxDecoration(
borderRadius: BorderRadius.only(
bottomRight: Radius.circular(18),
bottomLeft: Radius.circular(18),
),
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
stops: [0.8,1],
colors: [
Colors.black,
Colors.transparent,
],
)
),
),
Visibility(
visible: visCheckBoxes,
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(top:8.0),
child: const Text("Selecione os ficheiros para transferência", style: TextStyle(fontSize: 18)),
),
ColoredBox(color: Colors.black, child: Container(height: 1)),
],
),
),
Expanded(
child: FutureBuilder<ListResult>(
future: _storage.getAllUserFiles(user.uid),
builder: (BuildContext context,AsyncSnapshot<ListResult> snapshot) {
if (snapshot.hasData ) {
//List<Reference> files = List<Reference>.empty(growable: true);
if(snapshot.data!=null){
if(loading){
setState(() {
loading = false;
statusMessage = "";
});
}
allUserFiles = snapshot.data!.items;
if(allUserFiles.isEmpty){
return const Center(
child: Text( "Não existem ficheiros na sua área" )
);
}
return FutureBuilder<List<Month>>(
future: SeparateLists(allUserFiles),
builder: (BuildContext context,AsyncSnapshot<List<Month>> snapshot) {
if(snapshot.hasData){
if(loading){
setState(() {
loading = false;
statusMessage = "";
});
}
listsByMonth = snapshot.data!;
//List of months
return ListView.builder(
itemCount: listsByMonth.length,
itemBuilder: (context, indexMonth) {
//TODO Fix check boxes
Month month = listsByMonth[indexMonth];
return ExpansionTile(
title: visCheckBoxes? Row(
children: [
Text(month.name),
Checkbox(
value: listsByMonth[indexMonth].checked,
onChanged: (value){
if(value!=null) {
setState(() => listsByMonth[indexMonth].checked = value);
}
}),
],
) : Text(month.name),
initiallyExpanded: true,
children: [
//List of the files uploaded in that month
Padding(
padding: const EdgeInsets.only(left:10.0),
child: ListView.builder(
key: new Key("list_${month.name}"),
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: month.files.length,
itemBuilder: (context, indexFile) {
FileCell fileCell = month.files[indexFile];
return ListTile(
title: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("${fileCell.name}"),
Padding(
padding: const EdgeInsets.only(left:8.0),
child: Text(DateFormat('dd-MM-yyyy – kk:mm').format(fileCell.timeCreated!)),
),
],
),
trailing: visCheckBoxes? Checkbox(
value: month.files[indexFile].toTransfer,
onChanged: (value)=>{
if(value!=null)
setState(()=>listsByMonth[indexMonth].files[indexFile].toTransfer = value),
},
) : null,
subtitle: fileCell.downloadProgress != null
? LinearProgressIndicator(
valueColor: const AlwaysStoppedAnimation<Color>(Colors.greenAccent),
value: fileCell.downloadProgress,
): null,
);
},
),
)
],
);
},
);
}
return Loading(status: "A carregar os ficheiros");
},
);
}
在每個快照上, listsByMonth = snapshot.data!;
行將保留列表並取消選中該復選框。
您可以在全局范圍內定義listsByMonth
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.