flutter - 无法在 ListView 颤动中显示保存的项目
问题描述
我无法在我的应用程序的页面上的 ListView 中显示我的_saved
列表中的项目(在页面中)(单击时触发,在这里我们检查要保存的项目)。我希望我的应用程序仅在 ListView 中显示选中的项目,当我单击应用程序的本地后退按钮时(这是我的应用程序中的第二个选项卡)。FavoritesList
FavoritesList
FAB
SecondPage
我的SecondPage
标签代码:
import 'package:flutter/material.dart';
import 'favoritelist.dart';
class SecondPage extends StatefulWidget {
@override
_SecondPageState createState() => _SecondPageState();
}
class _SecondPageState extends State<SecondPage> {
@override
Widget build(BuildContext context) {
return
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Add Your Favorite Sites Here!❤',
style: TextStyle(color: Colors.white),
),
Container(
child: Icon(Icons.favorite, size: 150, color: Colors.blue[100]),
),
SizedBox(height: 250),
FloatingActionButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => FavoriteList(),
),
);
},
child: Icon(Icons.add),
foregroundColor: Colors.blue,
),
],
);
}
}
我的FavoritesList
页面代码:
class FavoriteList extends StatefulWidget {
@override
_FavoriteListState createState() => _FavoriteListState();
}
class _FavoriteListState extends State<FavoriteList> {
// bool isChecked = false;
final Set _saved = Set();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Add to Favorites!'), centerTitle: true, backgroundColor:
Colors.red),
// backgroundColor: Colors.indigo,
body: SafeArea(
child: ListView.builder(
itemCount: 53,
itemBuilder: (context, index) {
return CheckboxListTile(
activeColor: Colors.red,
checkColor: Colors.white,
// value: _saved.contains(context), // changed
value: _saved.contains(index),
onChanged: (val) {
setState(() {
// isChecked = val; // changed
// if(val == true){ // changed
// _saved.add(context); // changed
// } else{ // changed
// _saved.remove(context); // changed
// } // changed
if(val == true){
_saved.add(index);
} else{
_saved.remove(index);
}
});
},
title: Row(
children: <Widget>[
Image.asset('lib/images/${images[index]}'),
SizedBox(width: 10,),
Text(nameOfSite[index]),
],
),
);
},
),
),
);
}
}
解决方案
推荐阅读
- c# - 在两个视图上显示相同的弹出窗口
- asp.net-core - 如何按照存储库模式在 Asp.Net Core 5.0 项目上实现 .Net Core Identity?
- python - 在 matplotlib 中设置 Y 轴的字符串值
- c++ - 如何在cin之后新建一个动态数组
- r - 润滑到本月的最后一天
- python - Python Decimal 模块未按预期四舍五入
- spring-boot - 通过 Spring Security 验证从 WSO2 IS 生成的 JWT
- django - 使用 apscheduler 的 Django 后台任务计划?
- javascript - 为什么 boostrap vue 烤面包机会立即消失?
- laravel - 显示使用 laravel 8 从数据库中获取的下拉列表中的数据