使用导航器重新加载布尔



当我通过Navigator()返回主屏幕时,我想重新加载我的bool。bool表示项目是否在收藏夹中。事实上,当我点击收藏夹按钮,然后进入收藏夹页面,将此项目删除到该列表中,然后返回主页时,更改是不可见的。如何通过导航器重新加载?这是我的代码:

home.dart

import 'package:flutter/material.dart';
import 'package:my_app_from_scratch/data.dart';
import 'package:my_app_from_scratch/favorite.dart';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class Item {
final String title;
final String description;
final String imageURL;
final int countdown;
final int id;
Item({this.title, this.description, this.imageURL, this.countdown, this.id});
}
class _HomeState extends State<Home> {
List<Item> savedItems = new List<Item>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
actions: [
IconButton(
icon: Icon(Icons.favorite_border),
onPressed: () => pushToFavorite(context))
],
),
body: ListView.builder(
itemCount: itemData.length,
itemBuilder: (context, index) {
bool isSaved = savedItems.contains(itemData[index]);
return Row(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
itemData[index].title,
style: TextStyle(
fontSize: 22, fontWeight: FontWeight.bold),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(itemData[index].description),
),
],
),
Spacer(),
Padding(
padding: const EdgeInsets.all(12.0),
child: GestureDetector(
child: Icon(
isSaved ? Icons.favorite : Icons.favorite_border,
color: isSaved ? Colors.red : null,
),
onTap: () {
setState(() {
if (isSaved) {
savedItems.remove(itemData[index]);
} else {
savedItems.add(itemData[index]);
}
});
},
),
),
],
);
}));
}
Future pushToFavorite(BuildContext context) {
return Navigator.of(context).push(MaterialPageRoute(
builder: (context) => Favorite(favoriteItem: savedItems)));
}
}

最爱。dart

import 'package:flutter/material.dart';
import 'package:my_app_from_scratch/home.dart';
class Favorite extends StatefulWidget {
final List<Item> favoriteItem;
const Favorite({Key key, @required this.favoriteItem}) : super(key: key);
@override
_FavoriteState createState() => _FavoriteState();
}
class _FavoriteState extends State<Favorite> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Favourite'),
leading: IconButton(
onPressed: Back,
icon: Icon(Icons.arrow_back),
),
),
body: widget.favoriteItem.length > 0
? ListView.builder(
itemCount: widget.favoriteItem.length,
itemBuilder: (context, index) {
bool isSaved =
widget.favoriteItem.contains(widget.favoriteItem[index]);
return Row(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
widget.favoriteItem[index].title,
style: TextStyle(
fontSize: 22, fontWeight: FontWeight.bold),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(widget.favoriteItem[index].description),
),
],
),
Spacer(),
Padding(
padding: const EdgeInsets.all(12.0),
child: GestureDetector(
child: Icon(
isSaved ? Icons.favorite : Icons.favorite_border,
color: isSaved ? Colors.red : null,
),
onTap: () {
setState(() {
if (isSaved) {
widget.favoriteItem
.remove(widget.favoriteItem[index]);
} else {
widget.favoriteItem
.add(widget.favoriteItem[index]);
}
});
},
),
),
],
);
})
: Center(
child: Text('Il n'y a rien'),
),
);
}
void Back() {
Navigator.pop(
context,
setState(() {})
);
}
}

我是初学者,请帮帮我,谢谢!

class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
final bool isFavorite;
Home({isFavorite = false});
}...
class _HomeState extends State<Home> {
List<Item> savedItems = new List<Item>();
final bool isFavorite;
HomeState({isFavorite});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(...

当您导航到主屏幕时,如果其收藏夹为true,则将isFavorite作为true传递。

相关内容

  • 没有找到相关文章

最新更新