简体   繁体   English

flutter 水平列表视图不起作用

[英]flutter horizontal list view is not working

i hope I can make my point clear..我希望我能把我的观点说清楚..

This is my code in the HomePage, where I want to show the HorizontalListView.这是我在主页中的代码,我想在其中显示 HorizontalListView。

body: ListView(
    children: [
      imageCarousel,
      const Padding(
        padding: EdgeInsets.all(8.0),
        child: Text("CATEGORIES"),
      ),
       const HorizontalListView(),

that is how I put the code in the homepage, while runing the codes on the HorizontalListView the emulator only shows a white screen which means the codes are not working.这就是我将代码放在主页中的方式,而在 HorizontalListView 上运行代码时,模拟器只显示白屏,这意味着代码不起作用。 in-order to look at the issue, here is the codes of HorizontalListView page.为了看问题,这里是 HorizontalListView 页面的代码。

Container(
          height: 100,
          child: ListView(
            scrollDirection: Axis.horizontal,
            children: const [
              Category(
                image_location: 'assets/images/c2.png',
                image_caption: 'Drinks',
              ),
              Category(
                image_location: 'assets/images/c3.png', image_caption: 'Tea',
              ),
              Category(
                image_location: 'assets/images/c4.png', image_caption: 'Vgetable',
              ),
    class Category extends StatelessWidget {
      final String image_location;
      final String image_caption;
    
      const Category({required this.image_caption, required this.image_location});
      @override
      Widget build(BuildContext context) {
        return Padding(
          padding:const EdgeInsets.all(2.0),
        child: InkWell(
          onTap: (){},
        child: Container(
          height: 100,
          child: ListTile(
            title: Image.asset(image_location,
            width: 100,
              height: 80,
            ),
          subtitle: Container(
          alignment: Alignment.topCenter,
          child:Text(image_caption, style: const TextStyle(fontWeight: FontWeight.bold)),
          ),

      

and please don't worry much about the brackets i just delete some because the rules of the site.请不要太担心括号我只是删除一些因为网站的规则。

I'll appreciate any HELP.我会很感激任何帮助。 Thanks.谢谢。

Actually i cannot read your trouble in your code, because some code in your post is missing, like which widget wrap your container listview .实际上,我无法在您的代码中阅读您的问题,因为您帖子中的某些代码丢失了,例如哪个小部件包装了您的容器 listview But here i have some suggestion for you and you can try it:但在这里我有一些建议给你,你可以试试:

wrap your container list view with row.用行包装你的容器列表视图。

I have solved my problem with it in few days ago几天前我已经解决了我的问题

I check your code it creates an issue I wrap the widget into singlechildscrollView as a parent widget and call our list into a row widget. You can try out this solution it works for my side thanks
      
    import 'package:flutter/material.dart';  
        class FirstScreen extends StatefulWidget {
          const FirstScreen({Key? key}) : super(key: key);
        
          @override
          State<FirstScreen> createState() => _FirstScreenState();
        }
        
        class _FirstScreenState extends State<FirstScreen> {
          @override
          Widget build(BuildContext context) {
            return Scaffold(
              body:SingleChildScrollView(
              scrollDirection: Axis.horizontal,
              child: Row(
              children: const [
              Category(
              image_location: 'assets/MasterCard.png',
              image_caption: 'Drinks',
            ),
            Category(
            image_location: 'assets/Traveling.png', image_caption: 'Tea',
            ),
            Category(
            image_location: 'assets/Visa.png', image_caption: 'Vgetable',
            ),
            ])));
          }
        }
        
        class Category extends StatelessWidget {
          final String image_location;
          final String image_caption;
        
          const Category({required this.image_caption, required this.image_location});
          @override
          Widget build(BuildContext context) {
            return Padding(
                padding:const EdgeInsets.all(2.0),
                child: InkWell(
                onTap: (){},
            child: Container(
            height: 100,
            width: 300,
            child: ListTile(
            title: Image.asset(image_location,
            width: 100,
            height: 80,
            ),
            subtitle: Container(
            alignment: Alignment.topCenter,
            child:Text(image_caption, style: const TextStyle(fontWeight: FontWeight.bold)),
            ),
            )
            )
                )
            );
          }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM