【问题标题】:How to horizontally center a Text in flutter?如何在颤动中水平居中文本?
【发布时间】:2019-08-05 14:15:01
【问题描述】:

我正在尝试将文本水平居中。请检查以下代码。

import 'package:flutter/material.dart';

class LoginPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
        color: Colors.black,
        child: Row(
          children: <Widget>[
            Column(
              children: <Widget>[_buildTitle()],
            ),
          ],
        ));
  }

  Widget _buildTitle() {
    return 
    Center(child: Container(
      margin: EdgeInsets.only(top: 100),
      child: Column(
        
        children: <Widget>[
          Text(
            "something.xyz",
            style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 25,),
            textAlign: TextAlign.center,
          ),
        ],
      ),
    ),);
    
  }
}

这并没有水平居中,而是给出了以下输出。边距等很好。

我该如何解决这个问题?

【问题讨论】:

  • 你可以试试列属性crossAxisAlignment : CrossAxisAlignment.center
  • @Ryosuke:那没用

标签: dart flutter flutter-layout


【解决方案1】:
  Widget textSection = Container(
      child: Text(
      'This can be several lines centered in the child of a container Widget.',
      textAlign: TextAlign.center,
      style: TextStyle(
      fontSize: 15.0,
      color: Colors.white,
    ),
  ),
);

【讨论】:

    【解决方案2】:

    设置

    child: Center(
        child: Text(
          "Hello World",
          textAlign: TextAlign.center,
        ),
      ),
    

    在程序中

    【讨论】:

      【解决方案3】:

      您也可以使用ContainerTextAlign 来解决它:

      Container(
         width: double.infinity,
         child: Text(
            'something.xyz',
            textAlign: TextAlign.center,
         ),
      )
      

      在这种情况下,容器以double.infinity 占据整个宽度。文字适应容器,可以用TextAlign.center移动到屏幕中间。

      【讨论】:

        【解决方案4】:

        更简单的方法:

        child: Center(
                       child: Row(
                         mainAxisAlignment: MainAxisAlignment.center,
                         children: <Widget>[
                           Text('Sunday', style: TextStyle(fontSize: 20),),
                         ],
                       ),
                     ), //Center
        

        【讨论】:

          【解决方案5】:

          我为这个用例添加了我自己的小部件,它比行解决方案短得多:

          import 'package:flutter/material.dart';
          
          class CenterHorizontal extends StatelessWidget {   
          
              CenterHorizontal(this.child);   
              final Widget child;
          
              @override   
             Widget build(BuildContext context) => 
                  Row(mainAxisAlignment: MainAxisAlignment.center,children: [child]); 
          }
          

          结果是这样的:

          CenterHorizontal(Text('this is horizontal centered'))
          

          【讨论】:

            【解决方案6】:

            如果我理解,您只想将标题水平居中,而不是我想可能出现的其他元素。

            看看下面的代码:

            import 'package:flutter/material.dart';
            
            class LoginPage extends StatelessWidget {
              @override
              Widget build(BuildContext context) {
                // TODO: implement build
                return Scaffold(
                    appBar: AppBar(
                      backgroundColor: Colors.blueAccent,
                      title: Text("DEMO"),
                    ),
                    body: Container(
                        color: Colors.black,
                        child: Column(
                          children: <Widget>[
                            Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[_buildTitle()],
                            ),
                            Row(
                              children: <Widget>[
                                // add other elements that you don't to center horizontally
                                Text(
                                  "other elements here",
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontWeight: FontWeight.bold,
                                    fontSize: 25,
                                  ),
                                ),
                              ],
                            ),
                          ],
                        )));
              }
            
              Widget _buildTitle() {
                return Container(
                  margin: EdgeInsets.only(top: 100),
                  child: Text(
                    "something.xyz",
                    style: TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.bold,
                      fontSize: 25,
                    ),
                  ),
                );
              }
            }
            

            给出的结果:here

            【讨论】:

              【解决方案7】:

              试试这个

              import 'package:flutter/material.dart';
              
              void main() => runApp(MaterialApp(home: LoginPage()));
              
              class LoginPage extends StatelessWidget {
                @override
                Widget build(BuildContext context) {
                  // TODO: implement build
                  return Scaffold(
                    body: Container(
                        color: Colors.black,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[_buildTitle()],
                            ),
                          ],
                        )),
                  );
                }
              
                Widget _buildTitle() {
                  return Center(
                    child: Container(
                      margin: EdgeInsets.only(top: 100),
                      child: Column(
                        children: <Widget>[
                          Text(
                            "something.xyz",
                            style: TextStyle(
                              color: Colors.white,
                              fontWeight: FontWeight.bold,
                              fontSize: 25,
                            ),
                            // textAlign: TextAlign.center,
                          ),
                        ],
                      ),
                    ),
                  );
                }
              }
              

              【讨论】:

              • 嗯,这很好,但我不需要水平的一切。 title 是我添加到屏幕的第一件事,还会有许多其他元素。当您水平对齐 row 内的 Scaffold 时,所有内容都会水平对齐。
              • 你想达到什么目的?你想在中心或什么上使用相同的文字?
              • 受您的回答启发,我做了一些更改,现在它可以工作了。我会尽快发布我的代码。
              猜你喜欢
              • 2016-02-21
              • 2022-01-16
              • 1970-01-01
              • 1970-01-01
              • 2019-07-26
              • 2011-03-29
              • 2019-10-19
              • 2015-04-29
              • 2016-08-06
              相关资源
              最近更新 更多