flutter 如何在控件缩放后获取交互式查看器控件的大小?

hgtggwj0  于 2023-10-22  发布在  Flutter
关注(0)|答案(1)|浏览(155)

如何在控件缩放后获取交互式查看器控件的大小。我使用translate的矩阵4,这是我从我transformationController,去结束点的小部件(右或左)上的按钮触摸,所以想停止在年底,但交互式视图是无限的方式。有什么方法可以得到画布的最后偏移量或大小吗?
没有平移的平底锅和规模是工作罚款与有限的大小。

pvcm50d1

pvcm50d11#

创建一个GlobalKey来访问SizedBox的大小,并传递一个控制器给IneractiveViewer

GlobalKey _key = GlobalKey();
TransformationController _controller = TransformationController();

InteractiveViewer.builderSizedBox Package 较大的小部件。确保将_key传递给SizedBox

  • 注意:不要为SizeBox提供任何有界的高度或宽度 *
InteractiveViewer.builder(
controller:_controller,
      builder: (context, quad) {
    // Replace `yourWidget` with your actual widget
        return SizedBox(key: _key, child: yourWidget); 
    
      },);

小部件构建完成后,您可以使用_keyRenderBox访问SizedBox的大小。您可以在小部件的生命周期中执行此操作,例如在initState中或在小部件布局后的callback中。

double? widgetWidth = 1;
double? widgetHeight = 1;
_controller.addListener(() {
  double currentScale = _controller.value.getMaxScaleOnAxis();
  WidgetsBinding.instance.addPostFrameCallback((_) {
    Size? size = myWidgetKey.currentContext?.size;
    widgetWidth = size?.width;
    widgetHeight = size?.height;
  });
  print("Scale:$currentScale");
  print(
      'Widget Size: Width = ${(widgetWidth ?? 1) * currentScale}, Height = ${(widgetHeight ?? 1) * currentScale}');
});

这里有一个完整的代码为您的问题。

class MyInteractiveViewerWidget extends StatelessWidget {
  MyInteractiveViewerWidget({super.key});
  final GlobalKey myWidgetKey = GlobalKey();
  @override
  Widget build(BuildContext context) {
    TransformationController _controller = TransformationController();
    double? widgetWidth = 1;
    double? widgetHeight = 1;
    _controller.addListener(() {
      double currentScale = _controller.value.getMaxScaleOnAxis();
      WidgetsBinding.instance.addPostFrameCallback((_) {
        Size? size = myWidgetKey.currentContext?.size;
        widgetWidth = size?.width;
        widgetHeight = size?.height;
      });
      print("Scale:$currentScale");
      print(
          'Widget Size: Width = ${(widgetWidth ?? 1) * currentScale}, Height = ${(widgetHeight ?? 1) * currentScale}');
    });

    return Scaffold(
      appBar: AppBar(
        title: const Text('Mehran Ullah Khan'),
      ),
      body: InteractiveViewer.builder(
        scaleEnabled: true,
        transformationController: _controller,
        builder: (context, quad) {
          return SizedBox(
            key: myWidgetKey,
            child: Column(
              children: [
                for (var j = 1; j < 50; j++)
                  Row(
                    children: [
                      for (var i = 1; i < 50; i++)
                        Container(
                            margin: const EdgeInsets.all(2),
                            width: 500,
                            height: 300,

                            ///get random color for container
                            color: Color.fromARGB(255, i + 20, j + 30, j + 60)),
                    ],
                  )
              ],
            ),
          );
        },
      ),
    );
  }
}

相关问题