首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >颤振中的透明TextField

颤振中的透明TextField
EN

Stack Overflow用户
提问于 2021-04-02 14:31:03
回答 2查看 788关注 0票数 2

我正在尝试实现一个透明的TextField (我正在复制instagram故事编辑器UI)。然而,我得到了一个背景颜色为白色的TextField。如何删除白色背景并使TextField透明?

这是我的代码:

代码语言:javascript
运行
复制
 @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: widget.onTap,
          child: Container(
            color: Colors.black.withOpacity(0.5),
          ),
        ),   
        Center(
          child: Material(
            child: Container(
              color: Colors.transparent,
              child: TextField(
                focusNode: myFocusNode,
                cursorColor: Colors.blueAccent,
                decoration: InputDecoration(
                  fillColor: Colors.transparent,
                  filled: true,
                  border: InputBorder.none,
                  contentPadding: EdgeInsets.only(
                    left: 15,
                    bottom: 11,
                    top: 11,
                    right: 15,
                  ),
                ),
              ),
            ),
          ),
        )

以下是上述代码的结果:

我想让它看起来像:

更新:删除Material小部件和Container小部件并在顶部添加Scaffold解决了问题❤️

代码语言:javascript
运行
复制
@override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Stack(
        children: [
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: widget.onTap,
            child: Container(
              color: Colors.black.withOpacity(0.5),
            ),
          ),
          Center(
            child: TextField(
              focusNode: myFocusNode,
              cursorColor: Colors.blueAccent,
              decoration: InputDecoration(
                fillColor: Colors.transparent,
                filled: true,
                border: InputBorder.none,
                contentPadding: EdgeInsets.only(
                  left: 15,
                  bottom: 11,
                  top: 11,
                  right: 15,
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-04-02 14:37:42

使Material透明(而且不需要Container):

https://www.dartpad.dev/5c0168a92f89ab40b809d2e92c5d51c6?null_safety=true

代码语言:javascript
运行
复制
Center(
  child: Material(
    color: Colors.transparent,
    child: TextField(
      cursorColor: Colors.blueAccent,
      decoration: InputDecoration(
        fillColor: Colors.transparent,
        filled: true,
        border: InputBorder.none,
        contentPadding: EdgeInsets.only(
          left: 15,
          bottom: 11,
          top: 11,
          right: 15,
        ),
      ),
    ),
  ),
);
票数 1
EN

Stack Overflow用户

发布于 2021-04-02 14:37:03

您不需要将TextFieldContainerMaterial包装起来。Container是将白色背景添加到TextField中的那个。您只需设置TextField的边框属性:

代码语言:javascript
运行
复制
TextField(
   decoration: InputDecoration(
       border: InputBorder.none,
   ),
)

下面是InputBorderInputDecoration边框属性可用选项的引用(阅读文档中的所有属性是个好主意):https://api.flutter.dev/flutter/material/InputDecoration/border.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66920852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档