Flutter:列表视图内的网格视图

时间:2019-01-07 09:07:29

标签: android listview gridview dart flutter

我尝试过:

ListView.builder(
  shrinkWrap: true,
  itemBuilder: (BuildContext context, int index) {
  return GridView.count(
  crossAxisCount: 5,
  children: List.generate(10, (index) {
    return Center(
      child: Text(
        '$index AM',
      ),
    );
  }),
);
},
itemCount: partnerArr.length,
)

我想在脚手架中制作这种类型的列表视图,我该怎么做?

Grid view inside list view

3 个答案:

答案 0 :(得分:1)

您可以尝试以下代码:

@override
  Widget build(BuildContext context) {
    return Scaffold(
        body: ListView.separated(
      separatorBuilder: (context, int) {
        return Divider(color: Colors.black,);
      },
     // shrinkWrap: true,
      itemBuilder: (BuildContext context, int index) {
        return GridView.count(
          shrinkWrap: true,
          crossAxisCount: 3,
          childAspectRatio: 2.0,
          children: List.generate(6, (index) {
            return Center(
              child: RaisedButton(
                onPressed: (){},
                color: Colors.greenAccent,
                child: Text(
                  '$index AM',
                ),
              ),
            );
          }),
        );
      },
      itemCount: 4,
    ));
  }

输出:

enter image description here

答案 1 :(得分:0)

使用CustomScrollView

CustomScrollView(
  slivers: List.generate(
      10,
      (item) => SliverGrid(
            gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
              maxCrossAxisExtent: 150.0,
              mainAxisSpacing: 10.0,
              crossAxisSpacing: 10.0,
              childAspectRatio: 4.0,
            ),
            delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) {
                return Container(
                  alignment: Alignment.center,
                  color: Colors.amber[100 * (index % 9)],
                  child: Text('grid item $index'),
                );
              },
              childCount: 6,
            ),
          )),
)

答案 2 :(得分:0)

import 'package:flutter/material.dart';

class SubmitRequest extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _SubmitRequest();
  }
}

class _SubmitRequest extends State<SubmitRequest> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        drawer: Drawer(
          child: Container(
            color: Colors.black54,
            child: ListView(
              padding: EdgeInsets.only(top: 40.0),
              children: <Widget>[
                ListTile(
                  title: Text('Dashboard'),
                ),
                ListTile(
                  title: Text('Submit Reports'),
                ),
                ListTile(
                  title: Text('Inbox Requests'),
                ),
              ],
            ),
          ),
        ),
        appBar: AppBar(
          backgroundColor: Colors.blue,
          leading: IconButton(icon: Icon(Icons.menu), onPressed: () {}),
          actions: <Widget>[
            //    IconButton(icon: Icon(Icons.search), onPressed: () {}),
            IconButton(icon: Icon(Icons.help), onPressed: () {}),
          ],
          bottom: PreferredSize(
            child: Padding(
              padding: const EdgeInsets.fromLTRB(0.0, 0.0, 15.0, 16.0),
              child: Container(
                margin: EdgeInsets.only(left: 16.0),
                child: TextField(
                  decoration: new InputDecoration(
                      suffixIcon: IconButton(
                          icon: Icon(Icons.search),
                          onPressed: () {
                            debugPrint('222');
                          }),
                      border: new OutlineInputBorder(
                        borderRadius: const BorderRadius.all(
                          const Radius.circular(50.0),
                        ),
                      ),
                      filled: true,
                      hintStyle: new TextStyle(color: Colors.grey[800]),
                      hintText: "Search",
                      fillColor: Colors.white),
                ),
              ),
            ),
            preferredSize: Size(0.0, 80.0),
          ),
        ),
        body: Scaffold(
            body: ListView.builder(
//              separatorBuilder: (context, int) {
//                return Divider(color: Colors.black,);
//              },
              // shrinkWrap: true,
              itemBuilder: (BuildContext context, int index) {
                return GridView.count(
                  shrinkWrap: true,
                  crossAxisCount: 4,
                  childAspectRatio: 1.0,
                  children: List.generate(6, (index) {
                    return GridTile(
                      child: new Card(
                        color: Colors.blue.shade100,
                        child: new Center(
                          child: new Text('Exterior $index'),
                        ),
                      ),
                    );
                  }),
                );
              },
              itemCount: 4,
            )
        )
    );
  }
}

enter image description here