简体   繁体   English

如何在flutter中将定位的小部件拖动到堆栈小部件内

[英]How to drag positioned widget inside a stack widget in flutter

I'm creating an application in which the user can drag his text over an image and can generate that image.我正在创建一个应用程序,用户可以在其中将文本拖到图像上并生成该图像。 I can drag text widget over an image, but the problem is that the text goes out of the screen if we drag it to the end in any direction(top, left, bottom, right).我可以将文本小部件拖到图像上,但问题是,如果我们将文本沿任何方向(顶部、左侧、底部、右侧)拖到最后,文本就会从屏幕上消失。 And I want that if the text is larger and the user drags it to the right, it will arrange it to the next line automatically.我希望如果文本较大并且用户将其向右拖动,它会自动将其排列到下一行。

This is the feature that I want.这是我想要的功能。 Please visit memegenerator ,请访问memegenerator ,

Code代码

import 'package:flutter/material.dart';

class DesignScreen extends StatefulWidget {
  final String formData;

  const DesignScreen({Key? key, required this.formData}) : super(key: key);

  @override
  State<DesignScreen> createState() => _DesignScreenState();
}

class _DesignScreenState extends State<DesignScreen> {
  Offset _offset = Offset.zero;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Design"),
      ),
      body: Column(
        children: [
          GestureDetector(
            child: Stack(
              children: [
                Image.asset(
                  "assets/images/oneIO.jpg",
                  width: double.infinity,
                ),
                Positioned(
                  top: _offset.dy,
                  left: _offset.dx,
                  child: FittedBox(
                    fit: BoxFit.contain,
                    child: Text(widget.formData),
                  ),
                ),
              ],
            ),
            onPanUpdate: (details) {
              setState(() {
                _offset = Offset(_offset.dx + details.delta.dx,
                    _offset.dy + details.delta.dy);
              });
            },
          )
        ],
      ),
    );
  }
}

They go out-of-screen because their top and left properties are either too small or too large.它们超出屏幕是因为它们的topleft属性要么太小要么太大。 You can use clamp function to limit the offset to a range.您可以使用clamp功能将偏移量限制在一个范围内。 For example:例如:

  Positioned(
    top: _offset.dy.clamp(0, 200),
    left: _offset.dx.clamp(0, 200),
    child: ...
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Flutter 中的 Stack 小部件内移动、缩放和调整定位小部件的大小 - Move, zoom and resize Positioned widget inside Stack widget in Flutter 如何在flutter中定位定位小部件中的文本? - How to align text inside Positioned widget in flutter? 如何让 Positioned 小部件在 Stack (flutter) 中工作? - How can I get the Positioned widget to work in a Stack (flutter)? 如何让 Stack 小部件将自身约束到 Flutter 中的定位子项? - How to have a Stack widget constrain itself to a Positioned child in Flutter? Flutter - 堆栈中的定位小部件导致异常 - Flutter - Positioned Widget in Stack causing Exception 如何根据内部定位的元素扩展 Stack Widget? - How do I expand a Stack Widget depending on positioned elements inside? 如何在 Flutter 中的 StaggeredGridView 中使用堆栈小部件? - How to use stack widget inside a StaggeredGridView in flutter? ListView 小部件内的 Flutter Stack 小部件? - Flutter Stack Widget inside ListView widget? Flutter 在堆栈和列内包装定位小部件时出错(无限像素溢出的 RenderFlex。) - Flutter error when wrapping a Positioned widget inside Stack and Column (A RenderFlex overflowed by Infinity pixels.) 堆栈小部件内的颤振定位类,不接受子属性 - Flutter Positioned class inside stack widget, won't accept a child property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM