flutter 如何给GridView.Builder的子对象给予高度

vi4fp9gy  于 2022-12-30  发布在  Flutter
关注(0)|答案(9)|浏览(683)

我特灵给予gridview.builder的孩子身高,但它不接受。我尝试使用容器,但它不工作...请帮助

GridView.builder(
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2,
              ),
              itemCount: snapshot.data.length,
              itemBuilder: (BuildContext context, int index) {
                return Container(
                  height: 280.0,
                  child: Column(
                    children: <Widget>[
                      Padding(
                        padding:
                            EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 10.0),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Container(
                          height: 208.5,
                          width: 138.75,
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(10.0),
                              image: DecorationImage(
                                  image: NetworkImage(
                                      "${snapshot.data[index].url}"),
                                  fit: BoxFit.fill)),
                        ),
                      ),
                      Text(
                        snapshot.data[index].title,
                        style: TextStyle(fontSize: 17.0),
                      ),
                    ],
                  ),
                );
              },
            ),`

unguejic

unguejic1#

最好将SliverGridDelegatechildAspectRatio属性与MediaQuery一起使用:

class HomePage extends StatelessWidget {
  final List<String> items = <String>[
    "Item 1",
    "Item 2",
    "Item 3",
    "Item 4",
    "Item 5",
    "Item 6",
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Home')),
      body: GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          childAspectRatio: MediaQuery.of(context).size.width /
              (MediaQuery.of(context).size.height / 4),
        ),
        itemCount: items.length,
        itemBuilder: (context, index) {
          return GridTile(child: Text(items[index]));
        },
      ),
    );
  }
}
wko9yo5t

wko9yo5t2#

您可以使用mainAxisExtent代替childAspectRatio

GridView.builder(
            physics: BouncingScrollPhysics(),
            itemCount: resumes.length,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              mainAxisExtent: 256,
            ),
            itemBuilder: (_, index) => Container(),
          ),
dl5txlt9

dl5txlt93#

通过将childAspectRatio 0更改为1,可以更改项目的高度

ntjbwcob

ntjbwcob4#

为了使容器更大,长宽比应该小于1但大于0,例如0.4

g2ieeal7

g2ieeal75#

删除子AspectRatio并使用此mainAxisExtent:256,

uinbv5nw

uinbv5nw6#

试试flutter_staggered_grid_view软件包
与自然的GridView.builder不同,MasonryGridView允许根据子对象的内容大小来调整其高度。

MasonryGridView.count(
                      mainAxisSpacing: 10,
                      crossAxisSpacing: 10,
                      crossAxisCount: 2,
                    
                      itemCount: 
                      snapshot.data.length,
                      itemBuilder: (BuildContext context, int index) {
                 return Container(
                   height: 280.0,
                   child: Column(
                   children: <Widget>[
                    Padding(
                      padding:
                        EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 10.0),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Container(
                      height: 208.5,
                      width: 138.75,
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(10.0),
                          image: DecorationImage(
                              image: NetworkImage(
                                  "${snapshot.data[index].url}"),
                              fit: BoxFit.fill)),
                    ),
                  ),
                  Text(
                    snapshot.data[index].title,
                    style: TextStyle(fontSize: 17.0),
                  ),
                ],
              ),
            );},)
zed5wv10

zed5wv107#

import 'package:flutter/material.dart';                 

class AllCategories extends StatelessWidget {             
  AllCategories({Key? key}) : super(key: key);          
  
 

  @override
  Widget build(BuildContext context) {                    
    return Scaffold(                     
      backgroundColor: Colors.white,              
      appBar: AppBar(              
        backgroundColor: Colors.white,                
        title: Text('All Categories'),                
        leading: IconButton(              
          icon: Icon(                 
            EvaIcons.arrowIosBack,           
            color: Colors.black,
          ),              
          onPressed: () {
            Navigator.pop(context);            
          },
        ),
      ),                 
      body: GridView.builder(                
          shrinkWrap: true,                    
          itemCount: 6,                   
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(                  
              crossAxisCount: 2, childAspectRatio: 2 / 3),                       
          itemBuilder: (context, index) {                            
            return Container(              
                margin: EdgeInsets.all(8),                         
                decoration: BoxDecoration(                       
                  borderRadius: BorderRadius.circular(2),                       
                ),                           
                child: Column(                          
                  crossAxisAlignment: CrossAxisAlignment.start,                              
                  children: [                          
                    Flexible(            
                      child: Container(                 
                        decoration: BoxDecoration(         
                            image: DecorationImage(           
                                fit: BoxFit.cover,                    
                                image: NetworkImage(             
                                    'https://t3.ftcdn.net/jpg/03/15/59/88/360_F_315598844_WbT1Ix5HL17KN6sDzTBhu1zE4nb7Ry3o.jpg'))),                       
                      ),             
                    ),                       
                    Padding(                      
                      padding: const EdgeInsets.fromLTRB(8, 10, 0, 0),
                      child: Text(            
                        'Nike',                    
                        style: TextStyle(                                
                            fontSize: 18, fontWeight: FontWeight.w900),      
                      ),
                    ),                       
                    Padding(             
                      padding: const EdgeInsets.all(8.0),               
                      child: Text("Just Do It",            
                          style: TextStyle(fontSize: 15, color:  
    Colors.grey)),
                    )    
                  ],               
                ));                      
          }),                        
    );              
  }            
}
mefy6pfw

mefy6pfw8#

我的解决方法是先使用Wrap,然后使用list.map()列出子列表中的数据,这样还可以更好地控制列表的显示方式。

class MoviesList extends StatelessWidget {
  const MoviesList({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
            child: SingleChildScrollView(
                padding: const EdgeInsets.symmetric(vertical: 20),
                child: SizedBox(
                    width: MediaQuery.of(context).size.width,
                    child: Wrap(
                        alignment: WrapAlignment.spaceEvenly,
                        spacing: 10,
                        runSpacing: 10,
                        direction: Axis.horizontal,
                        children: _data.map((item) {
                          return Column(
                            children: [
                              Container(
                                height: MediaQuery.of(context).size.width * 0.6,
                                width: MediaQuery.of(context).size.width * 0.4,
                                decoration: BoxDecoration(
                                    borderRadius: BorderRadius.circular(14),
                                    image: DecorationImage(
                                        fit: BoxFit.fitHeight,
                                        image: NetworkImage(item['Poster']))),
                              ),
                              Text(item['Title'])
                            ],
                          );
                        }).toList())))));
  }
}

List _data = [
  {
    "Title": "Batman",
    "Poster":
        "https://images-na.ssl-images-amazon.com/images/M/MV5BMTYwNjAyODIyMF5BMl5BanBnXkFtZTYwNDMwMDk2._V1_.jpg"
  },
  {
    "Title": "The Dark Knight",
    "Poster":
        "https://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_UX182_CR0,0,182,268_AL_.jpg"
  },
  {
    "Title": "Dawn of Justice",
    "Poster":
        "https://ia.media-imdb.com/images/M/MV5BYThjYzcyYzItNTVjNy00NDk0LTgwMWQtYjMwNmNlNWJhMzMyXkEyXkFqcGdeQXVyMTQxNzMzNDI@._V1_UX182_CR0,0,182,268_AL_.jpg"
  },
  {
    "Title": "The Dark Knight Rises",
    "Poster":
        "https://ia.media-imdb.com/images/M/MV5BMTk4ODQzNDY3Ml5BMl5BanBnXkFtZTcwODA0NTM4Nw@@._V1_UX182_CR0,0,182,268_AL_.jpg"
  },
  {
    "Title": "The LEGO Batman Movie",
    "Poster":
        "https://ia.media-imdb.com/images/M/MV5BMTcyNTEyOTY0M15BMl5BanBnXkFtZTgwOTAyNzU3MDI@._V1_UX182_CR0,0,182,268_AL_.jpg"
  },
  {
    "Title": "Batman & Robin",
    "Poster":
        "https://ia.media-imdb.com/images/M/MV5BMGQ5YTM1NmMtYmIxYy00N2VmLWJhZTYtN2EwYTY3MWFhOTczXkEyXkFqcGdeQXVyNTA2NTI0MTY@._V1_UX182_CR0,0,182,268_AL_.jpg"
  },
  {
    "Title": "Batman Returns",
    "Poster":
        "https://ia.media-imdb.com/images/M/MV5BOGZmYzVkMmItM2NiOS00MDI3LWI4ZWQtMTg0YWZkODRkMmViXkEyXkFqcGdeQXVyODY0NzcxNw@@._V1_UX182_CR0,0,182,268_AL_.jpg"
  }
];

yyhrrdl8

yyhrrdl89#

通过将shrinkWrap: true添加到View.builder进行修复。

相关问题