import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
void main() => runApp(Myapp());
class Myapp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: MyHome());
}
}
class MyHome extends StatefulWidget {
@override
_MyHomeState createState() => _MyHomeState();
}
class _MyHomeState extends State {
final _suggestions = [];
bool _loading = true;
ScrollController _strollCtrl = ScrollController();
Future _getData() async {
await Future.delayed(Duration(seconds: 1));
setState(() {
_suggestions.addAll(generateWordPairs().take(15));
_loading = false;
});
}
@override
void initState() {
_getData();
_strollCtrl.addListener(() {
if (_strollCtrl.position.pixels == _strollCtrl.position.maxScrollExtent) {
if (!_loading) {
setState(() {
_loading = true;
});
}
_getData();
}
});
super.initState();
}
@override
void dispose() {
_strollCtrl.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ListView'),
),
body: Stack(
children: [
_loading
? Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RefreshProgressIndicator(
semanticsLabel: 'loading more..',
semanticsValue: 'loading more..',
),
Text(
'加载中...',
style: Theme.of(context).textTheme.title,
),
],
),
)
: Container(),
// 下拉加载更多组件
RefreshIndicator(
onRefresh: () async {
_suggestions.clear();
return _getData();
},
child: ListView.builder(
physics: const AlwaysScrollableScrollPhysics(), // 确保滚动视图始终可滚动
controller: _strollCtrl,
itemCount: _suggestions.length,
itemBuilder: (context, i) {
return ListTile(
leading: Text('${(i + 1).toString()}'),
title: Text('${_suggestions[i].asPascalCase}'),
trailing: Icon(Icons.event),
);
},
),
),
],
),
);
}
}