简体   繁体   English

如何为 ListView 生成器子 flutter 设置动画?

[英]how to animate ListView builder child flutter?

Hi i am new to Flutter development and Basically i am working on quiz type demo.嗨,我是 Flutter 开发的新手,基本上我正在做测验类型的演示。 when there will be 1 question and dynamic answers in ListView and after selecting answer and pressing next button another question will be there with dynamic answers.当 ListView 中将有 1 个问题和动态答案时,在选择答案并按下一个按钮后,另一个问题将出现在动态答案中。

above demo is working fine but i wan to animate ListView dynamic child from right to left.上面的演示工作正常,但我想从右到左为 ListView 动态子项设置动画。

问答题

To animate listview items i have tried https://pub.dev/packages/flutter_staggered_animations but it applies to entire ListView only for the 1st time and not applied for the rest of question answers.为了使列表视图项目动画化,我尝试了 https://pub.dev/packages/flutter_staggered_animations但它仅适用于整个 ListView 第一次,而不适用于 rest 的问题答案。

Can anyone please let me know how can i do this or give me any reference link.任何人都可以让我知道我该怎么做或给我任何参考链接。

you can try this package This package will help you to animate your list item.您可以试试这个 package 这个 package 将帮助您为您的列表项设置动画。

https://pub.dev/packages/auto_animated https://pub.dev/packages/auto_animated

List usage example列表使用示例

// With predefined options
LiveList.options(
  options: options,

  // Like ListView.builder, but also includes animation property
  itemBuilder: buildAnimatedItem,

  // Other properties correspond to the 
  // `ListView.builder` / `ListView.separated` widget
  scrollDirection: Axis.horizontal,
  itemCount: 10,
);

// Or raw
LiveList(
  delay: /*...*/,
  showItemInterval: /*...*/,
  // ... and all other arguments from `LiveOptions` (see above)
);

I highly recommend you to use PageView in this case, Just create new file and paste my code to see how it's work.我强烈建议您在这种情况下使用PageView ,只需创建新文件并粘贴我的代码即可查看其工作原理。

The code do what you want exactly, but off course doesn't implements any styling or your app mechanism.该代码完全按照您的要求执行,但当然不会实现任何样式或您的应用程序机制。 I just used buttons for simplicity.为了简单起见,我只是使用了按钮。 Implement it with your desired way:用你想要的方式实现它:

import 'package:flutter/material.dart';

class TestPage extends StatelessWidget {
  TestPage({Key? key}) : super(key: key);
  final PageController controller = PageController();
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Expanded(
          child: PageView(
            controller: controller,
            children: List.generate(
                6, (index) => QuestionPage(questionNumber: index)),
          ),
        ),
        Row(
          children: [
            Expanded(
              child: ElevatedButton(
                onPressed: () {
                  controller.previousPage(
                      duration: const Duration(milliseconds: 500),
                      curve: Curves.easeInOut);
                },
                child: const Text('Back'),
              ),
            ),
            const SizedBox(width: 20),
            Expanded(
              child: ElevatedButton(
                onPressed: () {
                  controller.nextPage(
                      duration: const Duration(milliseconds: 500),
                      curve: Curves.easeInOut);
                },
                child: const Text('Next'),
              ),
            ),
          ],
        )
      ],
    );
  }
}

class QuestionPage extends StatelessWidget {
  const QuestionPage({Key? key, required this.questionNumber})
      : super(key: key);

  final int questionNumber;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Text(
            'Question $questionNumber',
            textAlign: TextAlign.center,
          ),
          const SizedBox(height: 8),
          ElevatedButton(
            onPressed: () {},
            child: const Text('Answer 1'),
          ),
          const SizedBox(height: 8),
          ElevatedButton(
            onPressed: () {},
            child: const Text('Answer 2'),
          ),
          const SizedBox(height: 8),
          ElevatedButton(
            onPressed: () {},
            child: const Text('Answer 3'),
          ),
          const SizedBox(height: 8),
          ElevatedButton(
            onPressed: () {},
            child: const Text('Answer 4'),
          ),
        ],
      ),
    );
  }
}

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM