代码之家  ›  专栏  ›  技术社区  ›  shawon191

颤振线性推进指示器相对高度

  •  0
  • shawon191  · 技术社区  · 5 年前

    我试图在一个容器中显示一个进度条,其中有几个项目。问题是我无法找到一种方法使进度条的高度与容器的高度相同(我不想使容器的高度固定)。为数不多的例子 LinearProgressIndicator 我找到了所有的用途 SizedBox 设置一个固定的高度。如何设置进度条相对于容器的高度?

    class MyWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Stack(
          children: [
            LinearProgressIndicator(
              value: 0.5,
              valueColor: AlwaysStoppedAnimation<Color>(Color(0x88888888)),
            ),
            Container(
              child: Column(children: [
                  Text(
                    "Title",
                    textScaleFactor: 1.2,
                    style: TextStyle(color: Colors.white38),
                  ),
                  Text(
                    "Content",
                    textScaleFactor: 2,
                    style: TextStyle(color: Colors.white),
                  )
                ])
            )
          ],
        );
      }
    }
    

    DartPad链接- https://dartpad.dev/be0ae9c94833d27134256763136cfca1

    1 回复  |  直到 5 年前
        1
  •  1
  •   Thierry    5 年前

    解决方案1

    使用 fit: StackFit.expand Stack :

    class MyWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Stack(
          fit: StackFit.expand,
          children: [
            LinearProgressIndicator(
              value: 0.5,
              valueColor: AlwaysStoppedAnimation<Color>(Color(0x88888888)),
            ),
            Container(
              child: Column(children: [
                  Text(
                    "Title",
                    textScaleFactor: 1.2,
                    style: TextStyle(color: Colors.white38),
                  ),
                  Text(
                    "Content",
                    textScaleFactor: 2,
                    style: TextStyle(color: Colors.white),
                  )
                ])
            )
          ],
        );
      }
    }
    

    解决方案2

    Positioned.fill LinearProgressIndicator :

    class MyWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Stack(
          children: [
            Positioned.fill(
              child: LinearProgressIndicator(
                value: 0.5,
                valueColor: AlwaysStoppedAnimation<Color>(Color(0x88888888)),
              ),
            ),
            Container(
              child: Column(children: [
                  Text(
                    "Title",
                    textScaleFactor: 1.2,
                    style: TextStyle(color: Colors.white38),
                  ),
                  Text(
                    "Content",
                    textScaleFactor: 2,
                    style: TextStyle(color: Colors.white),
                  )
                ])
            )
          ],
        );
      }
    }