基础组件库
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

custom_page.dart 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import 'dart:convert';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:pull_to_refresh/pull_to_refresh.dart';
  4. import 'package:tab_indicator_styler/tab_indicator_styler.dart';
  5. import 'package:zhiying_base_widget/pages/custom_page/custom_item_page.dart';
  6. import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_bg_notifier.dart';
  7. import 'package:zhiying_base_widget/widgets/custom/search/custom_search_widget.dart';
  8. import 'package:zhiying_base_widget/widgets/empty/empty_widget.dart';
  9. import 'package:zhiying_base_widget/widgets/others/mine_header_bg_widget.dart';
  10. import 'package:zhiying_comm/zhiying_comm.dart';
  11. import 'package:flutter/material.dart';
  12. import 'package:provider/provider.dart';
  13. import 'package:flutter_bloc/flutter_bloc.dart';
  14. import 'bloc/custom_page_bloc.dart';
  15. import 'bloc/custom_page_state.dart';
  16. import 'bloc/custom_page_event.dart';
  17. import 'bloc/custom_page_repository.dart';
  18. import 'dart:ui';
  19. import 'package:fluttertoast/fluttertoast.dart';
  20. ///
  21. /// 通用模块页面
  22. ///
  23. class CustomPage extends StatefulWidget {
  24. final Map<String, dynamic> data;
  25. CustomPage(this.data, {Key key}) : super(key: key);
  26. @override
  27. _CustomPageState createState() => _CustomPageState();
  28. }
  29. class _CustomPageState extends State<CustomPage> {
  30. @override
  31. Widget build(BuildContext context) {
  32. return MultiProvider(
  33. providers: [
  34. ChangeNotifierProvider.value(value: MainPageBgNotifier()),
  35. ],
  36. child: BlocProvider<CustomPageBloc>(
  37. create: (_) => CustomPageBloc(CustomPageRepository(data: widget?.data))..add(CustomPageInitEvent()),
  38. child: _CommonPageContainer(widget?.data),
  39. // ),
  40. ),
  41. );
  42. }
  43. }
  44. class _CommonPageContainer extends StatefulWidget {
  45. final Map<String, dynamic> data;
  46. _CommonPageContainer(this.data);
  47. @override
  48. __CommonPageContainerState createState() => __CommonPageContainerState();
  49. }
  50. class __CommonPageContainerState extends State<_CommonPageContainer> with SingleTickerProviderStateMixin {
  51. TabController _tabController;
  52. // 是否有AppBar
  53. bool _isHasAppbar = false;
  54. // 是否有TabBar
  55. bool _isHasTabBar = false;
  56. /// 刷新
  57. void _onRefreshEvent() async {
  58. BlocProvider.of<CustomPageBloc>(context).add(CustomPageRefreshEvent());
  59. }
  60. @override
  61. void dispose() {
  62. _tabController?.dispose();
  63. super.dispose();
  64. }
  65. @override
  66. Widget build(BuildContext context) {
  67. return MediaQuery.removePadding(
  68. context: context,
  69. child: BlocConsumer<CustomPageBloc, CustomPageState>(
  70. listener: (context, state) {},
  71. buildWhen: (prev, current) {
  72. if (current is CustomPageErrorState) {
  73. return false;
  74. }
  75. if (current is CustomPageRefreshSuccessState) {
  76. // _refreshController.refreshCompleted(resetFooterState: true);
  77. return false;
  78. }
  79. if (current is CustomPageRefreshErrorState) {
  80. // _refreshController.refreshFailed();
  81. return false;
  82. }
  83. return true;
  84. },
  85. builder: (context, state) {
  86. /// 有数据
  87. if (state is CustomPageLoadedState) {
  88. if (EmptyUtil.isEmpty(state.model)) return _buildEmptyWidget();
  89. return _buildMainWidget(state.model);
  90. }
  91. /// 初始化失败
  92. if (state is CustomPageInitErrorState) {
  93. return _buildEmptyWidget();
  94. }
  95. /// 骨架图
  96. return _buildSkeletonWidget();
  97. },
  98. ),
  99. );
  100. }
  101. /// 有数据
  102. Widget _buildMainWidget(List<Map<String, dynamic>> model) {
  103. return Stack(
  104. children: <Widget>[
  105. Scaffold(
  106. appBar: _buildAppbar(model?.first),
  107. backgroundColor: HexColor.fromHex('#F9F9F9'),
  108. // floatingActionButton: _buildFloatWidget(),
  109. floatingActionButtonLocation: _CustomFloatingActionButtonLocation(FloatingActionButtonLocation.endFloat, 0, -100),
  110. body: Column(children: _buildFirstWidget(model)),
  111. ),
  112. ],
  113. );
  114. }
  115. /// 骨架图
  116. Widget _buildSkeletonWidget() {
  117. return Scaffold();
  118. }
  119. /// 空数据视图
  120. Widget _buildEmptyWidget() {
  121. return Scaffold(
  122. backgroundColor: HexColor.fromHex('#F9F9F9'),
  123. appBar: AppBar(
  124. brightness: Brightness.light,
  125. backgroundColor: Colors.white,
  126. leading: IconButton(
  127. icon: Icon(
  128. Icons.arrow_back_ios,
  129. size: 22,
  130. color: HexColor.fromHex('#333333'),
  131. ),
  132. onPressed: () => Navigator.maybePop(context),
  133. ),
  134. title: Text(
  135. '',
  136. style: TextStyle(color: HexColor.fromHex('#333333'), fontSize: 18, fontWeight: FontWeight.bold),
  137. ),
  138. centerTitle: true,
  139. elevation: 0,
  140. ),
  141. body: Column(
  142. crossAxisAlignment: CrossAxisAlignment.center,
  143. mainAxisAlignment: MainAxisAlignment.center,
  144. children: <Widget>[
  145. Align(
  146. alignment: Alignment.topCenter,
  147. child: EmptyWidget(
  148. tips: '网络似乎开小差了~',
  149. ),
  150. ),
  151. GestureDetector(
  152. onTap: () => _onRefreshEvent(),
  153. behavior: HitTestBehavior.opaque,
  154. child: Container(
  155. alignment: Alignment.center,
  156. decoration: BoxDecoration(borderRadius: BorderRadius.circular(10), border: Border.all(color: HexColor.fromHex('#999999'), width: 0.5), color: Colors.white),
  157. width: 80,
  158. height: 20,
  159. child: Text(
  160. '刷新一下',
  161. style: TextStyle(fontSize: 12, color: HexColor.fromHex('#333333'), fontWeight: FontWeight.bold),
  162. ),
  163. ),
  164. )
  165. ],
  166. ));
  167. }
  168. /// 数据,生成第一层widget
  169. List<Widget> _buildFirstWidget(List<Map<String, dynamic>> model) {
  170. List<Widget> result = [];
  171. // 分类导航的key ⚠️ 这里先写成Test 后续要改
  172. const String CATEGORY_KEY = 'category';
  173. // 判断是否有分类导航
  174. // 判断最后一个是否属于分类导航,如果属于,则有分类导航,如果不是,则无分类导航
  175. bool haveCategory = !EmptyUtil.isEmpty(model?.last) && model.last.containsKey('mod_name') && model.last['mod_name'] == CATEGORY_KEY;
  176. int endIndexLength = model.length;
  177. // 如果没有分类导航,则取分类导航之上的所有mod
  178. if (!haveCategory) {
  179. for (int i = 0; i < model.length; i++) {
  180. Map<String, dynamic> item = model[i];
  181. if (item['mod_name'] == CATEGORY_KEY) {
  182. endIndexLength = (i + 1);
  183. break;
  184. }
  185. }
  186. }
  187. for (int i = 0; i < endIndexLength; i++) {
  188. WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(model[i]));
  189. // last model
  190. if (i == endIndexLength - 1) {
  191. result.addAll(_buildTabBar(model[i], i));
  192. break;
  193. }
  194. // appBar 无需在这里添加
  195. if (item.modName.contains('appbar')) {
  196. continue;
  197. }
  198. result.addAll(WidgetFactory.create(item.modName, isSliver: false, model: model[i]));
  199. }
  200. // 没有appbar并且没有tabbar,则给第一个元素加边距
  201. if (!_isHasAppbar && !_isHasTabBar) {
  202. result.insert(0, SizedBox(height: MediaQueryData.fromWindow(window).padding.top, child: Container(color: HexColor.fromHex('#FF4242'),),));
  203. }
  204. return result;
  205. }
  206. /// appbar
  207. Widget _buildAppbar(final Map<String, dynamic> model) {
  208. if (EmptyUtil.isEmpty(model)) return null;
  209. String mobName = model['mod_name'];
  210. if (!mobName.contains('_appbar')) return null;
  211. Map<String, dynamic> data = Map<String, dynamic>();
  212. try {
  213. data = jsonDecode(model['data']);
  214. } catch (e, s) {
  215. Logger.warn(e, s);
  216. }
  217. String parentTitle = !EmptyUtil.isEmpty(widget?.data) ? widget?.data['title'] ?? '' : '';
  218. _isHasAppbar = true;
  219. return AppBar(
  220. backgroundColor: HexColor.fromHex(null != data ? data['app_bar_bg_color'] ?? '#FFFFFF' : '#FFFFFF'),
  221. brightness: Brightness.light,
  222. leading: IconButton(
  223. icon: Icon(
  224. Icons.arrow_back_ios,
  225. size: 22,
  226. color: HexColor.fromHex('#333333'),
  227. ),
  228. onPressed: () => Navigator.maybePop(context),
  229. ),
  230. title: Text(
  231. null != data && data.containsKey('app_bar_name') ? data['app_bar_name'] != '自定义页面' ? data['app_bar_name'] : parentTitle : parentTitle,
  232. style: TextStyle(
  233. color: HexColor.fromHex(null != data ? data['app_bar_name_color'] ?? '#333333' : '#333333'),
  234. fontSize: 16,
  235. fontWeight: FontWeight.bold,
  236. ),
  237. ),
  238. centerTitle: true,
  239. elevation: 0,
  240. );
  241. }
  242. /// tabBar
  243. List<Widget> _buildTabBar(final Map<String, dynamic> model, final int index) {
  244. Map<String, dynamic> data = Map<String, dynamic>();
  245. List<Map<String, dynamic>> listStyle = [];
  246. List<Widget> result = [];
  247. try {
  248. data = jsonDecode(model['data']);
  249. listStyle = List.from(data['list_style']);
  250. } catch (e, s) {
  251. Logger.warn(e, s);
  252. }
  253. // 1、导航栏没开启的情况 传null进去进行获取没开启导航栏的widget集合
  254. if (EmptyUtil.isEmpty(listStyle)) {
  255. result.add(Expanded(
  256. child: CustomItemPage(null, 0, model['mod_id']?.toString() ?? null, model['mod_pid']?.toString() ?? null, (!_isHasAppbar && index == 0 )),
  257. ));
  258. return result;
  259. }
  260. // 2、导航栏开启的情况
  261. if (listStyle.length > 0) {
  262. // tabContorller 初始化
  263. if (null == _tabController || _tabController.length != listStyle.length) {
  264. _tabController = new TabController(length: listStyle.length, vsync: this);
  265. }
  266. result.add(Container(
  267. height: 40,
  268. width: double.infinity,
  269. color: HexColor.fromHex(data['bg_color']),
  270. child: TabBar(
  271. controller: _tabController,
  272. isScrollable: /*listStyle.length <= 5 ? false : */ true,
  273. labelColor: HexColor.fromHex(data['choose_text_color'] ?? '#FF4242'),
  274. labelStyle: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
  275. unselectedLabelColor: HexColor.fromHex(data['text_color'] ?? '#999999'),
  276. unselectedLabelStyle: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
  277. indicatorSize: TabBarIndicatorSize.label,
  278. indicator: MaterialIndicator(
  279. color: HexColor.fromHex(data['choose_color'] ?? '#FF4242'),
  280. bottomLeftRadius: 1.25,
  281. topLeftRadius: 1.25,
  282. topRightRadius: 1.25,
  283. bottomRightRadius: 1.25,
  284. height: 2.5,
  285. horizontalPadding: 5,
  286. ),
  287. tabs: listStyle.map((e) => Text(e['name'])).toList(),
  288. ),
  289. ));
  290. _isHasTabBar = true;
  291. // 最后添加TabBarView
  292. result.add(Expanded(
  293. child: TabBarView(
  294. controller: _tabController,
  295. children: _buildTabBarViewChildren(listStyle, model['mod_id']?.toString(), model['mod_pid']?.toString(), index),
  296. ),
  297. ));
  298. }
  299. return result;
  300. }
  301. /// 返回TabBarView的视图
  302. List<Widget> _buildTabBarViewChildren(final List<Map<String, dynamic>> listStyle, final String modId, final String modPid, final int index) {
  303. List<Widget> result = [];
  304. for (int i = 0; i < listStyle.length; i++) {
  305. result.add(CustomItemPage(listStyle[i], i, modId, modPid, (!_isHasAppbar && !_isHasTabBar && index == 0 )));
  306. }
  307. return result;
  308. }
  309. // /// 悬浮按钮
  310. // Widget _buildFloatWidget() {
  311. // return Visibility(
  312. // visible: true,
  313. // child: GestureDetector(
  314. // onTap: () => _scrollTop(),
  315. // behavior: HitTestBehavior.opaque,
  316. // child: Container(
  317. // height: 30,
  318. // width: 30,
  319. // child: Icon(Icons.arrow_upward),
  320. // ),
  321. // ),
  322. // );
  323. // }
  324. }
  325. /// 回到顶部的icon
  326. class _CustomFloatingActionButtonLocation extends FloatingActionButtonLocation {
  327. FloatingActionButtonLocation location;
  328. double offsetX; // X方向的偏移量
  329. double offsetY; // Y方向的偏移量
  330. _CustomFloatingActionButtonLocation(this.location, this.offsetX, this.offsetY);
  331. @override
  332. Offset getOffset(ScaffoldPrelayoutGeometry scaffoldGeometry) {
  333. Offset offset = location.getOffset(scaffoldGeometry);
  334. return Offset(offset.dx + offsetX, offset.dy + offsetY);
  335. }
  336. }