Under which circumstances textAlign property works in Flutter?

前端 未结 8 1774
花落未央
花落未央 2020-12-07 18:19

In the code below, textAlign property doesn\'t work. If you remove DefaultTextStyle wrapper which is several levels above, textAlign s

相关标签:
8条回答
  • 2020-12-07 18:40

    DefaultTextStyle is unrelated to the problem. Removing it simply uses the default style, which is far bigger than the one you used so it hides the problem.


    textAlign aligns the text in the space occupied by Text when that occupied space is bigger than the actual content.

    The thing is, inside a Column, your Text takes the bare minimum space. It is then the Column that aligns its children using crossAxisAlignment which defaults to center.

    An easy way to catch such behavior is by wrapping your texts like this :

    Container(
       color: Colors.red,
       child: Text(...)
    )
    

    Which using the code you provided, render the following :

    The problem suddenly becomes obvious: Text don't take the whole Column width.


    You now have a few solutions.

    You can wrap your Text into an Align to mimic textAlign behavior

    Column(
      children: <Widget>[
        Align(
          alignment: Alignment.centerLeft,
          child: Container(
            color: Colors.red,
            child: Text(
              "Should be left",
            ),
          ),
        ),
      ],
    )
    

    Which will render the following :

    or you can force your Text to fill the Column width.

    Either by specifying crossAxisAlignment: CrossAxisAlignment.stretch on Column, or by using SizedBox with an infinite width.

    Column(
      children: <Widget>[
        SizedBox(
          width: double.infinity,
          child: Container(
            color: Colors.red,
            child: Text(
              "Should be left",
              textAlign: TextAlign.left,
            ),
          ),
        ),
      ],
    ),
    

    which renders the following:

    In that example, it is TextAlign that placed the text to the left.

    0 讨论(0)
  • 2020-12-07 18:42
    GestureDetector(
              onTap: () {},
              child: Container(
                padding: EdgeInsets.all(5),
                color: buttonContainerColor,
                margin: EdgeInsets.only(top: 10.0),
                width: double.infinity,
                height: bottomContainerHeight,
                alignment: Alignment.center,
                child: Text(
                  "CALCULATE",
                  style: TextStyle(fontSize: 25.0, color: Colors.white),
                ),
              ),
            )
    
    0 讨论(0)
  • 2020-12-07 18:45

    Set alignment: Alignment.centerRight in Container:

    Container(
        alignment: Alignment.centerRight,
        child:Text(
           "Hello",
        ),
    )
    
    0 讨论(0)
  • 2020-12-07 18:54

    For maximum flexibility, I usually prefer working with SizedBox like this:

    Row(
                                    children: <Widget>[
                                      SizedBox(
                                          width: 235,
                                          child: Text('Hey, ')),
                                      SizedBox(
                                          width: 110,
                                          child: Text('how are'),
                                      SizedBox(
                                          width: 10,
                                          child: Text('you?'))
                                    ],
                                  )
    

    I've experienced problems with text alignment when using alignment in the past, whereas sizedbox always does the work.

    0 讨论(0)
  • 2020-12-07 18:57

    textAlign property only works when there is a more space left for the Text's content. Below are 2 examples which shows when textAlign has impact and when not.


    No impact

    For instance, in this example, it won't have any impact because there is no extra space for the content of the Text.

    Text(
      "Hello",
      textAlign: TextAlign.end, // no impact
    ),
    


    Has impact

    If you wrap it in a Container and provide extra width such that it has more extra space.

    Container(
      width: 200,
      color: Colors.orange,
      child: Text(
        "Hello",
        textAlign: TextAlign.end, // has impact
      ),
    )
    

    0 讨论(0)
  • 2020-12-07 19:02

    In Colum widget Text alignment will be centred automatically, so use crossAxisAlignment: CrossAxisAlignment.start to align start.

    Column( 
        crossAxisAlignment: CrossAxisAlignment.start, 
        children: <Widget>[ 
        Text(""),
        Text(""),
        ]);
    
    0 讨论(0)
提交回复
热议问题