我在列中有一个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,)
],
)
);
}
}
答案 0 :(得分:0)
我有两句话:
Expanded
小部件与flex
参数一起使用。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: .... ... )