Flutter 无法在ListView中显示保存的项目
我无法在我的应用程序的Flutter 无法在ListView中显示保存的项目,flutter,dart,Flutter,Dart,我无法在我的应用程序的收藏夹列表页面的列表视图中显示我的\u保存的列表(在收藏夹列表页面中)中的项目(当单击FAB时会触发该页面,这里我们检查要保存的项目)。我希望我的应用程序在应用程序(我的应用程序中的第二个选项卡)的SecondPage上单击本地后退按钮时,仅在列表视图中显示选中的项目 我的第二页选项卡代码: import 'package:flutter/material.dart'; import 'favoritelist.dart'; class SecondPage extend
收藏夹列表
页面的列表视图中显示我的\u保存的列表(在收藏夹列表
页面中)中的项目(当单击FAB
时会触发该页面,这里我们检查要保存的项目)。我希望我的应用程序在应用程序(我的应用程序中的第二个选项卡)的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,
),
],
);
}
}
用户可以使用任何用于此类作业的状态管理技术。或者,您可以将数据从收藏夹页面传递到其pop方法的上一页。如果我理解您的问题,您希望在应用程序的第二页中选择项目列表,然后在第一页中仅显示所选项目,对吗?在这种情况下,您必须将所选项目的列表存储在两个页面之外,即在PageView
小部件的父小部件中,并使用该小部件更新/显示所选项目。@检查项目,然后单击“上一步”按钮时,不会将列表返回到SecondPage
。现在怎么办?@HarshvardhanJoshi是的,但是在检查和点击这些项目时,它不会返回任何东西,那么它将如何工作呢
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]),
],
),
);
},
),
),
);
}
}