【发布时间】:2021-04-08 15:18:54
【问题描述】:
我试图保存用户评分以在用户返回页面时显示它。但我有点挣扎,无法弄清楚如何做到这一点。评级有效,但正如我所说的储蓄无效。 所以发生的事情是它总是空的。我真正想要的是,如果用户回到页面,他会看到他的评分,如果他再次评分并且评分不同,我让他评分的最后一个评分,如果不是,那么如果他按下清除,评分将删除什么也可以正常工作。
也许任何人都可以提供帮助。
lass Ratingpage extends StatefulWidget {
final int maximumRating;
final Function(int) onRatingSelected;
Ratingpage(this.onRatingSelected, [this.maximumRating = 5]);
@override
_RatingpageState createState() => _RatingpageState();
}
class _RatingpageState extends State<Ratingpage> {
int haveusercurrentchoice;
int _currentRating = 0;
Widget _buildRatingStar(int index) {
if (index < _currentRating) {
return Icon(
Icons.star,
color: Colors.yellow,
);
} else {
return Icon(
Icons.star,
color: Colors.white,
);
}
}
Widget _buildBody() {
final stars = List<Widget>.generate(this.widget.maximumRating, (index) {
return Expanded(
child: GestureDetector(
child: _buildRatingStar(index),
onTap: () {
setState(() {
_currentRating = index;
});
this.widget.onRatingSelected(_currentRating);
},
),
);
});
return Row(
children: [
Expanded(
child: Row(
children: stars,
),
),
Expanded(
child: TextButton(
onPressed: () {
setState(() {
_currentRating = 0;
});
this.widget.onRatingSelected(_currentRating);
},
child: Text(
"Clear",
style: TextStyle(color: Colors.white),
),
),
),
],
);
}
@override
Widget build(BuildContext context) {
return _buildBody();
}
如果您需要更多信息,请发表评论。
这就是我调用页面的方式
Container(
width: 210,
height: 94,
//color: Colors.blue.withOpacity(0.5),
child: Column(
children: [
InkWell(
onTap: () {
setState(() {
israting = true;
});
// if( _rating !=null && _rating >0){
// likevideo(videos.data()['id']);}
},
child: israting
? Container(
height: 50,
margin: EdgeInsets.fromLTRB(
0, 0, 5, 0),
child: Column(
children: [
Ratingpage((rating) {
setState(() {
_rating = rating;
});
if (_rating != null &&
_rating > 0) {
likevideo(
videos.data()['id'],
_rating);
print(delteuserchoicing);
} else if (_rating ==
null ||
_rating == 0) {
dislike(
videos.data()['id'],
_rating);
}
}),
],
),
)
: Icon(
Icons.star,
size: 37,
color: videos
.data()['likes']
.contains(uid)
? Colors.yellow
: Colors.white,
),
),
它实际上在一个列内
【问题讨论】:
-
Hello @dfgdfv 通常状态不是保存在 GUI 中而是保存在外部类中;为简单起见,您可以创建一个充当全局变量的静态类;我不知道您是如何返回该页面的,但是如果再次创建该页面,如果您不将其保存在某处,则会丢失该值。
-
您是否尝试将评级 (int) 存储在
sharedpreferences中?并在用户导航到页面时检索它?shared_preferencespub.dev/packages/shared_preferences -
我更新了我的代码,这样你就可以看到我是如何调用页面的。我没有尝试共享偏好,但我实际上是新来的,并且真的不明白如何做到这一点?对于 Camille777 所以你的想法是保存价值并喜欢在 firebase 中然后喜欢打印?如果我理解正确的话,我不会真的很痛苦。
标签: flutter material-design ratingbar