开发者社区 > 云原生 > 容器服务 > 正文

如果不固定父容器的高度,则GridView.Count不可见

我是新手,所以我找不到这个代码中的问题。所有的事情都很好,但是我尝试使用两行的Grid列表,当我给列表的父容器赋予高度时,这些行工作得很好,但是我想按照条目包装高度。

void main() {
  runApp(new MaterialApp(
    home: new MyHome(),
 ));
}

class MyHome extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

TextEditingController controller = new TextEditingController();

class _AppState extends State<MyHome> {
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: PreferredSize(
        preferredSize: Size(null, 180),
        child: CustomAppBar(_scaffoldKey, controller),
      ),
      drawer: createDrawer(),
      body: SingleChildScrollView(
        child: Container(
          color: Colors.black12,
      //=========Main Container For Scrollview==============//
          child: Padding(
            padding: const EdgeInsets.fromLTRB(0, 15, 0, 0),
            child: Column(
              children: <Widget>[
                Container(
              //================Container for Categories==================//
                  color: Colors.white,
                  child: Padding(
                    padding: const EdgeInsets.fromLTRB(15, 10, 15, 10),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Column(
                          children: <Widget>[
                        CircleAvatar(
                          backgroundImage:
                              ExactAssetImage('images/user_icon.png'),
                          minRadius: 20,
                          maxRadius: 30,
                        ),
                        Text(
                          'Women',
                          style: TextStyle(
                              fontSize: 13,
                              color: Colors.black,
                              fontFamily: 'SFProRegular'),
                        )
                      ],
                    ),
                    Column(
                      children: <Widget>[
                        CircleAvatar(
                          backgroundImage:
                              ExactAssetImage('images/user_icon.png'),
                          minRadius: 20,
                          maxRadius: 30,
                        ),
                        Text(
                          'Women',
                          style: TextStyle(
                              fontSize: 13,
                              color: Colors.black,
                              fontFamily: 'SFProRegular'),
                        )
                      ],
                    ),
                    Column(
                      children: <Widget>[
                        CircleAvatar(
                          backgroundImage:
                              ExactAssetImage('images/user_icon.png'),
                          minRadius: 20,
                          maxRadius: 30,
                        ),
                        Text(
                          'Women',
                          style: TextStyle(
                              fontSize: 13,
                              color: Colors.black,
                              fontFamily: 'SFProRegular'),
                        )
                      ],
                    ),
                    Column(
                      children: <Widget>[
                        CircleAvatar(
                          backgroundImage:
                              ExactAssetImage('images/user_icon.png'),
                          minRadius: 20,
                          maxRadius: 30,
                        ),
                        Text(
                          'Women',
                          style: TextStyle(
                              fontSize: 13,
                              color: Colors.black,
                              fontFamily: 'SFProRegular'),
                        )
                      ],
                    ),
                  ],
                ),
              ),
            ),
            Card(
              child: SizedBox(
                  height: 200.0,
                  child: Carousel(
                    images: [
                      NetworkImage(
                          'https://cdn-images-1.medium.com/max/2000/1*GqdzzfB_BHorv7V2NV7Jgg.jpeg'),
                      NetworkImage(
                          'https://cdn-images-1.medium.com/max/2000/1*wnIEgP1gNMrK5gZU7QS0-A.jpeg'),
                    ],
                    dotSize: 4.0,
                    dotSpacing: 15.0,
                    indicatorBgPadding: 5.0,
                    borderRadius: false,
                  )),
            ),

//=//。

GridView.count(     
              childAspectRatio: 4.0,
              // Create a grid with 2 columns. If you change the scrollDirection to
              // horizontal, this produces 2 rows.
              crossAxisCount: 2,
              // Generate 100 widgets that display their index in the List.
              children: List.generate(100, (index) {
                return Center(
                  child: Text(
                    'Item $index',
                    style: Theme.of(context).textTheme.headline,
                  ),
                );
              }),
            )
          ],
        ),
      ),
    ),
    ),
    );
  }
}

展开
收起
游客5akardh5cojhg 2019-12-25 15:58:41 2119 0
1 条回答
写回答
取消 提交回答
  • 因为你用SingleChildScrollView作为您的父部件GridView,所以您需要指定primary: false和shrinkWrap: true因此,GridView根据项目计数取最小的高度。

    完整代码:

    void main() {
      runApp(new MaterialApp(
        home: new MyHome(),
     ));
    }
    
    class MyHome extends StatefulWidget {
      @override
      _AppState createState() => _AppState();
    }
    
    TextEditingController controller = new TextEditingController();
    
    class _AppState extends State<MyHome> {
      final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          key: _scaffoldKey,
          appBar: PreferredSize(
            preferredSize: Size(null, 180),
            child: CustomAppBar(_scaffoldKey, controller),
          ),
          drawer: createDrawer(),
          body: SingleChildScrollView(
            child: Container(
              color: Colors.black12,
          //=========Main Container For Scrollview==============//
              child: Padding(
                padding: const EdgeInsets.fromLTRB(0, 15, 0, 0),
                child: Column(
                  children: <Widget>[
                    Container(
                  //================Container for Categories==================//
                      color: Colors.white,
                      child: Padding(
                        padding: const EdgeInsets.fromLTRB(15, 10, 15, 10),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Column(
                              children: <Widget>[
                            CircleAvatar(
                              backgroundImage:
                                  ExactAssetImage('images/user_icon.png'),
                              minRadius: 20,
                              maxRadius: 30,
                            ),
                            Text(
                              'Women',
                              style: TextStyle(
                                  fontSize: 13,
                                  color: Colors.black,
                                  fontFamily: 'SFProRegular'),
                            )
                          ],
                        ),
                        Column(
                          children: <Widget>[
                            CircleAvatar(
                              backgroundImage:
                                  ExactAssetImage('images/user_icon.png'),
                              minRadius: 20,
                              maxRadius: 30,
                            ),
                            Text(
                              'Women',
                              style: TextStyle(
                                  fontSize: 13,
                                  color: Colors.black,
                                  fontFamily: 'SFProRegular'),
                            )
                          ],
                        ),
                        Column(
                          children: <Widget>[
                            CircleAvatar(
                              backgroundImage:
                                  ExactAssetImage('images/user_icon.png'),
                              minRadius: 20,
                              maxRadius: 30,
                            ),
                            Text(
                              'Women',
                              style: TextStyle(
                                  fontSize: 13,
                                  color: Colors.black,
                                  fontFamily: 'SFProRegular'),
                            )
                          ],
                        ),
                        Column(
                          children: <Widget>[
                            CircleAvatar(
                              backgroundImage:
                                  ExactAssetImage('images/user_icon.png'),
                              minRadius: 20,
                              maxRadius: 30,
                            ),
                            Text(
                              'Women',
                              style: TextStyle(
                                  fontSize: 13,
                                  color: Colors.black,
                                  fontFamily: 'SFProRegular'),
                            )
                          ],
                        ),
                      ],
                    ),
                  ),
                ),
                Card(
                  child: SizedBox(
                      height: 200.0,
                      child: Carousel(
                        images: [
                          NetworkImage(
                              'https://cdn-images-1.medium.com/max/2000/1*GqdzzfB_BHorv7V2NV7Jgg.jpeg'),
                          NetworkImage(
                              'https://cdn-images-1.medium.com/max/2000/1*wnIEgP1gNMrK5gZU7QS0-A.jpeg'),
                        ],
                        dotSize: 4.0,
                        dotSpacing: 15.0,
                        indicatorBgPadding: 5.0,
                        borderRadius: false,
                      )),
                ),
     GridView.count(   
    shrinkWrap: true,
    primary: false,  
                  childAspectRatio: 4.0,
                  // Create a grid with 2 columns. If you change the scrollDirection to
                  // horizontal, this produces 2 rows.
                  crossAxisCount: 2,
                  // Generate 100 widgets that display their index in the List.
                  children: List.generate(100, (index) {
                    return Center(
                      child: Text(
                        'Item $index',
                        style: Theme.of(context).textTheme.headline,
                      ),
                    );
                  }),
                )
              ],
            ),
          ),
        ),
        ),
        );
      }
    }
    
    2019-12-25 15:59:14
    赞同 展开评论 打赏
问答分类:

国内唯一 Forrester 公共云容器平台领导者象限。

相关电子书

更多
数据卷和数据卷容器 立即下载
容器计算服务 ACS 全新定义容器算力 立即下载
何种数据存储才能助力容器计算 立即下载