如何修复列中的ListView并将RefreshIndicator添加到ListView

时间:2019-06-04 10:46:51

标签: flutter flutter-layout

我在列中有一个ListView,需要将RefreshIndicator添加到ListView中,但是效果不佳 我尝试通过扩展包含listView,然后列表显示良好,但是当调用RefreshIndicator,应用程序转储... 有人可以帮助我,如何修复此代码,谢谢

import 'dart:async';

import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  List<int> items = List.generate(16, (i) => i);

  Future<Null> _handleRefresh() async {
    await Future.delayed(Duration(seconds: 5), () {
      print('refresh');
      setState(() {
        items.clear();
        items = List.generate(40, (i) => i);
        return null;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Refresh"),
        ),
        body:  Column(
          children: <Widget>[RefreshIndicator(child:
            ListView.builder(
              itemCount: items.length,
            shrinkWrap: true,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text("Index$index"),
                );
              },
             ), onRefresh: _handleRefresh,)
          ],
        )

        );
  }
}

2 个答案:

答案 0 :(得分:0)

我有两句话:

  1. 您应将Expanded小部件与flex参数一起使用。
  2. 您无需使用return方法中的任何东西setState
    import 'dart:async';
    import 'package:flutter/material.dart';
    void main() {
      runApp(new MaterialApp(
        home: new MyApp(),
      ));
    }

    class MyApp extends StatefulWidget {
      @override
      State<StatefulWidget> createState() => MyAppState();
    }

    class MyAppState extends State<MyApp> {
      List<int> items = List.generate(16, (i) => i);

      Future<Null> _handleRefresh() async {
        await Future.delayed(Duration(seconds: 5), () {
          print('refresh');
          setState(() {
            items.clear();
            items = List.generate(40, (i) => i);
          });
        });
      }

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("Refresh"),
          ),
          body: Column(
            children: <Widget>[
              Expanded(
                flex: 1,
                child: RefreshIndicator(
                  child: ListView.builder(
                    itemCount: items.length,
                    shrinkWrap: true,
                    itemBuilder: (context, index) {
                      return ListTile(
                        title: Text("Index$index"),
                      );
                    },
                  ),
                  onRefresh: _handleRefresh,
                ),
              )
            ],
          ),
        );
      }
    }

答案 1 :(得分:0)

上面的答案(由塔雷克·巴兹(Tarek Baz)提出)是正确的,但是在某些特殊情况下(例如深/复杂的小部件树),可能还不够,您可能必须将物理参数传递给ListView.builder()函数。

ListView.builder( 
physics: const AlwaysScrollableScrollPhysics(), 
itemCount: .... ... )