1

如何使用 json API 在列表视图中创建网格视图

我浏览了上面的链接及其很好的解释,但是当我用这段代码再添加一个 sliver 列表时,如果尝试快速滚动,slivergrid 会发生一些事情,slivergrid 中只有一个项目,请帮助我是新手扑。

4

1 回答 1

2

对于 main.dart

import 'package:flutter/material.dart';
import './pages/fetch.dart';

void main() {
  runApp(new MaterialApp(
      home: new fetchPost(),

  ));

}

对于 fetch.dart

class fetch extends StatefulWidget{
  fetchPost createState()=> fetchPost();
}

class fetchPost extends State<fetch>{

    //Add your own Json fetch function

       @override
          Widget build(BuildContext context) {
            return CustomScrollView(
                slivers: <Widget>[
                  SliverPadding(
                    padding: EdgeInsets.all(0.0),
                    sliver :SliverList(
                    delegate: SliverChildListDelegate([
                      Column(
                        children: <Widget>[
                          //your widgets
                          firstWidget(),
                          yourNextWidget(),  
                        ],
                      )
                    ]),
                  )
                  )
                ],
            );
          }
    Widget firstWidget(){
        return Column( GridView.builder(
                  gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2,crossAxisSpacing: 0.0, childAspectRatio: 1/1),
                  itemCount: recent == null ? 0 : list.length,
                  itemBuilder: (BuildContext context, int index, ) {
                  return Column(
                   children: <Widget>[
                      Card(
                        child: Column(
                          children: <Widget>[
                            new Image.network('asset/image']),
                            new ListTile(                         
                                title: new Text(''),
                                    subtitle: Text("",),
                                    onTap: () {Navigator.push(
                                      context, new MaterialPageRoute(
                                      builder: (context) => new nextclass(),
                                      ),
                                    );
                                    },
                                dense: true,
                              ),  
                            ],
                         ),
                       )
                     ],
                   );
                 },shrinkWrap: true,
                physics: ClampingScrollPhysics(),
              ) );
           }


     Widget nextWidget(){
           return Column(
         //your custom widget
          );
        }
    }
于 2019-05-27T16:51:35.183 回答