flutter 如何在一个页面中添加具有延迟加载和幻灯片显示的gridview

ccrfmcuu  于 2023-06-30  发布在  Flutter
关注(0)|答案(1)|浏览(172)

我试图使网页与gridview。建设者和幻灯片在首页,并添加到gridview lasy加载。我添加了所有的东西,但lasy加载不工作,这是我的代码:

class _HomePageState extends State<HomePage> {
  List<String> images = [
    'https://picsum.photos/id/1/200/300',
    'https://picsum.photos/id/2/200/300',
    'https://picsum.photos/id/3/200/300',
  ];

  List<dynamic> data = [];

  bool isLoading = false;

  int page = 1;

  final int perPage = 10;

  ScrollController _scrollController = ScrollController();

  Future<void> fetchData() async {
    setState(() {
      isLoading = true;
    });

    final response = await http.get(Uri.parse(
        'https://jsonplaceholder.typicode.com/photos?_page=$page&_limit=$perPage'));

    setState(() {
      isLoading = false;
      page++;
      data.addAll(jsonDecode(response.body));
    });
  }

  @override
  void initState() {
    super.initState();

    fetchData();

    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        fetchData();
      }
    });
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Demo'),
      ),
      body: ListView(
        children: [
          SizedBox(
            height: 200,
            child: ListView.builder(
              scrollDirection: Axis.horizontal,
              itemCount: images.length,
              itemBuilder: (BuildContext context, int index) {
                return Image.network(images[index]);
              },
            ),
          ),
          SizedBox(height: 8),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: GridView.builder(
              controller: _scrollController,
              shrinkWrap: true,
              physics: NeverScrollableScrollPhysics(),
              itemCount: data.length,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2,
                mainAxisSpacing: 8,
                crossAxisSpacing: 8,
                childAspectRatio: 0.75,
              ),
              itemBuilder: (BuildContext context, int index) {
                return Card(
                  child: Column(
                    children: [
                      Expanded(
                        child: Image.network(
                          data[index]['thumbnailUrl'],
                          fit: BoxFit.cover,
                        ),
                      ),
                      SizedBox(height: 8),
                      Text(
                        data[index]['title'],
                        style: TextStyle(fontSize: 16),
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                      ),
                      SizedBox(height: 8),
                      Text(
                        'ID: ${data[index]['id']}',
                        style: TextStyle(fontSize: 12),
                      ),
                    ],
                  ),
                );
              },
            ),
          ),
          SizedBox(height: isLoading ? 16 : 0),
          Center(
            child: isLoading
                ? CircularProgressIndicator()
                : SizedBox(),
          ),
        ],
      ),
    );
  }
}

请注意,在添加幻灯片放映之前,它可以正常工作,但在添加幻灯片放映之后,它不再工作。代码中不显示错误。有什么办法解决这个问题吗?

bgtovc5b

bgtovc5b1#

您提供的代码似乎缺少延迟加载功能。更新fetchData函数,以根据当前页值和每页限制获取数据。目前,您从静态URL获取数据,但您可以修改它以从API或任何其他支持分页的数据源获取数据。修改initState方法以在页面加载时获取初始数据:example:@override void initState(){ super.initState(); getData();}

相关问题