如何使用Flutter Layout小部件准确定位文本?

发布于 2025-02-12 08:17:42 字数 1264 浏览 0 评论 0 原文

我正在尝试以扑动布局中的特定x/y坐标叠加文本。这是我要实现的目标:

“所需结果”

当前,我的布局看起来像这样:

我希望文本位于x:39和y:82。

我尝试使用定位,但是我有难以使其正常工作。我了解 stack 也可能是解决方案。有人可以使用Flutter的布局小部件准确地放置文本吗?

代码:

class LogIn extends StatefulWidget {
  const LogIn({Key? key}) : super(key: key);

  @override
  State<LogIn> createState() => _LogInState();
}

class _LogInState extends State<LogIn> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        backgroundColor: Color(0xffF24004),
        body: Center(
          child: Container(
              width: 350,
              height: 350,
              decoration: BoxDecoration(
                  image: DecorationImage(
                image: AssetImage("lib/img/pomodoro.png"),
              ))),
        ),
      ),
    );
  }
}

I'm trying to overlay text at a specific X/Y coordinate within a Flutter layout. Here's what I want to achieve:

desired result

Currently, my layout looks like this:

current result

I want the text positioned at X: 39 and Y: 82.

I've tried using Positioned, but I'm having trouble getting it to work correctly. I understand Stack might also be a solution. Could someone please explain how to position the text accurately using Flutter's layout widgets?

Code:

class LogIn extends StatefulWidget {
  const LogIn({Key? key}) : super(key: key);

  @override
  State<LogIn> createState() => _LogInState();
}

class _LogInState extends State<LogIn> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        backgroundColor: Color(0xffF24004),
        body: Center(
          child: Container(
              width: 350,
              height: 350,
              decoration: BoxDecoration(
                  image: DecorationImage(
                image: AssetImage("lib/img/pomodoro.png"),
              ))),
        ),
      ),
    );
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满地尘埃落定 2025-02-19 08:17:42

您将要在堆栈内使用位置的小部件。

位置小部件允许您放置在X和Y

时,而堆栈可确保位置小部件不受任何其他小部件的约束。

用这样的两个小部件包装您的容器:

Stack(
  children[
    Positioned(
      left: 39,
      top: 82
      child: Container(
              width: 350,
              height: 350,
              decoration: BoxDecoration(
                image: DecorationImage(
            image: AssetImage("lib/img/pomodoro.png"),
          ))),
         )
       ]
)

You're going to want to use a Positioned Widget inside of a Stack.

The Position Widget allows you to place at an X and Y

while the Stack ensures that the position widget is not bound by any other widgets.

Wrap your Container with those two widget like this:

Stack(
  children[
    Positioned(
      left: 39,
      top: 82
      child: Container(
              width: 350,
              height: 350,
              decoration: BoxDecoration(
                image: DecorationImage(
            image: AssetImage("lib/img/pomodoro.png"),
          ))),
         )
       ]
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文