【问题标题】:Flutter : Change size of stack when children overflowFlutter:当孩子溢出时改变堆栈的大小
【发布时间】:2020-03-03 06:44:00
【问题描述】:

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

我想添加一个手势转换作为 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)当我将项目移出屏幕时,gestureDetection 停止,项目不再可移动。

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

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


  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 面向对象的有状态小部件操作中。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您可以使用 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 对象让你知道增量

    指针在事件坐标空间中的移动量 自上次更新以来的接收者

    在 x 和 y 轴上。

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

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

    【讨论】:

      猜你喜欢
      • 2021-04-07
      • 2013-02-07
      • 2012-01-12
      • 2013-07-11
      • 2010-12-04
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      • 2018-09-10
      相关资源
      最近更新 更多