flutter实现付费解锁内容的遮挡

dart 复制代码
import 'package:configs/PathConfig.dart';
import 'package:utils/JadeColors.dart';
import 'package:utils/Utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ContentMask extends StatelessWidget{
  final String text;//文本字符串
  final double percent;//显示的百分比
  const ContentMask({super.key,required this.text,this.percent = 1.0});

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Column(
      children: [
        Text(getFirstPercentStr()),
        Container(
          padding: EdgeInsets.only(top: 60.w,bottom: 40.w),
          decoration: BoxDecoration(
            color: Colors.white,
            boxShadow: [
              BoxShadow(
                color: JadeColors.white.withValues(alpha: 0.9), // 阴影颜色(半透明)
                offset: const Offset(0, -5), // 向上偏移5px(y轴负数)
                blurRadius: 10, // 模糊半径(阴影扩散范围)
                spreadRadius: 10, // 扩散半径,这个参数可以让阴影扩散到上面的Text文本上(阴影基础大小)
                blurStyle: BlurStyle.normal, // 模糊样式
              ),
            ],
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Image.asset(PathConfig.iconShieldGold,width: 52.w),
                  Text('真实性保障,不实即退!',style: TextStyle(fontSize: 30.sp,fontWeight: FontWeight.bold))
                ],
              ),
              Text('解锁后您将获得:',style: TextStyle(fontSize: 26.sp,height: 3)),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  Column(
                    children: [
                      Image.asset(PathConfig.iconSelectedGold,width: 40.w),
                      Text('具体的操作细节',style: TextStyle(fontSize: 26.sp,height: 2))
                    ],
                  ),
                  Column(
                    children: [
                      Image.asset(PathConfig.iconSelectedGold,width: 40.w),
                      Text('隐藏的优惠券口令',style: TextStyle(fontSize: 26.sp,height: 2))
                    ],
                  ),
                  Column(
                    children: [
                      Image.asset(PathConfig.iconSelectedGold,width: 40.w),
                      Text('具体店铺地址',style: TextStyle(fontSize: 26.sp,height: 2))
                    ],
                  )
                ],
              ),
              GestureDetector(
                onTap: (){},
                child: Container(
                  color: JadeColors.orange_14,
                  padding: EdgeInsets.symmetric(horizontal: 50.w,vertical: 20.w),
                  margin: EdgeInsets.only(top: 40.w,bottom: 40.w),
                  child: Text('仅需 ¥ 5.00,即可解锁。',style: TextStyle(color: Colors.white,fontSize: 26.sp),),
                ),
              ),
              Text.rich(
                  TextSpan(
                      text: '已有',
                      style: TextStyle(color: JadeColors.grey,fontSize: 24.sp),
                      children: [
                        TextSpan(
                          text: '485',
                          style: TextStyle(color: JadeColors.grey_2,fontSize: 24.sp),
                        ),
                        TextSpan(
                          text: '人解锁',
                          style: TextStyle(color: JadeColors.grey,fontSize: 24.sp),
                        )
                      ]
                  )
              ),
              Container(
                margin: EdgeInsets.only(top: 30.w),
                height: 80.w,
                child: ListView.builder(
                    itemCount: 6,
                    shrinkWrap: true,
                    scrollDirection: Axis.horizontal,
                    itemBuilder: (_,index){
                      return index == 5
                          ? Image.asset(PathConfig.iconThreeDotOrange,width: 75.w)
                          : Container(
                        color: Colors.transparent,
                        margin: EdgeInsets.only(right: 10.w),
                        child: Stack(
                          alignment: Alignment.topRight,
                          children: [
                            Padding(padding: EdgeInsets.only(right: 16.w,top: 4.w),
                                child: Utils().roundedImage(PathConfig.testImageUrl[3], 60,width: 75.w,height: 75.w)),
                            Container(
                              padding: EdgeInsets.symmetric(horizontal: 6.w,vertical: 2.w),
                              decoration: BoxDecoration(
                                  color: JadeColors.gold_11,
                                  border: Border.all(color: JadeColors.gold_12,width: 1),
                                  borderRadius: BorderRadius.circular(10)
                              ),
                              child: Text('已解锁',style: TextStyle(color: JadeColors.gold_12,fontSize: 12.sp)),
                            )
                          ],
                        ),
                      );
                    }),
              )
            ],
          ),
        )
      ],
    );
  }

  String getFirstPercentStr() {
    final chars = text.characters;
    int endIndex = (chars.length * percent).floor().clamp(1, chars.length);
    return chars.take(endIndex).toString();
  }
}

使用

dart 复制代码
bool _isFree = false; //是否免费
  //内容
  _contentView(){
    return _isFree ? Column(
      children: [
        Container(
          padding: EdgeInsets.only(top: 20.w,left: 20.w,right: 20.w),
          child: Text(_testContext, style: TextStyle(fontSize: 30.sp),),
        ),
        _addressView(),
      ],
    ):ContentMask(text: _testContext,percent: 0.4);

  }
相关推荐
里欧跑得慢7 小时前
15. Web可访问性最佳实践:让每个用户都能平等访问
前端·css·flutter·web
Lanren的编程日记10 小时前
Flutter 鸿蒙应用数据版本管理实战:版本记录+版本回退+版本对比,实现全链路数据版本控制
flutter·华为·harmonyos
饭小猿人13 小时前
Android 腾讯X5WebView如何禁止系统自带剪切板和自定义剪切板视图
android·java
_李小白13 小时前
【android opencv学习笔记】Day 8: remap(像素位置重映射)
android·opencv·学习
美狐美颜SDK开放平台13 小时前
多场景美颜SDK解决方案:直播APP(iOS/安卓)开发接入详解
android·人工智能·ios·音视频·美颜sdk·第三方美颜sdk·短视频美颜sdk
嗷o嗷o14 小时前
Android BLE 里,MTU、分包和长数据发送到底该怎么处理
android
Gary Studio15 小时前
Android AIDL HAL工程结构示例
android
y = xⁿ16 小时前
MySQL八股知识合集
android·mysql·adb
MonkeyKing16 小时前
Flutter列表性能极致优化:从卡顿到丝滑
flutter·dart
IntMainJhy16 小时前
「Flutter三方库sqflite的鸿蒙化适配与实战指南:从入门到踩坑的本地数据库开发全记录」
数据库·flutter·华为·信息可视化·数据库开发·harmonyos