gpt4 book ai didi

flutter : Change size of stack when children overflow

转载 作者:行者123 更新时间:2023-12-02 21:14:56 27 4
gpt4 key购买 nike

Stac Simulation

我有一个可拖动的小部件(来自 https://medium.com/flutter-community/create-a-draggable-widget-in-flutter-50b61f12635d ),容器中的堆栈(红色)由可移动的子项组成。这是小部件树: widget tree

我想添加一个手势转换作为 FormBuilder ( https://github.com/flutter/flutter/blob/master/examples/flutter_gallery/lib/demo/transformations/transformations_demo.dart ) 来转换矩阵,正如你在 GIF 中看到的那样,主要是放大/缩小和转换 x/y。

class _HomeViewState extends State<HomeView> {
final _stackKey;
_HomeViewState(this._stackKey);
List<Widget> movableItems = [];

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: Text('SynApp'),
actions: <Widget>[
IconButton(
onPressed: () {
x = 200.0;
y = 200.0;
setState(() {
movableItems.add(
MoveableStackItem(_stackKey),
);
});
},
icon: Icon(Icons.add),
),
],
),
body: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
// Draw the scene as big as is available, but allow the user to
// translate beyond that to a visibleSize that's a bit bigger.
final Size size = Size(constraints.maxWidth, constraints.maxHeight);
final Size visibleSize = Size(size.width * 3, size.height * 2);
return GestureTransformable(
reset: _reset,

onResetEnd: () {
setState(() {
_reset = false;
});
},
child: new Container(
color: Colors.red,
child: Stack(
key: _stackKey,
overflow: Overflow.visible,
fit: StackFit.expand,
children: movableItems),
),


boundaryRect: Rect.fromLTWH(
-visibleSize.width / 2,
-visibleSize.height / 2,
visibleSize.width,
visibleSize.height,
),

initialTranslation: Offset(size.width, size.height),
size: size,
);
}),
);
}
}

问题是:

a) 堆栈的大小等于初始屏幕。

b)当我将项目移出屏幕时,手势检测停止,项目不再可移动。

我想要什么:我想根据我移动项目的位置动态调整堆栈(红色框)的大小。

我能够找到堆栈小部件的位置和大小。


Size stackSize;
Offset stackPosition;
_MoveableStackItemState(this._stackKey);


getSizeAndPosition() {
RenderStack _stackStack = _stackKey.currentContext.findRenderObject();
stackSize = _stackStack.size;
stackPosition = _stackStack.localToGlobal(Offset.zero);
print('stackSize $stackSize');
print('stackPosition $stackPosition');
}

但是我开始迷失在面向高级 UI 对象的有状态小部件操作中。

最佳答案

您可以使用 AnimatedContainer 包裹红色堆栈。

LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
// Draw the scene as big as is available, but allow the user to
// translate beyond that to a visibleSize that's a bit bigger.
final Size size = Size(constraints.maxWidth, constraints.maxHeight);
final Size visibleSize = Size(size.width * 3, size.height * 2);
return GestureTransformable(
reset: _reset,

onResetEnd: () {
setState(() {
_reset = false;
});
},
child: new AnimatedContainer(
color: Colors.red,
duration: Duration(milliseconds: 200),
width: _stackWidth,
height: _stackHeight,
child: Stack(
key: _stackKey,
overflow: Overflow.visible,
fit: StackFit.expand,
children: movableItems),
),


boundaryRect: Rect.fromLTWH(
-visibleSize.width / 2,
-visibleSize.height / 2,
visibleSize.width,
visibleSize.height,
),

initialTranslation: Offset(size.width, size.height),
size: size,
);
}),

尝试监听GestureTransformable的以下事件

onPanUpdate: (DragUpdateDetails details){
var deltaX = details.delta.dx;
var deltaY = details.delta.dy;
}

DragUpdateDetails 对象让您了解增量

the amount the pointer has moved in the coordinate space of the event receiver since the previous update

在 x 和 y 轴上。

在“onPanUpdate”中,您可以更新与手势增量相关的动画容器的宽度和高度。

setState((){
_stackHeight = /* ... */
_stackWidth = /* ... */
});

关于 flutter : Change size of stack when children overflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58735967/

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