Flutter ListView 上拉滑动加载更多

复制代码
复制代码
//ListView滚动Demo
import 'dart:ffi';

import 'package:flutter/material.dart';

void main() {
  runApp(DemoApp());
}

class DemoApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new DemoAppState();
  }
}

class DemoAppState extends State {
  //初始化
  var _items = [];
  int countIndex = 0;

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

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

  void getData() {
    //初始化数据源
    for (int i = 0; i < 20; i++) {
      _items.insert(_items.length, "第${_items.length}条原始数据");
      print(_items[i]);
    }
  }

  Future<void> _onRefresh() async {
    await Future.delayed(Duration(seconds: 1)).then((e) {
      setState(() {
        _items.clear();
        for (int i = 0; i < 20; i++) {
          _items.insert(_items.length, "第${_items.length}条下拉刷新后的数据");
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'SingleChildListViewDemo',
      home: Scaffold(
        appBar: AppBar(
          title: Text('SingleChildListViewDemo'),
        ),
        body: new ListView.separated(
          physics: BouncingScrollPhysics(),
          //ListView构造器
          itemBuilder: (context, index) {
            return ListTile(
              title: Text(
                '$_items[$index]',
                textAlign: TextAlign.center,
                style: TextStyle(fontSize: 28.0, color: Colors.black38),
              ),
            );
          },
          separatorBuilder: (context, index) {
            return new Divider(
              color: Colors.blue,
            );
          },
          itemCount: _items.length,
        ),
      ),
    );
  }
}
复制代码

 


 


 





复制代码

 

posted @   KingWang588  阅读(339)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示