【问题标题】:Flutter: why is a Positioned widget transparent over a TextField?Flutter:为什么定位小部件在 TextField 上是透明的?
【发布时间】:2021-07-31 00:12:57
【问题描述】:

为什么Positioned红色widget是透明的,让Second Label Text widget可以透过它看到?

设置: 栏目:

  • 堆栈
    • 文本字段
    • 已定位
      • 红色容器
  • TextField(第二个标签文本

其目的是红色小部件以不透明的方式覆盖其下方的文本字段。

谢谢

@override
  Widget build(BuildContext context) {
    const pad = 16.0;
    const vertPadding = 10.0;

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Column(children: [
        Stack(clipBehavior: Clip.none, children: [
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 5.0),
            child: TextField(
              autocorrect: false,
              maxLines: null,
              decoration: InputDecoration(
                border: _border,
                labelText: "Label text",
                labelStyle: TextStyle(color: Colors.grey),
              ),
            ),
          ),
          Positioned(
            top: pad,
            left: pad,
            width: 200.0,
            child: Container(
              decoration: BoxDecoration(
                color: Colors.red,
              ),
              width: 200,
              height: 84,
              child: Padding(
                padding:
                    const EdgeInsets.fromLTRB(16, vertPadding, 0, vertPadding),
                child: Container(),
              ),
            ),
          ),
        ]),
        Padding(
          padding: const EdgeInsets.fromLTRB(25, 0, 25, 0),
          child: TextField(
            decoration: InputDecoration(
              border: _border,
              labelText: "Second Label text",
            ),
          ),
        )
      ]),
    );
  }

  final OutlineInputBorder _border = OutlineInputBorder(
    borderRadius: BorderRadius.circular(4.0),
    borderSide: BorderSide(
      color: Colors.grey,
      width: 1.0,
    ),
  );

【问题讨论】:

  • 如果在Colors.red 之后添加.withOpacity(1) 会怎样?
  • 是的,我试过了,结果一样。我什至添加了一个装饰图像作为装饰,一个不透明的图像,同样的东西。

标签: flutter transparent flutter-positioned


【解决方案1】:

你有没有想过为什么第一个文本字段在红框后面,第二个文本字段在红框上方?这是因为它们在堆栈的小部件列表中的索引。

您的小部件树是错误的。父小部件应该是堆栈,它的第一个孩子应该是两个文本字段的列,第二个孩子将是你想要的红色框。试试下面的代码,并在评论中告诉我。

@override
Widget build(BuildContext context) {
const pad = 16.0;
const vertPadding = 10.0;

return Scaffold(
  appBar: AppBar(
    title: Text(""),
  ),
  body: Stack(clipBehavior: Clip.none, children: [
    Column(
      children: [
        Padding(
          padding:
              const EdgeInsets.symmetric(horizontal: 8.0, vertical: 5.0),
          child: TextField(
            autocorrect: false,
            maxLines: null,
            decoration: InputDecoration(
              border: _border,
              labelText: "Label text",
              labelStyle: TextStyle(color: Colors.grey),
            ),
          ),
        ),
        Padding(
          padding: const EdgeInsets.fromLTRB(25, 0, 25, 0),
          child: TextField(
            decoration: InputDecoration(
              border: _border,
              labelText: "Second Label text",
            ),
          ),
        )
      ],
    ),
    Positioned(
      top: pad,
      left: pad,
      width: 200.0,
      child: Container(
        decoration: BoxDecoration(
          color: Colors.red,
        ),
        width: 200,
        height: 84,
        child: Padding(
          padding:
              const EdgeInsets.fromLTRB(16, vertPadding, 0, vertPadding),
          child: Container(),
        ),
      ),
    ),
  ]),
);
}

【讨论】:

  • 感谢您的回答,但不,小部件树是正确的。在这个现实世界的应用程序中,一列中有多个小部件,其中一个小部件是堆栈。在堆栈之后,下一个小部件可以是任何东西,堆栈的 Positioned 子项在其溢出部分不应是透明的。如果将堆栈的兄弟(第二个文本字段)替换为容器,则 Positioned 小部件将不透明,并将不透明地覆盖此容器。仅当堆栈的直接兄弟是 TextField 时才会发生这种情况。
  • 是的,堆栈会允许小部件溢出,你是对的,它显然可以放在列中。但是在您的情况下,如果您将父小部件堆叠起来,这很容易。你实现了你想要的吗?
  • 从我的应用程序的设计观点来看,堆栈之后的其他小部件不应该是堆栈的一部分,它们应该一个接一个地垂直放置。使用堆栈来实现这将很麻烦,这是一个列的工作。
  • @johnnyMac “只有在堆栈的直接兄弟是 TextField 时才会发生这种情况。”不,它没有。如果您在第二个文本字段所在的位置使用彩色Container,它仍将覆盖红色框。 Pratham 对您的小部件树的看法是正确的
  • @johnnyMac 如果您想控制 z 轴排序,请尝试在您当前的 ColumnStack 周围使用第二个 Stack
猜你喜欢
  • 1970-01-01
  • 2022-08-04
  • 1970-01-01
  • 2021-12-04
  • 2020-02-24
  • 2020-06-26
  • 1970-01-01
  • 2019-08-23
  • 2020-04-16
相关资源
最近更新 更多