Search code examples
flutterdartlayoutflutter-layout

How to center Text and make as big as given size


I have a view, where I have GestureDetector which has onHorizontalDragEnd function. As a child I want to have centered Text and possibility to swipe horizontally starting from every place on screen. My problem is that when I put Text in Center widget I can only swipe only when I start on Text.

I tried to wrap Text with Container. It fixes problem with swiping, but Text is not centered.

Here is how it looks now:

Center(
   child: Container(
   height: size.height * 0.6,
   child: Text(
       'example text',
       textAlign: TextAlign.center,
       ),
   ),
)

Solution

  • Add alignment: Alignment.center in your container

    Center(
       child: Container(
       height: size.height * 0.6,
       alignment: Alignment.center, //HERE
       child: Text(
           'example text',
           textAlign: TextAlign.center,
           ),
       ),
    )