在 Flutter 中单击时强制动画停止(并到达最后位置)
我想像社交媒体平台一样实现 Stories
功能。我遇到的问题很简单,但我不太了解 Flutter 来解决它。
这是我想要的输入:-
这就是我所拥有的:-
如您所见,单击时,加载栏不会出现充实而变化。它只是保持其状态。我不想要这个。我想要它就像上面的 gif
这是我的代码:-
//.... code about changing the index on tap and other things is right. so no need to put them here.
Positioned(child: Row(children: buildStoryBars(context, widget.options, index),
))
这是完成工作的函数:-
List<StoryBar> buildStoryBars(
BuildContext context, StoryOptions options, int index) {
final screenWidth = MediaQuery.of(context).size.width;
List<StoryBar> data = [];
double width;
width = screenWidth / options.stories.length;
for (var i = 0; i < options.stories.length; i++) {
data.add(
StoryBar(
storyType: StoryMediaType.image,
duration: options.duration,
seen: i < index ? true : false,
shouldAnimate: i <= index ? true : false,
width: width),
);
}
return data;
}
这是我用于显示动画条的小部件:-
import 'package:flutter/material.dart';
import 'package:percent_indicator/linear_percent_indicator.dart';
import '../utils/enums.dart';
class StoryBar extends StatelessWidget {
final StoryMediaType storyType;
final Duration duration;
final bool seen;
final bool shouldAnimate;
final double width;
const StoryBar(
{Key? key,
required this.storyType,
required this.duration,
required this.seen,
required this.width,
required this.shouldAnimate})
: super(key: key);
@override
Widget build(BuildContext context) {
return LinearPercentIndicator(
width: width,
lineHeight: 10.0,
percent: shouldAnimate ? 1.0 : 0.0,
linearStrokeCap: LinearStrokeCap.roundAll,
backgroundColor: Colors.grey,
progressColor: Colors.white,
isRTL: true,
animation: seen == false ? true : false,
animationDuration: duration.inMilliseconds,
addAutomaticKeepAlive: false,
padding: const EdgeInsets.symmetric(horizontal: 5.0),
);
}
}
那么有没有一种方法可以实现上面所需的输出,就像Facebook? 我已经使用了我所知道的关于 Flutter 和编程的一切:( 谢谢。
I want to implement Stories
functionality just like social media platforms. The problem I have is straight forward but I don't know Flutter that much to solve it.
This is the input that I want:-
This is what I have:-
As you can see, on click, the loading bar doesn't get full and change. It just keeps its state. And I don't want this. I want it just Like the above gif
This is my code:-
//.... code about changing the index on tap and other things is right. so no need to put them here.
Positioned(child: Row(children: buildStoryBars(context, widget.options, index),
))
And this is the function that does the work:-
List<StoryBar> buildStoryBars(
BuildContext context, StoryOptions options, int index) {
final screenWidth = MediaQuery.of(context).size.width;
List<StoryBar> data = [];
double width;
width = screenWidth / options.stories.length;
for (var i = 0; i < options.stories.length; i++) {
data.add(
StoryBar(
storyType: StoryMediaType.image,
duration: options.duration,
seen: i < index ? true : false,
shouldAnimate: i <= index ? true : false,
width: width),
);
}
return data;
}
And this is my widget for showing the animated bars:-
import 'package:flutter/material.dart';
import 'package:percent_indicator/linear_percent_indicator.dart';
import '../utils/enums.dart';
class StoryBar extends StatelessWidget {
final StoryMediaType storyType;
final Duration duration;
final bool seen;
final bool shouldAnimate;
final double width;
const StoryBar(
{Key? key,
required this.storyType,
required this.duration,
required this.seen,
required this.width,
required this.shouldAnimate})
: super(key: key);
@override
Widget build(BuildContext context) {
return LinearPercentIndicator(
width: width,
lineHeight: 10.0,
percent: shouldAnimate ? 1.0 : 0.0,
linearStrokeCap: LinearStrokeCap.roundAll,
backgroundColor: Colors.grey,
progressColor: Colors.white,
isRTL: true,
animation: seen == false ? true : false,
animationDuration: duration.inMilliseconds,
addAutomaticKeepAlive: false,
padding: const EdgeInsets.symmetric(horizontal: 5.0),
);
}
}
So is there a way to achieve the desired output above just like Facebook?
I have used everything I know about Flutter and programming :(
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加一个新条件,将所有先前的项目设置为 1.0(如果它们已被“看到”)
这里的问题是,当跳过一个故事时,您显式禁用动画
但这将停止并保持进度指示器的状态(如果是 0.2,它将保持这种状态),上述解决方案是将所有被视为“已看到”的项目设置为值 1.0
Add a new condition to set all previous items to a value of 1.0 if they have been "seen"
The issue here is that you are disabling the animation explicitly when a a story is skipped
But that will stop and keep the status of the progress indicator (if it was 0.2 it will stay that way) and the above solution is to set all items that are considered as "seen" to a value of 1.0