gpt4 book ai didi

flutter - 使用for循环在Flutter中创建嵌套的小部件

转载 作者:行者123 更新时间:2023-12-03 04:13:42 27 4
gpt4 key购买 nike

嗨,我是Flutter的新手,在创建旋转动画时遇到Flutter的多嵌套代码的问题。由于它是非常重复的,因此我尝试使用for循环使其更短,但目前还没有运气。也尝试使用nested library但不起作用。有人知道吗?先感谢您。

Widget _rotateAnimationWidget(BuildContext context, Widget child) {
return SizedBox(
height: 200,
width: 200,
child: Transform.rotate(
angle: 10 * math.pi / 180,
child: Transform.rotate(
angle: 20 * math.pi / 180,
child: Transform.rotate(
angle: 10 * math.pi / 180,
child: Transform.rotate(
angle: -30 * math.pi / 180,
child: Transform.rotate(
angle: 0 * math.pi / 180,
child: Transform.rotate(
angle: 20 * math.pi / 180,
child: Transform.rotate(
angle: -30 * math.pi / 180,
child: Transform.rotate(
angle: 40 * math.pi / 180,
child: Transform.rotate(
angle: 10 * math.pi / 180,
child: Transform.rotate(
angle: 10 * math.pi / 180,
child: Transform.rotate(
angle: 30 * math.pi / 180,
child: child),
),
),
),
),
),
),
),
),
),
),
);
}

最佳答案

好吧,我不知道您要实现什么,但是您可以尝试以下代码。如果您在Flutter Inspector中 checkin ,您将看到嵌套的Transform。但是对我来说,模拟器中什么也没显示。
更新:抱歉,显示的是我在 child 旋转时传递的容器。我忘了SizedBox。但是没有动画。

Widget _rotateAnimationWidget(BuildContext context, Widget child) {
List<int> angleList = [10, 20, 10, -30, 0, 20, -30, 40, 10, 10, 30];
Widget transform;
int i = 0;

do {
transform = Transform.rotate(
angle: angleList[i] * math.pi / 180, child: transform);
i++;
} while (i < angleList.length - 1);

transform =
Transform.rotate(angle: angleList.last * math.pi / 180, child: child);

return SizedBox(height: 200, width: 200, child: transform);
}

关于flutter - 使用for循环在Flutter中创建嵌套的小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62604912/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com