【问题标题】:Flutter: Avoid Text Widget re-centering when Timer is activeFlutter:当 Timer 处于活动状态时,避免 Text Widget 重新居中
【发布时间】:2021-12-12 05:17:15
【问题描述】:

我有一个带有Scaffold 的颤振应用程序,我在其中放置了以下容器。 此容器包含计时器的分钟和秒,以及两个按钮。 我试图避免Text 小部件在计时器处于活动状态时重新居中。这似乎是因为 Text 小部件在显示不同的数字时会改变大小。

我尝试用Expanded 小部件包装Text 小部件,但它没有解决问题。我该如何解决这个问题?

Container(
                      height: size.height * 0.40,
                      width: size.width * 0.80,
                      decoration: BoxDecoration(
                          color: Color(0xFFf4f6fa),
                          borderRadius: BorderRadius.circular(29.5)),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              Expanded(
                                child: Text(
                                  "${f.format(_minutes)}:${f.format(_seconds)}",
                                  textAlign: TextAlign.center,
                                  style: TextStyle(
                                    fontSize: 80,
                                    color: Color(0xff7165E3),
                                  ),
                                ),
                              ),
                            ],
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: [
                              // STOP BUTTON
 
                              Container(
                                decoration: BoxDecoration(
                                  color: Colors.grey[800],
                                  shape: BoxShape.circle,
                                ),
                                height: 90,
                                width: 90,
                                child: ElevatedButton(
                                  style: ButtonStyle(
                                    backgroundColor: MaterialStateProperty.all(
                                        Colors.grey[800]),
                                    shape: MaterialStateProperty.all(
                                        CircleBorder()),
                                  ),
                                  onPressed: () {
                                    setState(() {
                                      _stopTimer();
                                    });
                                  },
                                  child: Container(
                                    alignment: Alignment.center,
                                    child: Text(
                                      "Annulla",
                                      style: TextStyle(
                                        fontSize: 14,
                                        fontWeight: FontWeight.bold,
                                        color: Colors.white,
                                      ),
                                    ),
                                  ),
                                ),
                              ),
 
                              // START BUTTON 
 
                              Container(
                                decoration: BoxDecoration(
                                  shape: BoxShape.circle,
                                ),
                                height: 90,
                                width: 90,
                                child: ElevatedButton(
                                  style: ButtonStyle(
                                    backgroundColor: MaterialStateProperty.all(
                                        Color(0xff7165E3)),
                                    shape: MaterialStateProperty.all(
                                        CircleBorder()),
                                  ),
                                  onPressed: () {
                                    _startTimer();
                                  },
                                  child: Container(
                                    alignment: Alignment.center,
                                    child: Text(
                                      "Avvia",
                                      style: TextStyle(
                                        fontSize: 14,
                                        fontWeight: FontWeight.bold,
                                        color: Colors.white,
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),

【问题讨论】:

  • 你在 f.format(_minutes) 函数中有什么?
  • @Vettiyanakan 我正在使用 intl 包来显示两位数,我有这个 var f = NumberFormat("00");。如果您需要更多信息,请随时询问
  • 你能发布完整的页面代码吗?至少声明和那些函数。
  • 请查看此 FB 帖子和 cmets,facebook.com/vettiyankan.vm/posts/900287173767708:7
  • @Vettiyanakan 这里有完整页面代码:pastebin.com/fj2PyK8n

标签: android flutter dart user-interface timer


【解决方案1】:

这是因为您使用的字体。

您使用的字体没有负空格。尝试使用一些调整了负空间的字体。 I Roboto Mono 是一种有负空间的字体。

在上图中,字符占据相等的空间。

pubspec.yaml

google_fonts: ^2.1.0

sport_screen.dart

Text(
  "${f.format(_minutes)}:${f.format(_seconds)}",
  textAlign: TextAlign.center,
  style: GoogleFonts.robotoMono(
    fontSize: 80.0,
    color: const Color(0xff7165E3),
  ),
)

请参阅here 了解有关字体和负空间的更多信息。

默认字体

等宽字体

【讨论】:

  • 嘿,看看我的其他评论。我已经显示了两位数,但文本小部件仍然会重新居中。
  • 哇!我认为这是问题所在,非常感谢!
猜你喜欢
  • 1970-01-01
  • 2019-03-05
  • 1970-01-01
  • 2019-09-16
  • 2013-09-12
  • 1970-01-01
  • 2021-03-23
  • 2020-10-01
  • 2014-06-01
相关资源
最近更新 更多