gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 07:03:06 31 4
gpt4 key购买 nike

我在列中有一个 ListView,需要将 RefreshIndicator 添加到 ListView,但效果不佳我尝试通过 Expanded 包含 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,)
],
)

);
}
}

最佳答案

我有两点意见:

  1. 您应该使用带有 flex 参数的 Expanded 小部件。
  2. 您不需要在 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,
),
)
],
),
);
}
}

关于flutter - 如何修复列中的 ListView 并将 RefreshIndicator 添加到 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56442336/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com