[英]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.它们超出屏幕是因为它们的
top
和left
属性要么太小要么太大。 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.