首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在向动画列表中添加元素时停止最后一个元素跳转

如何在向动画列表中添加元素时停止最后一个元素跳转
EN

Stack Overflow用户
提问于 2021-02-14 00:01:10
回答 1查看 152关注 0票数 1

我正在尝试重新创建RobinHood幻灯片编号动画,我正在将每个数字视为n个动画列表中的一个项目,当我将一个数字添加到列表的开头时,列表中的最后一个项目会发生跳转,但我不知道如何修复它。

这是每个数字的代码

代码语言:javascript
运行
复制
class NumberColView extends StatefulWidget {
  final int animateTo;

  final bool comma;
  final TextStyle textStyle;
  final Duration duration;
  final Curve curve;

  NumberColView(
      {@required this.animateTo,
      @required this.textStyle,
      @required this.duration,
      this.comma = false,
      @required this.curve})
      : assert(animateTo != null && animateTo >= 0 && animateTo < 10);

  @override
  _NumberColState createState() => _NumberColState();
}

class _NumberColState extends State<NumberColView>
    with SingleTickerProviderStateMixin {
  ScrollController _scrollController;

  double _elementSize = 0.0;

  @override
  void initState() {
    super.initState();
    print(_elementSize);
    _scrollController = new ScrollController();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _elementSize = _scrollController.position.maxScrollExtent / 10;
      setState(() {});

    });
  }

  @override
  void didUpdateWidget(NumberColView oldWidget) {
    if (oldWidget.animateTo != widget.animateTo) {
      _scrollController.animateTo(_elementSize * widget.animateTo,
          duration: widget.duration, curve: widget.curve);
    }

    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    // print(widget.animateTo);
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        IgnorePointer(
          child: ConstrainedBox(
            constraints: BoxConstraints(maxHeight: _elementSize),
            child: SingleChildScrollView(
              controller: _scrollController,
              child: Column(
                children: List.generate(10, (position) {
                  return Text(position.toString(), style: widget.textStyle);
                }),
              ),
            ),
          ),
        ),
        widget.comma
            ? Container(
                child: Text(', ',
                    style:
                        TextStyle(fontSize: 16, fontWeight: FontWeight.bold)))
            : Container(),
      ],
    );
  }
}

这是构建整数的代码

代码语言:javascript
运行
复制
class _NumberSlideAnimationState extends State<NumberSlideAnimation> {
  @override
  void didUpdateWidget(oldWidget) {
    if (oldWidget.number.length != widget.number.length) {
      int newLen = widget.number.length - oldWidget.number.length;
      if(newLen > 0) {
        widget.listKey.currentState.insertItem(0,
                            duration: const Duration(milliseconds: 200));
        
      }
      // setState(() {
      //   animateTo = widget.animateTo;
      // });
    }

    super.didUpdateWidget(oldWidget);
  }

  Widget digitSlide(BuildContext context, int position, animation) {
    int item = int.parse(widget.number[position]);
    return SlideTransition(
      position: Tween<Offset>(
        begin: const Offset(0, 1),
        end: Offset(0, 0),
      ).animate(animation),
      child:
          // TestCol(animateTo: item, style: widget.textStyle)

          NumberColView(
        textStyle: widget.textStyle,
        duration: widget.duration,
        curve: widget.curve,
        comma: (widget.number.length - 1) != position &&
                (widget.number.length - position) % 3 == 1 ??
            true,
        animateTo: item,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 40,
      child: AnimatedList(
        shrinkWrap: true,
        scrollDirection: Axis.horizontal,
        key: widget.listKey,
        initialItemCount: widget.number.length,
        itemBuilder: (context, position, animation) {
          return digitSlide(context, position, animation);
        },
      ),
    );
  }
}
EN

回答 1

Stack Overflow用户

发布于 2021-02-18 07:00:42

我不知道你的问题的正确解决方案,但我知道另一种方法,我只是分享它。

如果你想使用这个方法,下面是完整的代码:

更新:现在0将像它应该的那样从底部出现,未带电的值将在宫殿中。

代码语言:javascript
运行
复制
class Count extends StatefulWidget {
  const Count({Key key}) : super(key: key);

  @override
  _CountState createState() => _CountState();
}

class _CountState extends State<Count> {
  int count = 10;
  int count2 = 10;
  int count3 = 10;
  Alignment alignment = Alignment.topCenter;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ClipRRect(
            child: Align(
              alignment: Alignment.center,
              heightFactor: 0.2,
              child: SizedBox(
                height: 100,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    ...List.generate(count.toString().length, (index) {
                      final num2IndexInRange =
                          count2.toString().length - 1 >= index;
                      final num3IndexInRange =
                          count3.toString().length - 1 >= index;

                      final num1 = int.parse(count.toString()[index]),
                          num2 = num2IndexInRange
                              ? int.parse(count2.toString()[index])
                              : 9,
                          num3 = num3IndexInRange
                              ? int.parse(count3.toString()[index])
                              : 1;

                      return AnimatedAlign(
                        key: Key("${num2} ${index}"),
                        duration: Duration(milliseconds: 500),
                        alignment: (num1 != num2 || num1 != num3)
                            ? alignment
                            : Alignment.center,
                        child: Text(
                          num3.toString(),
                          style: TextStyle(
                            fontSize: 20,
                          ),
                        ),
                      );
                    }),
                  ],
                ),
              ),
            ),
          ),
          SizedBox(height: 200),
          RaisedButton(
            onPressed: () async {
              setState(() {
                alignment = Alignment.topCenter;
                count += 10;
              });
              await Future.delayed(Duration(milliseconds: 250));
              setState(() {
                alignment = Alignment.bottomCenter;
                count2 = count;
              });
              await Future.delayed(Duration(milliseconds: 250));
              setState(() {
                count3 = count;
              });
            },
          ),
        ],
      ),
    );
  }
}

如果您不喜欢数字之间的间距变化,您可以使用一个大小不同的框来扭曲文本小部件,并给它一个固定的宽度。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66187084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档