首页 > 解决方案 > 容器包装了文本小部件,但为什么按钮小部件会受到 textAlign 的影响?

问题描述

先生,在代码中我包装了唯一的 Text 小部件。之后,我使用了一些自定义样式,例如文本对齐。但是在使用之后,我希望只将文本部分居中,因为只有这个小部件不会被包裹到另一个小部件上。但我看到我的按钮也居中。怎么可能?我被包裹了一个小部件,但我的另一个小部件受到影响。先生,这里面有什么逻辑? 截屏

标签: androidflutter

解决方案


您需要设置一个crossAxisAlignment: CrossAxisAlignment.start, 并用中心小部件包装标题文本。希望下面的代码可以给你一个很好的例子。

class Demo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("title"),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Center(
            child: Text("Whats your favourite food?"),
          ),
          RaisedButton(
            onPressed: () {},
            child: Container(
              padding: EdgeInsets.zero,
              decoration: BoxDecoration(),
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text("Answer 1"),
              ),
            ),
          ),
          RaisedButton(
            onPressed: () {},
            child: Container(
              padding: EdgeInsets.zero,
              decoration: BoxDecoration(),
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text("Answer 1"),
              ),
            ),
          ),
          RaisedButton(
            onPressed: () {},
            child: Container(
              padding: EdgeInsets.zero,
              decoration: BoxDecoration(),
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text("Answer 1"),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

推荐阅读