包含最喜欢的项目的 ListView
ListView with favorite items
我有一个 ListView.builder
的项目,我们可以点击一颗心添加到收藏夹。我想将所有添加到收藏夹的项目放在另一个屏幕的另一个 ListView.builder
中。我也想通过点击心脏来抑制收藏夹项目。我该怎么做?这是我的代码:
Home_screen.dart
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import '../recyclerview/data.dart';
import 'package:chrolix/constants.dart';
import 'package:flutter_countdown_timer/flutter_countdown_timer.dart';
int itemCount = item.length;
List<bool> selected = new List<bool>();
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
initState() {
for (var i = 0; i < itemCount; i++) {
selected.add(false);
}
super.initState();
}
Icon notFavorite = Icon(Icons.favorite_border, size: 32,);
Icon inFavorite = Icon(Icons.favorite, size: 32,);
@override
Widget build(BuildContext context) {
return new Scaffold(
body: ListView.builder(
scrollDirection: Axis.vertical,
physics: BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
itemCount: itemCount,
itemBuilder: (BuildContext context, int index) {
return Container(
child: new Row(
children: <Widget>[
//Image
new Container(
margin: new EdgeInsets.only(top: 5.0, left: 0.0),
child: new CachedNetworkImage(
imageUrl: item[index].imageURL,
height: MediaQuery.of(context).size.width / 3,
width: MediaQuery.of(context).size.width / 2,
fit: BoxFit.cover,
),
),
new Container(
height: MediaQuery.of(context).size.width / 3,
width: MediaQuery.of(context).size.width / 2,
child : new Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
//Text
new Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Spacer(),
//Titre
Container(
padding: const EdgeInsets.only(top: 15.0 ),
child: Text(
item[index].title,
style: kItemTitle,
),
),
//Decription
Container(
padding: const EdgeInsets.only(left: 10.0, top: 15.0),
child:Text(
item[index].description,
style: kItemDescription,
),
),
Spacer(),
],
),
Container(
padding: const EdgeInsets.only(top: 10.0),
child: CountdownTimer(
daysSymbol: new Text("j "),
hoursSymbol: new Text(" : "),
minSymbol: new Text(" : "),
secSymbol: new Text(""),
endTime: item[index].countdown,
textStyle: TextStyle(fontSize: 28, fontWeight: FontWeight.bold),
onEnd: () {
print('onEnd');
},
),
),
Container(
padding: const EdgeInsets.only(left: 8.0, top: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
GestureDetector(
child: selected.elementAt(index) ? inFavorite : notFavorite,
onTap: () {
setState(() {
selected[index] = !selected.elementAt(index);
});
},
),
],),
),
],),
),
],
),
);
}
)
);
}
}
Favorite_screen.dart
import 'package:flutter/material.dart';
import 'package:chrolix/constants.dart';
import 'package:chrolix/nav.dart';
import 'package:get/get.dart';
class Favoris extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: //Put here the LisView.builder with the favorite items
)
}
}
我没有看到其他问题可以回答我的问题。谢谢!
更新 : 我已经使用了提供者的包
推荐的方法是使用 provider。这用于将变量共享到树下的多个小部件。
这需要的不仅仅是 Whosebug 的答案,所以我会 link 你阅读 flutter 的相关文章,它基本上解释了你想做的一切:
Here is is.
如果 link 损坏,您可以通过搜索“Simple app state management flutter”找到它
我有一个 ListView.builder
的项目,我们可以点击一颗心添加到收藏夹。我想将所有添加到收藏夹的项目放在另一个屏幕的另一个 ListView.builder
中。我也想通过点击心脏来抑制收藏夹项目。我该怎么做?这是我的代码:
Home_screen.dart
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import '../recyclerview/data.dart';
import 'package:chrolix/constants.dart';
import 'package:flutter_countdown_timer/flutter_countdown_timer.dart';
int itemCount = item.length;
List<bool> selected = new List<bool>();
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
initState() {
for (var i = 0; i < itemCount; i++) {
selected.add(false);
}
super.initState();
}
Icon notFavorite = Icon(Icons.favorite_border, size: 32,);
Icon inFavorite = Icon(Icons.favorite, size: 32,);
@override
Widget build(BuildContext context) {
return new Scaffold(
body: ListView.builder(
scrollDirection: Axis.vertical,
physics: BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
itemCount: itemCount,
itemBuilder: (BuildContext context, int index) {
return Container(
child: new Row(
children: <Widget>[
//Image
new Container(
margin: new EdgeInsets.only(top: 5.0, left: 0.0),
child: new CachedNetworkImage(
imageUrl: item[index].imageURL,
height: MediaQuery.of(context).size.width / 3,
width: MediaQuery.of(context).size.width / 2,
fit: BoxFit.cover,
),
),
new Container(
height: MediaQuery.of(context).size.width / 3,
width: MediaQuery.of(context).size.width / 2,
child : new Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
//Text
new Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Spacer(),
//Titre
Container(
padding: const EdgeInsets.only(top: 15.0 ),
child: Text(
item[index].title,
style: kItemTitle,
),
),
//Decription
Container(
padding: const EdgeInsets.only(left: 10.0, top: 15.0),
child:Text(
item[index].description,
style: kItemDescription,
),
),
Spacer(),
],
),
Container(
padding: const EdgeInsets.only(top: 10.0),
child: CountdownTimer(
daysSymbol: new Text("j "),
hoursSymbol: new Text(" : "),
minSymbol: new Text(" : "),
secSymbol: new Text(""),
endTime: item[index].countdown,
textStyle: TextStyle(fontSize: 28, fontWeight: FontWeight.bold),
onEnd: () {
print('onEnd');
},
),
),
Container(
padding: const EdgeInsets.only(left: 8.0, top: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
GestureDetector(
child: selected.elementAt(index) ? inFavorite : notFavorite,
onTap: () {
setState(() {
selected[index] = !selected.elementAt(index);
});
},
),
],),
),
],),
),
],
),
);
}
)
);
}
}
Favorite_screen.dart
import 'package:flutter/material.dart';
import 'package:chrolix/constants.dart';
import 'package:chrolix/nav.dart';
import 'package:get/get.dart';
class Favoris extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: //Put here the LisView.builder with the favorite items
)
}
}
我没有看到其他问题可以回答我的问题。谢谢!
更新 : 我已经使用了提供者的包
推荐的方法是使用 provider。这用于将变量共享到树下的多个小部件。
这需要的不仅仅是 Whosebug 的答案,所以我会 link 你阅读 flutter 的相关文章,它基本上解释了你想做的一切: Here is is.
如果 link 损坏,您可以通过搜索“Simple app state management flutter”找到它