【问题标题】:Flutter Stack's child is transparent while using linear gradientFlutter Stack的孩子在使用线性渐变时是透明的
【发布时间】:2020-11-20 10:32:54
【问题描述】:

我正在使用堆栈,其中节省 60% 的小部件位于具有白色背景的容器顶部。我在折扣容器上使用渐变。我希望渐变是纯色的,但正如您在图像中看到的那样,它是半透明的,我们可以看到它下面的白色背景。

我的堆栈代码是:

 Container(
      width: width * 0.38,
      height: 250,
      child: Stack(
        children: [
          Positioned(
            top: 20,
            child: InkWell(
              onTap: () {},
              child: Container(
                width: width * 0.38,
                height: 200,
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(20)),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    SizedBox(height: 35.h),
                    buildProductPrice(product, _intros),
                    buildCheckMark(product),
                  ],
                ),
              ),
            ),
          ),
          Align(
              alignment: Alignment.topCenter,
              child: buildDiscountText(product, _intros)),
        ],
      ),
    );

buildDiscountText 小部件:

       return Container(
        height: 40,
        width: 100,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            gradient: LinearGradient(
              colors: [
                Colors.lightBlue.withOpacity(0.8),
                Color(0xffCE41FD),
              ],
              end: Alignment.centerRight,
              begin: Alignment.centerLeft,
            ),
            borderRadius: BorderRadius.circular(20)),
        child: Text(
          'SAVE $_rounded %',
          style: _saveTextStyle,
        ),
      );      

为了获得坚实的背景,我可以用 Container 包装 buildDiscountTextWidget 并提供相同的渐变并执行 2.3 次,但我认为这不是正确的方法。

here is the screen shot of the widget

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    你只需要从Colors.lightBlue.withOpacity(0.8)的末尾删除.withOpacity(0.8)

    【讨论】:

      【解决方案2】:

      如下更改 buildDiscountText 小部件:

      return Container(
          height: 40,
          width: 100,
          alignment: Alignment.center,
          decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [
                  Colors.lightBlue, //remove opacity
                  Color(0xffCE41FD),
                ],
                end: Alignment.centerRight,
                begin: Alignment.centerLeft,
              ),
              borderRadius: BorderRadius.circular(20)),
          child: Text(
            'SAVE $_rounded %',
            style: _saveTextStyle,
          ),
        );  
      

      【讨论】:

      • 我添加了不透明度以获得所需的渐变。我没有意识到它会导致透明度。
      • 那么你得到满意的结果了吗?
      猜你喜欢
      • 1970-01-01
      • 2011-12-05
      • 2011-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 2023-02-02
      • 1970-01-01
      相关资源
      最近更新 更多