在 Flutter 中单击时强制动画停止(并到达最后位置)

发布于 2025-01-12 00:14:43 字数 2608 浏览 0 评论 0原文

我想像社交媒体平台一样实现 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:-

enter image description here

This is what I have:-

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掀纱窥君容 2025-01-19 00:14:43

添加一个新条件,将所有先前的项目设置为 1.0(如果它们已被“看到”)

percent: seen ? 1.0 : shouldAnimate ? 1.0 : 0.0

这里的问题是,当跳过一个故事时,您显式禁用动画

animation: seen == false ? true : false

但这将停止并保持进度指示器的状态(如果是 0.2,它将保持这种状态),上述解决方案是将所有被视为“已看到”的项目设置为值 1.0

Add a new condition to set all previous items to a value of 1.0 if they have been "seen"

percent: seen ? 1.0 : shouldAnimate ? 1.0 : 0.0

The issue here is that you are disabling the animation explicitly when a a story is skipped

animation: seen == false ? true : false

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文