Dart 如何刷新标签栏的拉入颤振?
我有一个选项卡栏,有5个选项卡,每个选项卡包含一些数据。当用户下拉刷新时,我想显示一个刷新指示器,选项卡中的数据应该通过API调用更新。下面是我迄今为止尝试过的代码 帮助我了解如何在pull上显示刷新指示器,以及如何使用回调函数执行操作Dart 如何刷新标签栏的拉入颤振?,dart,flutter,Dart,Flutter,我有一个选项卡栏,有5个选项卡,每个选项卡包含一些数据。当用户下拉刷新时,我想显示一个刷新指示器,选项卡中的数据应该通过API调用更新。下面是我迄今为止尝试过的代码 帮助我了解如何在pull上显示刷新指示器,以及如何使用回调函数执行操作 import 'dart:async'; import 'package:flutter/material.dart'; void main() => runApp(MaterialApp( debugShowCheckedModeBann
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,
),
);
}
}
导入'dart:async';
进口“包装:颤振/材料.省道”;
void main()=>runApp(MaterialApp(
debugShowCheckedModeBanner:false,
主页:主页(),
));
类主页扩展了StatefulWidget{
@凌驾
_HomePageState createState()=>new_HomePageState();
}
类_HomePageState扩展状态{
var refreshKey=GlobalKey();
//@覆盖
//void initState(){
//super.initState();
//随机=随机();
//刷新列表();
// }
Future refreshList()异步{
refreshKey.currentState?显示(atTop:false);
等待未来。延迟(持续时间(秒:2));
设置状态(){
新网页(;
});
返回null;
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“拉动刷新”),
),
正文:刷新指示器(
键:刷新键,
子级:新的DefaultTabController(
长度:5,
子:新列(
儿童:[
新容器(
宽度:1200.0,
儿童:新材料(
颜色:颜色。浅蓝色,
孩子:新的TabBar(
isScrollable:是的,
labelColor:Colors.white,
选项卡:[
标签(
子项:新文本(“全部”,
样式:新文本样式(fontSize:20.0)
),
),
标签(
子项:新文本(“移动”,
样式:新文本样式(fontSize:20.0)),
),
标签(
子项:新文本(“空闲”,
样式:新文本样式(fontSize:20.0)),
),
标签(
子项:新文本(“已停驻”,
样式:新文本样式(fontSize:20.0)),
),
标签(
子项:新文本(“非活动”,
样式:新文本样式(fontSize:20.0)),
),
],
),
),
),
新扩展(
子项:新选项卡视图(
儿童:[
标签(
子项:新文本(“演示”,
样式:新文本样式(fontSize:20.0)),
),
标签(
子项:新文本(“演示”,
样式:新文本样式(fontSize:20.0)),
),
标签(
子项:新文本(“演示”,
样式:新文本样式(fontSize:20.0)),
),
标签(
子项:新文本(“演示”,
样式:新文本样式(fontSize:20.0)),
),
标签(
子项:新文本(“演示”,
样式:新文本样式(fontSize:20.0)),
),
],
),
),
],
),
),
onRefresh:refreshList,
),
);
}
}
刷新指示器的子项应是可滚动的,在上面的示例中,它是DefaultTabController
,因此您无法获得刷新指示器
我将RefreshIndicator
移动到Tab
视图中,并添加了一个可滚动的子视图(ListView)。请参考以下代码
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: 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 RefreshIndicator(
child: new ListView(
children: <Widget>[
new Column(
children: <Widget>[
new Center(
child: new Text("Demo",
style: new TextStyle(fontSize: 20.0)),
)
],
)
],
),
onRefresh: refreshList,
key: refreshKey,
),
),
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)),
),
],
),
),
],
),
),
);
}
}
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));
//network call and setState so that view will render the new values
print("refresh");
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: new DefaultTabController(
length: 5,
child: Scaffold(
appBar: AppBar(
title: Text("Pull to refresh"),
bottom: 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)),
),
],
),
),
body: new Column(
children: <Widget>[
new Expanded(
child: new TabBarView(
children: [
Tab(
child: new RefreshIndicator(
child: new ListView(
children: <Widget>[
new Column(
children: <Widget>[
new Center(
child: new Text("Demo",
style: new TextStyle(fontSize: 20.0)),
)
],
)
],
),
onRefresh: refreshList,
key: refreshKey,
),
),
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)),
),
],
),
),
],
),
),
));
}
}
RefreshIndicator的子级应该是可滚动的
简单的句子,但最大的帮助是:D@moshfiqur你有这个问题的解决办法吗。?