我试图学习在Flutter中使用具有Stack和PositionedTransitions的动画,并为此制作了一个基于精灵的简单游戏。目前,我的精灵显示在动画的结束位置,而不是开始处,然后滑动到结束位置。
我的代码如下所示:
if (lamb != null) {
beginTop = ((lamb.lastY - 1) * roomLength) + lamb.lastY;
beginLeft = ((lamb.lastX - 1) * roomLength) + lamb.lastX;
endTop = ((lamb.y - 1) * roomLength) + lamb.y;
endLeft = ((lamb.x - 1) * roomLength) + lamb.x;
layerAnimation = RelativeRectTween(
begin: RelativeRect.fromLTRB(beginLeft, beginTop, 0.0, 0.0),
end: RelativeRect.fromLTRB(endLeft, endTop, 0.0, 0.0),
).animate(_controller.view);
return PositionedTransition(
rect: layerAnimation,
child: Text(
lamb.emoji,
style: TextStyle(color: Colors.black, fontSize: roomLength - 12),
),
);
}
Run Code Online (Sandbox Code Playgroud)
我应该在_controller.forward()某个地方打个电话吗?何时何地?屏幕上最多有10个动画小部件同时使用同一_controller,所有小部件必须同时滑动。
谢谢
PS:以下代码,而不是PositionedTransition内容,似乎朝着正确的方向发展:
return …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用图像和其中的文本来实现 gridview。我想要黑色背景图像底部的文本。这是我的代码ListItem
class ListItem extends StatelessWidget {
String url;
String name;
ListItem(this.url, this.name);
@override
Widget build(BuildContext context) {
return new Container(
child: new Column(
children: <Widget>[
new Image.network('${url}', fit: BoxFit.cover),
new Positioned(
child: new Container(
child: new Text('${name}',
style: new TextStyle(fontSize: 20.0, color: Colors.white)),
decoration: new BoxDecoration(color: Colors.black),
padding: new EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 16.0)),
left: 0.0,
bottom: 108.0,
)
],
));
}
}
Run Code Online (Sandbox Code Playgroud)
使用此代码,它显示错误
Positioned widgets must be placed directly inside Stack widgets.
Positioned(no depth, dirty) has a Stack …Run Code Online (Sandbox Code Playgroud) 我只想在屏幕上拖动小部件,而不是拖放。
但是,每当我离开它并再次开始拖动时,它就会开始从开始的位置开始拖动。就好像它是一样reset。
我的猜测是 build 被一次又一次地调用,所以它会自动使用original position values. 我该如何解决?代码应该是什么?
换句话说,position就是设置为结束(100, 100)之后DragEvent。然后下一个拖动开始位置,(100, 100)而不是小部件被放置到的最后位置。
我也尝试过这个GlobalPosition,但得到了相同的结果。这是我的代码:
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
double x1 = 100.0;
double x2 = 200.0;
double y1 = 100.0;
double y2 = 200.0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('Drag widget on screen'),
),
body: Stack(
children: [
Positioned( …Run Code Online (Sandbox Code Playgroud) dart flutter flutter-positioned flutter-state flutter-android
当我尝试使用定位小部件封装PandemicCard时,出现以下异常。如果我将卡设置为“单独放置/不放置”窗口小部件,则可以正常工作。
I/flutter ( 7331): ??? EXCEPTION CAUGHT BY RENDERING LIBRARY ??????????????????????????????????????????????????????????
I/flutter ( 7331): The following assertion was thrown during performLayout():
I/flutter ( 7331): RenderStack object was given an infinite size during layout.
I/flutter ( 7331): This probably means that it is a render object that tries to be as big as possible, but it was put
I/flutter ( 7331): inside another render object that allows its children to pick their own size.
I/flutter ( 7331): The nearest ancestor providing …Run Code Online (Sandbox Code Playgroud)