我有一个选项卡栏,其中有5个选项卡,每个选项卡包含一些数据。当用户下拉以进行刷新时,我希望显示一个刷新指示器,并通过API调用更新选项卡内的数据。以下是我到目前为止尝试过的代码。
请帮助我如何在下拉时显示刷新指示器,以及编写一个回调函数来执行某些操作。
请帮助我如何在下拉时显示刷新指示器,以及编写一个回调函数来执行某些操作。
import 'dart:async';
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
));
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => new _HomePageState();
}
class _HomePageState extends State<HomePage> {
var refreshKey = GlobalKey<RefreshIndicatorState>();
// @override
// void initState() {
// super.initState();
// random = Random();
// refreshList();
// }
Future<Null> refreshList() async {
refreshKey.currentState?.show(atTop: false);
await Future.delayed(Duration(seconds: 2));
setState(() {
new HomePage();
});
return null;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Pull to refresh"),
),
body: RefreshIndicator(
key: refreshKey,
child: new DefaultTabController(
length: 5,
child: new Column(
children: <Widget>[
new Container(
width: 1200.0,
child: new Material(
color: Colors.lightBlue,
child: new TabBar(
isScrollable: true,
labelColor: Colors.white,
tabs: [
Tab(
child: new Text("All",
style: new TextStyle(fontSize: 20.0)
),
),
Tab(
child: new Text("Moving",
style: new TextStyle(fontSize: 20.0)),
),
Tab(
child: new Text("Idle",
style: new TextStyle(fontSize: 20.0)),
),
Tab(
child: new Text("Parked",
style: new TextStyle(fontSize: 20.0)),
),
Tab(
child: new Text("Inactive",
style: new TextStyle(fontSize: 20.0)),
),
],
),
),
),
new Expanded(
child: new TabBarView(
children: [
Tab(
child: new Text("Demo",
style: new TextStyle(fontSize: 20.0)),
),
Tab(
child: new Text("Demo",
style: new TextStyle(fontSize: 20.0)),
),
Tab(
child: new Text("Demo",
style: new TextStyle(fontSize: 20.0)),
),
Tab(
child: new Text("Demo",
style: new TextStyle(fontSize: 20.0)),
),
Tab(
child: new Text("Demo",
style: new TextStyle(fontSize: 20.0)),
),
],
),
),
],
),
),
onRefresh: refreshList,
),
);
}
}
RefreshIndicator
的子组件必须是可滚动的。 - moshfiqur