flutter - 如何为容器使用两种不同的背景色

标签 flutter dart colors containers

我希望容器看起来像这样:
batterybackGround colorred,取决于设备的电池电量。我希望它是动态的。
我这样尝试过:

return Container(
      child: Row(
        children: [
          Expanded(flex: 100-battery,child: SizedBox()),
          Expanded(flex: battery,child: Container(child: Text(battery.toString()+'%', style: TextStyle(fontSize: 10,),),color: Colors.green)),
        ],
      ),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(5), border: Border.all(color: Colors.white)),
    );
看起来不错,问题在于Text对齐到一侧。我希望它在一侧。所以我必须指定两个background color来解决这个问题。任何的想法?

最佳答案

使用gradientBoxDecoration参数
您可以使用三个渐变之一

  • 线性渐变
  • RadialGradient
  • SweepGradient

  • 以这个为例
     Container(
          decoration: BoxDecoration(
            gradient: RadialGradient(
              center: Alignment.center,
              colors: [const Color(0xFF283250), const Color(0xFF0C1225)],
              tileMode: TileMode.clamp,
            ),
            borderRadius: BorderRadius.circular(5),
            border: Border.all(color: Colors.white),
          ),
          child: Container(
            alignment: Alignment.center,
            child: Row(
              children: [
                Expanded(flex: 100 - battery, child: SizedBox()),
                Expanded(
                    flex: battery,
                    child: Container(
                        child: Text(
                          battery.toString() + '%',
                          style: TextStyle(
                            fontSize: 10,
                          ),
                        ),
                        color: Colors.green)),
              ],
            ),
          ),
        ),
    
    https://api.flutter.dev/flutter/painting/Gradient-class.html了解有关使用gradients的更多信息

    关于flutter - 如何为容器使用两种不同的背景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63435684/

    相关文章:

    colors - Web 颜色 - PC 上的浅灰色未显示

    html - CSS 背景图片和 div 样式滤色器

    java - Java 中有颜色操作库吗?

    dart - 在 Dart 中使用 AES ECB 加密二进制数组

    flutter - 什么?在 Dart 中做什么?

    列中的 Flutter 多交叉轴对齐

    flutter - 在同一页面中使用两个 BLoC 并在第二个 BLoC 中传递第一个 BLoC 的状态

    image - 如何在 Flutter 中重叠两个小部件?

    Flutter SVG 作为 ImageIcon

    flutter - Flutter在导航弹出后获得引用