在 flutter 中创建可调整大小的视图

2023-12-01

我正在为图像创建一个可调整大小的视图。代码如下:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: SafeArea(
          child: ImageManager(),
        ),
      ),
    );
  }
}

final ballRadius = 7.5;

class ImageManager extends StatefulWidget {
  @override
  _ImageManagerState createState() => _ImageManagerState();
}

class _ImageManagerState extends State<ImageManager> {
  double _x = 0;
  double _y = 0;

  double _height = 200;
  double _width = 300;

  double _aspectRatio = 200 / 300;

  @override
  Widget build(BuildContext context) {
    return Stack(
      overflow: Overflow.visible,
      children: <Widget>[
        Positioned(
          top: _y,
          left: _x,
          child: GestureDetector(
            onPanUpdate: (DragUpdateDetails details) {
              setState(() {
                _x += details.delta.dx;
                _y += details.delta.dy;
              });
            },
            child: Image.network(
              "https://via.placeholder.com/300x200",
              width: _width,
            ),
          ),
        ),

        // top left
        Positioned(
          top: _y - ballRadius,
          left: _x - ballRadius,
          child: Ball(
            onDragStart: () {},
            onDrag: (double dx, double dy) {},
            onDragEnd: () {},
          ),
        ),

        // top middle
        Positioned(
          top: _y - ballRadius,
          left: _x + _width / 2 - ballRadius,
          child: Ball(
            onDragStart: () {},
            onDrag: (double dx, double dy) {},
            onDragEnd: () {},
          ),
        ),

        // top right
        Positioned(
          top: _y - ballRadius,
          left: _x + _width - ballRadius,
          child: Ball(
            onDragStart: () {},
            onDrag: (double dx, double dy) {},
            onDragEnd: () {},
          ),
        ),

        // middle left
        Positioned(
          top: _y + _height / 2 - ballRadius,
          left: _x - ballRadius,
          child: Ball(
            onDragStart: () {},
            onDrag: (double dx, double dy) {},
            onDragEnd: () {},
          ),
        ),

        // middle right
        Positioned(
          top: _y + _height / 2 - ballRadius,
          left: _x + _width - ballRadius,
          child: Ball(
            onDragStart: () {},
            onDrag: (double dx, double dy) {},
            onDragEnd: () {},
          ),
        ),

        // bottom left
        Positioned(
          top: _y + _height - ballRadius,
          left: _x - ballRadius,
          child: Ball(
            onDragStart: () {},
            onDrag: (double dx, double dy) {},
            onDragEnd: () {},
          ),
        ),

        // bottom middle
        Positioned(
          top: _y + _height - ballRadius,
          left: _x + _width / 2 - ballRadius,
          child: Ball(
            onDragStart: () {},
            onDrag: (double dx, double dy) {},
            onDragEnd: () {},
          ),
        ),

        // bottom right
        Positioned(
          top: _y + _height - ballRadius,
          left: _x + _width - ballRadius,
          child: Ball(
            onDragStart: () {},
            onDrag: (double dx, double dy) {
              var mid = (dx + dy) / 2;
              var newWidth = _width + 2 * mid;
              var newHeight = newWidth * _aspectRatio;

              setState(() {
                _width = newWidth;
                _height = newHeight;
                _y = _y - dy;
                _x = _x - 2 * dx;
              });
            },
            onDragEnd: () {},
          ),
        ),
      ],
    );
  }
}

class Ball extends StatelessWidget {
  final Function onDragStart;
  final Function onDrag;
  final Function onDragEnd;

  const Ball({this.onDragStart, this.onDrag, this.onDragEnd});

  void _onDragStart(DragStartDetails details) {
    if (onDragStart != null) onDragStart();
  }

  void _onDragUpdate(DragUpdateDetails details) {
    if (onDrag != null) onDrag(details.delta.dx, details.delta.dy);
  }

  void _onDragEnd(DragEndDetails details) {
    if (onDragEnd != null) onDragEnd();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onPanStart: _onDragStart,
      onPanUpdate: _onDragUpdate,
      onPanEnd: _onDragEnd,
      child: Container(
        height: 2 * ballRadius,
        width: 2 * ballRadius,
        decoration: BoxDecoration(
          color: Colors.blue,
          borderRadius: BorderRadius.circular(ballRadius),
          border: Border.all(
            width: 3,
            color: Colors.white,
          ),
        ),
      ),
    );
  }
}

我的目标是统一调整大小,如下所示:

enter image description here

然而,目前看来是这样的。

enter image description here

正如你所看到的,x 和 y 坐标被弄乱了。这里的目标是,如果您从右下角调整图像大小,则图像将保留在左上角。请帮忙解决这个问题。谢谢。


我像这样改变左上角的位置

 // top left
    Positioned(
      top: _y - ballRadius,
      left: _x - ballRadius,
      child: Ball(
        onDragStart: () {},
        onDrag: (double dx, double dy) {
          var newWidth = _width - dx;
          var newHeight = newWidth * _aspectRatio;
          setState(() {
            _y = _y + (_height - newHeight);
            _x = _x + dx;
            _width = newWidth ;
            _height = newHeight;
          });
        },
        onDragEnd: () {},
      ),
    ),

和右下角定位(只是为了完成答案)

        Positioned(
      top: _y + _height - ballRadius,
      left: _x + _width - ballRadius,
      child: Ball(
        onDragStart: () {},
        onDrag: (double dx, double dy) {

          var newWidth = _width + dx;
          var newHeight = newWidth * _aspectRatio;

          setState(() {
            _width = newWidth ;
            _height = newHeight;
          });
        },
        onDragEnd: () {},
      ),
    ),

并将此参数添加到 image.network

fit: BoxFit.fill,

完整代码在这里:https://dartpad.dev/44adae92cecbd2dddc00f264293e5c3a

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 flutter 中创建可调整大小的视图 的相关文章

随机推荐

  • jquery ajax调用不是异步的

    我对 jQuery 的 AJAX 方法的实现很陌生 我有一个简单的设置 可以访问两个不同的页面 一个需要 10 秒才能完成 我在上面设置了一个计时器 另一个检查第一页的状态 这两个函数是progressCheck 它每秒请求其页面的最新状态
  • 使用邻接列表与邻接矩阵的图的大小?

    Suppose there are 236 web pages and on average each web page has 24 hyperlinks Consider the directed graph with one vert
  • 在 Python 中通过 Selenium 导航到新页面

    如何在 python 中使用与 Selenium 相同的驱动程序导航到另一个网页 我不想打开新页面 我想继续使用同一个驱动程序 我认为以下方法会起作用 driver navigate to https support tomtom com
  • 如何在 PHP 中构建跨数据库查询?

    在我们的最后一集中 我如何在 MySQL 中构建跨数据库查询 我学会了如何在MySQL中构建跨数据库查询 这很有效 但是当我们的英雄尝试在 PHP 中使用这些新知识时 他发现他最好的朋友失败了 我看了一下mysql select db对于
  • 如何在应用程序中的每个 MessageBox 处放置断点?

    如何在应用程序中的每个 MessageBox 处放置断点 在 Visual Studio IDE 中选择菜单Debug gt New breakpoint gt Breakpoint at Function 用文本 MessageBox 填
  • Retrofit 收到 400 错误请求,但与邮递员一起使用

    我的 api 基本网址是 https locodealapi herokuapp com api deals 在邮递员中 在标题中传递以下内容 效果很好 x access token eyJhbGciOiJIUzI1NiIsInR5cCI6
  • VBA - 如果 A 列中的单元格不为空,则 B 列等于

    我正在寻找一些代码来查看 A 列 只要 A 列中的单元格不为空 那么 B 列中的相应单元格将等于特定值 因此 如果 Cell A1 则 Cell B1 Value MyText 重复此操作 直到 A 列中的单元格为空白或为空 为了添加更多说
  • Mercurial - 添加不在存储库文件夹内的项目

    我是一个没有经验的 Mercurial 用户 我正在使用 VS2010 和 Mercurial 我让一切都在一个存储库上运行良好 该存储库包含一个解决方案文件和嵌套在 父 文件夹中的两个项目 每当我更改文件时 提交过程都会检测到更改等 然后
  • 如何为 Swing 中的 JTable 提供分页支持?

    我在 Swing Java 中创建了一个 GUI 其中使用了 JTable 现在我想通过使用分页在其中显示下一页信息 我该怎么做呢 实现此目的的另一个选项是使用无滚动条的滚动窗格和几个导航按钮来实现控制 已经添加的按钮都是正常的JButto
  • 如何从 Analytics Application Insights 获取 Qna Maker“Q”?

    我已经使用 Qna Maker 创建了聊天机器人的知识库 并尝试使用 Analytics Application Insights 可视化一些统计数据 我想做的事 我想创建一个图表 其中包含最常见的 Qna Maker 问题 我的问题 我在
  • 读取 React 应用程序 src 文件夹中的 excel

    我使用创建了一个反应应用程序react CLI 我现在创建了一个名为的文件夹data现在我想使用读取该数据xlsxnpm 包 但是 它不起作用 我认为这可能与我引用 Excel 文件的方式有关 因为我收到警告 字符串 类型上不存在属性 工作
  • 当我使用另一个控制器渲染页面时,表单验证停止工作

    我编写了一个快速 CI 库类来渲染我的页面 这样我就不必一直输入 this gt load gt view 并且保持 DRY 现在 当我在传递无效数据后重新呈现联系表单时 错误消息不会显示 图书馆类 class Page extends C
  • 如何让用户在 Laravel 5 中切换语言?

    我创建了一个双语 Laravel 5 应用程序 其中包含两个语言环境 en 和 ar 我想要的是网站访问者能够通过单击标有语言名称的链接来更改网站的语言 选项1 将用户语言存储在数据库中 我的用户语言存储在用户表中 这是为了避免用户每次登录
  • 如何使用 Win32::Console 读取特殊键?

    当我按下Up key 这个脚本 术语 术语密钥 输出You pressed
  • 如何停止EnumWindows无限运行win32

    该代码一直有效 不知何故 我设法让 Visual C Express 不会在最终返回语句上遇到断点 并且它似乎会永远运行 在下面的示例代码中 EnumWindows 无限枚举 在枚举完所有窗口后 如何才能使其停止 include
  • C# 剥离/转换一个或多个字符

    有没有一种快速的方法 无需显式循环字符串中的每个字符 并剥离或保留它 在 Visual FoxPro 中 有一个函数 CHRTRAN 可以很好地完成此任务 它采用 1 1 字符替换 但如果替代位置中没有字符 则会从最终字符串中删除该字符 前
  • 如何将 getSymbols(quantmod 库)中的数据存储到列表中?

    这是我正在运行的代码 library quantmod library tseries Stocks companies c IOC BO BPCL BO ONGC BO HINDPETRO BO GAIL BO for i in comp
  • 从代码隐藏或 XAML 中设置 NotifyIcon 控件中的图像

    我正在使用 WindowsForms 中的 NotifyIcon 因为在 WPF 中我们没有这样的控件 但 WinForms 中的控件工作正常 我的问题只是当图像位于项目中时将图像设置为 NotifyIcon 中的图标 我的项目中有一个名为
  • 使用 CreateInstance 对表格单元格内容进行 Specflow 步骤参数转换

    有没有人解决了如何结合 SpecFlow Assist CreateInstance CreateSet 将 SpecFlow 步骤参数转换应用于表格中的单元格的难题 这里合并代码以节省空间 Given a table like the f
  • 在 flutter 中创建可调整大小的视图

    我正在为图像创建一个可调整大小的视图 代码如下 import package flutter material dart void main gt runApp MyApp class MyApp extends StatelessWidg