repair_list_page.dart 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import 'package:flutter/material.dart';
  2. import 'package:liftmanager/common/common.dart';
  3. import 'package:liftmanager/common/user_db.dart';
  4. import 'package:liftmanager/internal/repair/model/repair_count_item.dart';
  5. import 'package:liftmanager/internal/repair/provider/repair_page_provider.dart';
  6. import 'package:liftmanager/internal/repair/repair_router.dart';
  7. import 'package:liftmanager/internal/repair/widgets/repair_list.dart';
  8. import 'package:liftmanager/net/api_service.dart';
  9. import 'package:liftmanager/res/resources.dart';
  10. import 'package:liftmanager/routers/fluro_navigator.dart';
  11. import 'package:liftmanager/utils/theme_utils.dart';
  12. import 'package:liftmanager/utils/toast.dart';
  13. import 'package:liftmanager/widgets/app_bar.dart';
  14. import 'package:provider/provider.dart' as p;
  15. class RepairListPage extends StatefulWidget {
  16. RepairListPage({this.topInto = false});
  17. final bool topInto;
  18. @override
  19. RepairListPageState createState() => RepairListPageState();
  20. }
  21. class RepairListPageState extends State<RepairListPage>
  22. with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  23. RepairPageProvider provider = RepairPageProvider();
  24. TabController _tabController;
  25. PageController _pageController = PageController(initialPage: 0);
  26. List<Widget> mListView = [];
  27. RepairCountItem countItem = RepairCountItem();
  28. @override
  29. void initState() {
  30. super.initState();
  31. initListView();
  32. _tabController = new TabController(vsync: this, length: 3);
  33. // if (widget.topInto) {
  34. // _tabController.animateTo(1);
  35. // _pageController = PageController(initialPage: 1);
  36. // } else {
  37. _pageController = PageController(initialPage: 0);
  38. // }
  39. getHasRole();
  40. getCount();
  41. }
  42. getCount() {
  43. ApiService(context: context).repairCount(onSuccess: (res) {
  44. if (res != null) {
  45. countItem = res;
  46. setState(() {});
  47. }
  48. }, onError: (code, msg) {
  49. toasts(msg);
  50. });
  51. }
  52. bool noRole = true;
  53. getHasRole() async {
  54. var role = await User().getCompanyRole();
  55. if (role == Constant.RoleAdmin ||
  56. role == Constant.RoleRegion ||
  57. role == Constant.RoleWork) {
  58. noRole = false;
  59. setState(() {});
  60. }
  61. }
  62. initListView() {
  63. mListView.add(RepairList(
  64. index: 0,
  65. callback: () {
  66. getCount();
  67. }));
  68. mListView.add(RepairList(
  69. index: 1,
  70. callback: () {
  71. getCount();
  72. }));
  73. mListView.add(RepairList(
  74. index: 2,
  75. callback: () {
  76. getCount();
  77. }));
  78. }
  79. @override
  80. void dispose() {
  81. _tabController.dispose();
  82. super.dispose();
  83. }
  84. _onPageChange(int index) {
  85. _tabController.animateTo(index);
  86. provider.setIndex(index);
  87. getCount();
  88. }
  89. goToPage(pagePath) {
  90. NavigatorUtils.pushResult(context, pagePath, (res) {
  91. getCount();
  92. print(_pageController.page.toInt());
  93. RepairList widget = mListView[_pageController.page.toInt()];
  94. if (widget.mState != null) {
  95. widget.mState.onRefresh();
  96. }
  97. });
  98. }
  99. @override
  100. Widget build(BuildContext context) {
  101. return p.ChangeNotifierProvider<RepairPageProvider>(
  102. create: (_) => provider,
  103. child: WillPopScope(
  104. onWillPop: () {
  105. NavigatorUtils.goBackWithParams(context, true);
  106. return Future.value(false);
  107. },
  108. child: Scaffold(
  109. appBar: MyAppBar(
  110. centerTitle: "紧急召修",
  111. actions: <Widget>[
  112. Offstage(
  113. offstage: noRole,
  114. child: FlatButton(
  115. child: Text("新建", key: const Key('actionName')),
  116. textColor: Colours.text,
  117. highlightColor: Colors.transparent,
  118. onPressed: () {
  119. goToPage(RepairRouter.repairCreatePage);
  120. },
  121. ))
  122. ],
  123. ),
  124. body: Column(
  125. crossAxisAlignment: CrossAxisAlignment.start,
  126. children: <Widget>[
  127. Container(
  128. // 隐藏点击效果
  129. color: ThemeUtils.getTabsBg(context),
  130. child: TabBar(
  131. onTap: (index) {
  132. if (!mounted) {
  133. return;
  134. }
  135. _pageController.jumpToPage(index);
  136. },
  137. isScrollable: false,
  138. controller: _tabController,
  139. labelStyle: TextStyles.textBold18,
  140. indicatorSize: TabBarIndicatorSize.label,
  141. labelPadding: const EdgeInsets.all(0),
  142. unselectedLabelColor: Colours.text_gray,
  143. labelColor: Theme.of(context).primaryColor,
  144. indicatorPadding:
  145. const EdgeInsets.only(left: 20.0, right: 20.0),
  146. tabs: <Widget>[
  147. _TabView("待修理", "(${countItem.toRepair})", 0),
  148. _TabView("修理中", "(${countItem.processing})", 1),
  149. _TabView("已完成", "(${countItem.complete})", 2),
  150. ],
  151. ),
  152. ),
  153. Gaps.line,
  154. Expanded(
  155. child: PageView.builder(
  156. key: const Key('pageView'),
  157. itemCount: 3,
  158. onPageChanged: _onPageChange,
  159. controller: _pageController,
  160. itemBuilder: (BuildContext context, int index) {
  161. return Container(
  162. color: ThemeUtils.getBackgroundColor(context),
  163. child: mListView[index]);
  164. },
  165. ),
  166. )
  167. ],
  168. ),
  169. ),
  170. ));
  171. }
  172. @override
  173. bool get wantKeepAlive => true;
  174. }
  175. class _TabView extends StatelessWidget {
  176. const _TabView(this.tabName, this.tabSub, this.index);
  177. final String tabName;
  178. final String tabSub;
  179. final int index;
  180. @override
  181. Widget build(BuildContext context) {
  182. return p.Consumer<RepairPageProvider>(
  183. builder: (_, provider, child) {
  184. bool isDark = ThemeUtils.isDark(context);
  185. return Tab(
  186. child: SizedBox(
  187. child: Row(
  188. crossAxisAlignment: CrossAxisAlignment.center,
  189. mainAxisAlignment: MainAxisAlignment.center,
  190. children: <Widget>[
  191. Text(
  192. tabName,
  193. style: TextStyle(
  194. fontSize: 15,
  195. color: isDark ? Colours.dark_text : Colours.text),
  196. ),
  197. Padding(
  198. padding: const EdgeInsets.only(top: 1.0),
  199. child:
  200. Text(tabSub, style: TextStyle(fontSize: Dimens.font_sp12)),
  201. ),
  202. ],
  203. ),
  204. ));
  205. },
  206. );
  207. }
  208. }