完整代码如下:
关键点做了注释
import 'package:dianxue/pages/tabTwo/dealError/dealErrorDetail.dart';
import 'package:flutter/material.dart';
import 'package:dianxue/config/common.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:dianxue/config/global.dart' as global;
class DealError extends StatefulWidget {
DealError({Key key,}):super(key: key);
DealErrorState createState() => DealErrorState();
}
class DealErrorState extends State<DealError> with SingleTickerProviderStateMixin {
List<int> page = [1, 1];
List _data = [[], []];
List<bool> hasMore = [true, true];
List<bool> isLoading = [false, false];
List<String> tabs = ["待批改", "已批改"];
int currentTab = 0;
TabController _tabbarController; // 关键
ScrollController scrollCtrl = ScrollController();
void _loadMore() async{
if(isLoading[currentTab] || !hasMore[currentTab]) return;
setState(() {
isLoading[currentTab] = true;
});
List<int> newData;
await Future.delayed(Duration(seconds: 1), () {
page[currentTab]++;
newData = List<int>.generate(10, (index) => index);
hasMore[currentTab] = page[currentTab] > 5 ? false : true;
});
_data[currentTab].addAll(newData);
setState(() {
page = page;
hasMore = hasMore;
isLoading[currentTab] = false;
_data = _data;
});
}
@override
void initState() {
super.initState();
// 关键
_tabbarController = new TabController(initialIndex: 0, length: tabs.length, vsync: this);
_tabbarController.addListener(() {
//关键 添加内容tab切换监听, 将变化同步到自定义的TabNav状态上
setState(() {
currentTab = _tabbarController.index;
});
if(page[_tabbarController.index] == 1) {
_loadMore();
}
});
_loadMore();
scrollCtrl.addListener(() {
if (scrollCtrl.position.pixels == scrollCtrl.position.maxScrollExtent) {
_loadMore();
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('批改错题', style: TextStyle(fontSize: setFont(18), color: Color(0xffffffff), fontWeight: FontWeight.w800),),
leading: goBackArr(context),
elevation: 0,
actions: <Widget>[
GestureDetector(
onTap: () {
showMenu();
},
child: Container(
width: setSize(36),
alignment: Alignment.center,
child: Image(image: getAssetImage('menu.png'), width: setSize(22), height: setSize(22), fit: BoxFit.fill,),
)
),
SizedBox(width: setSize(28),)
],
),
body: Container(
width: setFullWidth(context),
height: setFullHeight(context),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
width: setSize(310),
height: setSize(33),
decoration: BoxDecoration(
gradient: LinearGradient(colors: [Color(0xFF569EFF), Color(0xFF48A2FF)]),
borderRadius: BorderRadius.circular(6)
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: tabs.map((e) => GestureDetector(
onTap: () {
int idx = tabs.indexOf(e);
setState(() {
currentTab = idx;
});
// 关键 将状态赋予tabBarView
_tabbarController.index = currentTab;
if(page[idx] == 1) {
_loadMore();
}
},
child: Container(
width: setSize(150),
height: setSize(27),
decoration: BoxDecoration(
color: currentTab == tabs.indexOf(e) ? Color.fromRGBO(255, 255, 255, 0.98) : Colors.transparent,
borderRadius: BorderRadius.circular(6)
),
alignment: Alignment.center,
child: Text(e, style: TextStyle(fontSize: setFont(14), color: currentTab == tabs.indexOf(e) ? Color(0xff62aaff) : Colors.white, fontWeight: FontWeight.w600),),
),
)
).toList()
),
),
SizedBox(height: setSize(15),),
Expanded(
child: TabBarView(
controller: _tabbarController,
children: _data.map((e) {
return e.length > 0 ? ListView.builder(
itemCount: e.length + 1,
physics: const AlwaysScrollableScrollPhysics(),
controller: scrollCtrl,
itemBuilder: (context, index) {
if (index == e.length) {
return loadMoreBottom(hasMore[currentTab]);
} else {
return GestureDetector(
onTap: () {
Navigator.of(global.homeContext).push(MaterialPageRoute(builder: (context) {
return DealErrorDetail();
}));
},
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
padding: EdgeInsets.only(top: setSize(15)),
margin: EdgeInsets.only(bottom: setSize(10)),
decoration: BoxDecoration(
color: Color(0xffffffff),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
color: Colors.white,
padding: EdgeInsets.symmetric(horizontal: setSize(40)),
child: Html(
data: '<h3>这道题好难啊!</h3><p> 阿兰德斯康啦是的</p><p>八十多分离开阿斯顿里卡多死了卡斯蒂略</p><p>hello world</p>',
),
),
Container(
height: setSize(40),
padding: EdgeInsets.symmetric(horizontal: setSize(40)),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
height: setSize(20),
padding: EdgeInsets.symmetric(horizontal: setSize(14)),
decoration: BoxDecoration(
color: Color.fromRGBO(255, 207, 117, 0.18),
borderRadius: BorderRadius.circular(setSize(2)),
boxShadow: [BoxShadow(blurRadius: setSize(20), color: Color.fromRGBO(255, 207, 117, 1), offset: Offset(0, setSize(20)), spreadRadius: setSize(-15))]
),
alignment: Alignment.center,
child: Text('数学', style: TextStyle(fontSize: setFont(10), color: Color(0xffff7f00), fontWeight: FontWeight.w800),),
),
Text('36/120', style: TextStyle(fontSize: setFont(10), color: Color(0xffff7f00), fontWeight: FontWeight.w800),),
],
)
)
],
),
),
Container(
height: setSize(8),
color: Color(0xfff4f4f4),
)
],
)
);
}
},
) : (isLoading[currentTab] || _data.indexOf(e) != currentTab ?
LoadingView(context: context,) :
blankView(context, getAssetImage('blank_content.png'), '暂无内容哦~'));
},).toList(),
),
)
],
)
)
);
}
}