How do I center text vertically and horizontally in Flutter?

后端 未结 8 1314
不思量自难忘°
不思量自难忘° 2020-12-13 05:34

I\'d like to know how to center the contents of a Text widget vertically and horizontally in Flutter. I only know how to center the widget itself using Center(child: T

相关标签:
8条回答
  • 2020-12-13 05:45

    Text element inside Center of SizedBox work much better way, below Sample code

    Widget build(BuildContext context) {
        return RawMaterialButton(
          fillColor: Colors.green,
          splashColor: Colors.greenAccent,
          shape: new CircleBorder(),
          child: Padding(
            padding: EdgeInsets.all(10.0),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                SizedBox(
                  width: 100.0,
                  height: 100.0,
                  child: Center(
                    child: Text(
                    widget.buttonText,
                    maxLines: 1,
                    style: TextStyle(color: Colors.white)
                  ),
                  )
              )]
            ),
        ),
      onPressed: widget.onPressed
    );
    }
    

    Enjoy coding

    0 讨论(0)
  • 2020-12-13 05:47

    Text alignment center property setting only horizontal alignment.

    I used below code to set text vertically and horizontally center.

    Code:

          child: Center(
            child: Text(
              "Hello World",
              textAlign: TextAlign.center,
            ),
          ),
    
    0 讨论(0)
  • 2020-12-13 05:51

    Overview: I used the Flex widget to center text on my page using the MainAxisAlignment.center along the horizontal axis. I use the container padding to create a margin space around my text.

      Flex(
                direction: Axis.horizontal,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                    Container(
                        padding: EdgeInsets.all(20),
                        child:
                            Text("No Records found", style: NoRecordFoundStyle))
      ])
    
    0 讨论(0)
  • 2020-12-13 05:55

    I think a more flexible option would be to wrap the Text() with Align() like so:

    Align(
      alignment: Alignment.center, // Align however you like (i.e .centerRight, centerLeft)
      child: Text("My Text"),
    ),
    
    

    Using Center() seems to ignore TextAlign entirely on the Text widget. It will not align TextAlign.left or TextAlign.right if you try, it will remain in the center.

    0 讨论(0)
  • 2020-12-13 05:58

    If you are a intellij IDE user, you can use shortcut key Alt+Enter and then choose Wrap with Center and then add textAlign: TextAlign.center

    0 讨论(0)
  • 2020-12-13 06:00
                           child: Align(
                              alignment: Alignment.center,
                              child: Text(
                                'Text here',
                                textAlign: TextAlign.center,                          
                              ),
                            ),
    

    This produced the best result for me.

    0 讨论(0)
提交回复
热议问题