flutter列表视图分离

ql3eal8s  于 2023-02-16  发布在  Flutter
关注(0)|答案(1)|浏览(133)

我试着在一个小部件上使用listViewSeparated沿着一行滚动,但它不显示任何东西,像它只有一个脚手架(白色页)我不知道是什么问题这里是我的代码

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../layout/cubit/cubit.dart';
import '../../layout/cubit/states.dart';

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocConsumer<LifeStyleCubit, LifeStates>(
      listener: (context, state) {},
      builder: (context, state) {
        return Scaffold(
          body: Padding(
            padding: const EdgeInsets.all(8.0),
            child: SingleChildScrollView(
              child: Column(
                children: [
                  SingleChildScrollView(
                    scrollDirection: Axis.horizontal,
                    child: Row(
                      children: [
                        ListView.separated(
                          shrinkWrap: true,
                            itemBuilder: (context,index) =>SpecialProgramWidget(),
                            separatorBuilder: (context,index) => SizedBox(width: 20,),
                            itemCount: 3
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  Widget SpecialProgramWidget() => Container(
        width: 310,
        height: 300,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10),
        ),
        child: Column(
          children: [
            Stack(
              alignment: Alignment.bottomRight,
              children: [
                Container(
                  height: 230,
                  width: 310,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(10),
                          topRight: Radius.circular(10)),
                      image: DecorationImage(
                          image: NetworkImage(
                              'https://i.pinimg.com/564x/20/bd/68/20bd68b5a248bb191178bd9b98b53a76.jpg'),
                          fit: BoxFit.cover)),
                ),
                Align(
                  alignment: Alignment.topLeft,
                  child: Container(
                    height: 230,
                    width: 290,
                    decoration: BoxDecoration(
                        image: DecorationImage(
                            image: NetworkImage(
                                'https://www.pngplay.com/wp-content/uploads/6/Fitness-PNG-HD-Quality.png'),
                            fit: BoxFit.cover)),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(
                    'Special Program',
                    style: TextStyle(fontSize: 21, color: Colors.white),
                  ),
                )
              ],
            ),
            Container(
              width: 310,
              height: 70,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.only(
                  bottomLeft: Radius.circular(10),
                  bottomRight: Radius.circular(10),
                ),
                border: Border.all(color: Colors.grey.shade400, width: 1),
              ),
              child: Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Align(
                        alignment: Alignment.topLeft,
                        child: Text(
                          'Super pump biceps workout',
                          style: TextStyle(
                            fontSize: 15,
                          ),
                        )),
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 8),
                    child: Align(
                        alignment: Alignment.topLeft,
                        child: Text(
                          '6 Weeks',
                          style: TextStyle(fontSize: 15, color: Colors.grey),
                        )),
                  ),
                ],
              ),
            )
          ],
        ),
      );
}

我需要我的屏幕显示像这样

return Scaffold(
      body: Column(
        children: [
          SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Row(
              children: [
                SpecialProgramWidget(),
                SizedBox(width: 20,),
                SpecialProgramWidget(),
                SizedBox(width: 20,),
                SpecialProgramWidget()
              ],
            ),
          ),
        ],
      )
       );

它沿着一行显示小部件...我想用ListViewSeparated做同样的事情,但我只得到了没有数据的白色页面
我尝试了一些解决方案,如 Package 扩展的小部件上的ListViewSeparated,但仍然不工作,我该如何解决这个问题?

j5fpnvbx

j5fpnvbx1#

问题出在scrollDirection: Axis.horizontal,上。另外,您不需要在SingleChildScrollView中使用ListView.separated(,父级已经在处理滚动事件。
您可以通过ListView.separated提供fixed来绕过此问题,

Widget build(BuildContext context) {
  // return
  // BlocConsumer<LifeStyleCubit, LifeStates>(
  //   listener: (context, state) {},
  //   builder: (context, state) {
  return Scaffold(
    body: LayoutBuilder(
      builder: (_, constraints) => Padding(
        padding: const EdgeInsets.all(8.0),
        child: SingleChildScrollView(
          child: Column(
            children: [
              SingleChildScrollView(
                scrollDirection: Axis.horizontal,
                child: Row(
                  children: [
                    SizedBox(
                      width: constraints.maxWidth,
                      child: ListView.separated(
                        shrinkWrap: true,
                        itemBuilder: (context, index) =>
                            SpecialProgramWidget(),
                        separatorBuilder: (context, index) =>
                            SizedBox(width: 20),
                        itemCount: 3,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    ),
    // );
    // },
  );
}

一个更好的决定将是使用自定义滚动视图,我认为这将是更好的东西,你试图得到。

相关问题