简体   繁体   English

如何将文本居中并使其与给定大小一样大

[英]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.我有一个视图,其中我有 GestureDetector,它具有 onHorizontalDragEnd function。作为一个孩子,我希望文本居中并且可以从屏幕上的每个位置开始水平滑动。 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,
       ),
   ),
)

Add alignment: Alignment.center in your container在容器中添加alignment: Alignment.center

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM