首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在颤动中通过边框的文本

在颤动中通过边框的文本
EN

Stack Overflow用户
提问于 2020-01-12 17:56:41
回答 2查看 3.2K关注 0票数 5

我想在flutter中通过容器边框添加文本。

我只想让这个地址出现在顶部边框之间的空隙中。使用定位的小部件似乎不太可能,因为这样边框线就会通过"Address“文本出现。

这有可能吗?

EN

回答 2

Stack Overflow用户

发布于 2020-01-12 23:09:20

这是你想要的吗?

代码语言:javascript
运行
复制
class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  FocusNode focusNode = FocusNode();
  bool isFocused = false;

  @override
  void initState() {
    focusNode.addListener(_onFocusChange);
    super.initState();
  }

  void _onFocusChange() {
    setState(() => isFocused = !isFocused);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        margin: const EdgeInsets.symmetric(horizontal: 500, vertical: 300),
        child: Stack(
          children: <Widget>[
            Container(
              padding: const EdgeInsets.only(top: 10),
              child: TextFormField(
                focusNode: focusNode,
                decoration: InputDecoration(
                  border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(30)),
                ),
              ),
            ),
            Align(
              alignment: Alignment.topCenter,
              child: Container(
                color: Colors.white,
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 8.0),
                  child: Text(
                    'Address',
                    style:
                        isFocused ? TextStyle(color: Colors.blue[800]) : null,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

据我所知,没有办法在InputDecoration中以labelText为中心。

票数 10
EN

Stack Overflow用户

发布于 2020-01-12 19:37:24

它必须是容器边界吗?如果没有,您可以将TextField与InputDecoration一起使用,如下所示:

代码语言:javascript
运行
复制
TextField(
     decoration: InputDecoration(
         border: OutlineInputBorder(),
         labelText: 'Label Text',
     ),
     textAlign: TextAlign.center,
),

但遗憾的是,TextField不支持居中的标签文本(textAlign: TextAlign.center,只支持居中的提示文本)。如果要使标签文本居中,则必须更改TextField.dart

这是TextField,所以它不像通常的Text,因为它是可编辑的。如果您希望它类似于文本,请设置enabled: false,并为其指定一个控制器,然后设置一个初始值。或者你可以使用TextFormField,这样你就不必使用控制器了。如下所示:

代码语言:javascript
运行
复制
TextFormField(
    decoration: InputDecoration(
        border: OutlineInputBorder(),
        labelText: 'Address',
    ),
    textAlign: TextAlign.center,
    enabled: true,
    initialValue: 'Address Here',
),
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59702697

复制
相关文章

相似问题

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