Flutter:CustomScrollView自定义滚动使用


CustomScrollView的使用场景:当需要构建复杂的滚动布局时,如上图,有分页,爆款商品是ListView

注意事项

haskell 复制代码
1、Widget需要进行转换
_buildBanner().sliverToBoxAdapter(),

2、不能直接使用paddingHorizontal,
可用sliverPaddingHorizontal()代替

3、所有组件放在
CustomScrollView(
	slivers: []
)中

上图代码

js 复制代码
// 爆款商品
Widget _buildFlashSell() {
  return <Widget>[
    SizedBox(height: 25.w,),
    // 标题
    <Widget>[
      TDImage(assetUrl: "assets/myimage/nav-11.png",width: 36.w,height: 36.w,),
      SizedBox(width: 7.w,),
      TextWidget.body('爆款商品',size: 28.sp,color: const Color(0xff181818),),
      SizedBox(width: 20.w,),
      TextWidget.body('品质卓越,超值享受,一切尽在我们的爆款产品! ',size: 20.sp,color: const Color(0xff999999),),
    ].toRow(
      crossAxisAlignment: CrossAxisAlignment.baseline,
      textBaseline: TextBaseline.alphabetic,
    ).paddingHorizontal(30.w),
    SizedBox(height: 30.w,),

    // 爆款商品可左右滑动查看
    <Widget>[
      SizedBox(width: 30.w,),
      for (var i = 0; i < 5; i++)
      <Widget>[
        TDImage(assetUrl: "assets/myimage/goods.png",width: 200.w,height: 160.w,),
        SizedBox(height: 5.w,),
        TextWidget.body(
          '爆款商品爆款商品爆款商品',
          size: 28.sp,
          color: const Color(0xff181818),
          overflow: TextOverflow.ellipsis,
          maxLines: 1,
        ),
        SizedBox(height: 5.w,),
        <Widget>[
          TextWidget.body('¥',size: 24.sp,color: const Color(0xffFF6E00),),
          TextWidget.body('599.00',size: 28.sp,color: const Color(0xffFF6E00),),
        ].toRow(),
        Text('原价¥599.00',
          style: TextStyle(
            fontSize: 24.sp,
            color: const Color(0xff999999),
            decoration: TextDecoration.lineThrough,
            decorationColor: const Color(0xff999999)
          ),
        ),
      ].toColumn(crossAxisAlignment: CrossAxisAlignment.start).width(200.w).marginOnly(right: 15.w),
      SizedBox(width: 15.w,),
    ].toListView(scrollDirection: Axis.horizontal,).expanded(),

  ].toColumn()
  .decorated(border: Border.all(width: 1, color: const Color(0xffEDF1F2)),borderRadius: BorderRadius.circular(20.w))
  .tight(height: 390.w);
}
  
// 商品列表
Widget _buildGoodsList() {
  return SliverGrid(
    delegate: SliverChildBuilderDelegate(
      (BuildContext context, int position) {
        // var product = controller.goodsList[position];
        return <Widget>[
          TDImage(assetUrl: "assets/myimage/goods.png",width: 300.w,height: 300.w,),
          <Widget>[
            TextWidget.body('爆款商品爆款商品爆款商品爆款商品爆款商品爆款商品商品',size: 28.sp,color: const Color(0xff181818),overflow: TextOverflow.ellipsis),
            SizedBox(height: 10.w,),
            // 价格
            <Widget>[
              TextWidget.body('¥',size: 24.sp,color: const Color(0xffFF6E00),weight: FontWeight.bold,),
              TextWidget.body('599.00',size: 32.sp,color: const Color(0xffFF6E00),weight: FontWeight.bold,),
            ].toRow(),
            <Widget>[
              Text('原价¥599.00',
                style: TextStyle(
                  fontSize: 24.sp,
                  color: const Color(0xff999999),
                  decoration: TextDecoration.lineThrough,
                  decorationColor: const Color(0xff999999)
                ),
              ),
              TextWidget.body('销量:100',size: 24.sp,color: const Color(0xff999999),),
            ].toRow(mainAxisAlignment: MainAxisAlignment.spaceBetween),
          ].toColumn().paddingOnly(left: 20.w,right: 20.w,top: 20.w,)

        ].toColumn()
        .paddingTop(20.w)
        .decorated(border: Border.all(width: 1, color: const Color(0xffEDF1F2)),borderRadius: BorderRadius.circular(20.w));
      },
      childCount: controller.goodsList.length,
    ),
    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
      crossAxisCount: 2,
      mainAxisSpacing: AppSpace.listRow,
      crossAxisSpacing: AppSpace.listItem,
      childAspectRatio: 0.7,
    ),
  );
}

// 主视图
Widget _buildView() {
  return CustomScrollView(
    slivers: [
      _buildBanner().sliverToBoxAdapter(),
      _buildSearch().sliverToBoxAdapter().sliverPaddingHorizontal(30.w),
      SizedBox(height: 30.w,).sliverToBoxAdapter(),
      _buildCategory().sliverToBoxAdapter().sliverPaddingHorizontal(30.w),
      SizedBox(height: 40.w,).sliverToBoxAdapter(),
      _buildNotice().sliverToBoxAdapter().sliverPaddingHorizontal(30.w),
      SizedBox(height: 30.w,).sliverToBoxAdapter(),
      _buildFlashSell().sliverToBoxAdapter().sliverPaddingHorizontal(30.w),
      SizedBox(height: 30.w,).sliverToBoxAdapter(),
      _buildGoodsList().sliverPaddingHorizontal(30.w),
    ],
  );
}

@override
Widget build(BuildContext context) {
  return GetBuilder<HomeController>(
    init: HomeController(),
    id: "home",
    builder: (_) {
      return Scaffold(
        // appBar: AppBar(title: const Text("card")),
        backgroundColor: Colors.white,
        body: SmartRefresher(
          controller: controller.refreshController,
          enablePullUp: true, // 启用上拉加载
          onRefresh: controller.onRefresh, // 下拉刷新回调
          onLoading: controller.onLoading, // 上拉加载回调
          footer: const SmartRefresherFooterWidget(), // 底部加载更多组件
          child: _buildView(),
        ),
      );
    },
  );
}
相关推荐
Luke Ewin18 小时前
FunASR的Java实现Paraformer实时语音识别 | 一款无需联网的本地实时字幕软件
java·人工智能·语音识别·asr·funasr·paraformer·sensevoice
叫我阿柒啊18 小时前
从Java全栈到前端框架的全面实战:一次真实面试的深度解析
java·spring boot·缓存·微服务·消息队列·vue3·rest api
望未来无悔18 小时前
系统学习算法 专题十八 队列+宽搜
java·算法
前行的小黑炭18 小时前
Android 不同构建模式下使用不同类的例子:如何在debug模式和release模式,让其使用不同的类呢?
android·kotlin·gradle
Linlichaoblms18 小时前
Nginx性能调优:参数详解与压测对比
java·spring boot·nginx
一个很老的小萌新18 小时前
json 解析 [{“id“:1,“name“:“apple“},{“id“:2,“name“:“banana“}]
java·前端·json
andyguo19 小时前
AI模型测评平台工程化实战十二讲(第一讲:从手工测试到系统化的觉醒)
android
2501_9159214319 小时前
小团队如何高效完成 uni-app iOS 上架,从分工到工具组合的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
是2的10次方啊19 小时前
🏗️ 线程池深度解析:ThreadPoolExecutor底层实现与CompletableFuture异步编程实战
java
小蒜学长19 小时前
django全国小米su7的行情查询系统(代码+数据库+LW)
java·数据库·spring boot·后端