Flutter-文本insideLinearProgressIndicator

问题描述 投票:0回答:2

在Flutter中,我有LinearProgressIndicator。可以,但是我想在此栏内添加文本(进度)。

我尝试这样的事情

 SafeArea(
                    child: Row(children: <Widget>[
                  Center(
                      child: Text(DateFormat.ms()
                          .format(DateTime.fromMillisecondsSinceEpoch(
                              minuteSecond * 1000))
                          .toString())),
                  Expanded(
                      child: LinearProgressIndicator(
                          semanticsLabel: (animation.value * 100.0).toStringAsFixed(1).toString(),                
                          semanticsValue:  (animation.value * 100.0).toStringAsFixed(1).toString(),   
                          value: animation.value,
                          backgroundColor: Colors.white,
                          valueColor:
                              AlwaysStoppedAnimation<Color>(Colors.red))),
                ])),

但是很明显,这会将文本添加到栏的同一行中,而不是内部。

所以:有人知道如何在此条形图内添加中心(居中)?使用stack元素?

flutter flutter-layout
2个回答
0
投票

使用它,我增加了进度指示器的高度,因为文本不适合。

       Stack(
          children: <Widget>[
            SizedBox(
              height: 18,
              child: LinearProgressIndicator(
                valueColor: AlwaysStoppedAnimation<Color>(Theme.of(context).primaryColor),
                backgroundColor: Theme.of(context).primaryColorLight,
              ),
            ),
            Positioned(
              child: Center(
                child: Text('Some Text'),
              ),
            ),
          ],
        ),

0
投票

您必须使用Stack,请尝试以下操作:

为简单起见,我将0.6值硬编码,可以在其中使用animation.value

Stack(
  children: <Widget>[
    SizedBox(
      height: 20,
      child: LinearProgressIndicator(
        value: 0.6,
        backgroundColor: Colors.white,
        valueColor: AlwaysStoppedAnimation<Color>(Colors.red),
      ),
    ),
    Align(child: Text("Text"), alignment: Alignment.topCenter,),
  ],
)

屏幕截图:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.