BasicPage.dart 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_screenutil/flutter_screenutil.dart';
  3. import 'package:get/get.dart';
  4. abstract class BasicPage extends StatefulWidget {
  5. // 导航标题
  6. // 用法 naviTitle = xxxx
  7. final _title = Rx<String>("");
  8. final isTabBar = Rx<bool>(false);
  9. final tabIndex =Rx<int>(0);
  10. set naviTitle(String t) {
  11. _title.value = t;
  12. }
  13. set handleTabbar(int index) {
  14. tabIndex.value=index;
  15. isTabBar.value = true;
  16. }
  17. // 允许滚动 - 不是响应式的
  18. bool canScroll;
  19. //
  20. BasicPage({Key? key, this.canScroll = true}) : super(key: key);
  21. @protected
  22. Widget builder(BuildContext context);
  23. // 将要展示
  24. @protected
  25. @mustCallSuper
  26. void beforeShow() {
  27. assert(() {
  28. print("===========beforeShow===========");
  29. return true;
  30. }());
  31. }
  32. @protected
  33. @mustCallSuper
  34. void beforeHide() {
  35. assert(() {
  36. print("===========beforeHide===========");
  37. return true;
  38. }());
  39. }
  40. @protected
  41. @mustCallSuper
  42. void beforeUnmounted() {
  43. assert(() {
  44. print("===========beforeUnmounted===========");
  45. return true;
  46. }());
  47. }
  48. @override
  49. State<BasicPage> createState() => _BasicPageState();
  50. }
  51. class _BasicPageState extends State<BasicPage> {
  52. @override
  53. void initState() {
  54. super.initState();
  55. }
  56. @override
  57. void didChangeDependencies() {
  58. super.didChangeDependencies();
  59. widget.beforeShow();
  60. }
  61. @override
  62. void deactivate() {
  63. super.deactivate();
  64. widget.beforeHide();
  65. }
  66. @override
  67. void dispose() {
  68. super.dispose();
  69. widget.beforeUnmounted();
  70. }
  71. // appBar 的样式请查询 theme 文件
  72. PreferredSizeWidget? _getAppBar() {
  73. return AppBar(title: Obx(() => Text(widget._title.value)));
  74. }
  75. Widget _buildChild(BuildContext context) {
  76. if (widget.canScroll) {
  77. return SingleChildScrollView(
  78. child: widget.builder(context),
  79. );
  80. } else {
  81. return widget.builder(context);
  82. }
  83. }
  84. final List _titles = ['首页', '订单', '资讯','我的'];
  85. final tabTextStyleSelected = TextStyle(
  86. color: const Color(0xFFFF703B),
  87. fontSize: 15.sp,
  88. fontWeight: FontWeight.bold); //选线卡选中字体颜色
  89. final tabTextStyleNormal = TextStyle(
  90. color: const Color(0xFF323232),
  91. fontSize: 15.sp,
  92. fontWeight: FontWeight.bold); //选项卡未选中字体颜色
  93. TextStyle getTabTextStyle(int curIndex) {
  94. //设置tabbar 选中和未选中的状态文本
  95. if (curIndex == widget.tabIndex.value) {
  96. return tabTextStyleSelected;
  97. }
  98. return tabTextStyleNormal;
  99. }
  100. // 切换底部选项卡,标题的变化设置
  101. Text getTabTitle(int curIndex) {
  102. return Text(_titles[curIndex], style: getTabTextStyle(curIndex));
  103. }
  104. List images = [
  105. ['images/index/HomesOFFImgaes.png', 'images/index/HomesNOImgaes.png'],
  106. ['images/index/OrdersOFFImgaes.png', 'images/index/OrdersNOImgaes.png'],
  107. ['images/index/newsONImages.png', 'images/index/newsOFFImages.png'],
  108. ['images/index/MineOFFImgaes.png', 'images/index/MineNOImgaes.png'],
  109. ];
  110. Image getTabIcon(int curIndex) {
  111. //设置tabbar选中和未选中的状态图标
  112. if (curIndex == widget.tabIndex.value) {
  113. return Image.asset(
  114. images[curIndex][1],
  115. width: 22.w,
  116. height: 22.w,
  117. );
  118. }
  119. return Image.asset(
  120. images[curIndex][0],
  121. width: 22.w,
  122. height: 22.w,
  123. );
  124. }
  125. Image getTabImage(path) {
  126. return Image.asset(path, width: 22.w, height: 22.w);
  127. }
  128. Widget tabBar(index) {
  129. return GestureDetector(
  130. behavior: HitTestBehavior.opaque,
  131. onTap: () {
  132. if(index!=widget.tabIndex.value){
  133. if(index==0){
  134. Get.offNamed('/');
  135. }else if(index==1){
  136. Get.offNamed('/order');
  137. }else if(index==2){
  138. Get.offNamed('/infomation');
  139. }else{
  140. Get.offNamed('/main');
  141. }
  142. }
  143. },
  144. child: Container(
  145. width: 93.w,
  146. height: 65.h,
  147. child: Column(
  148. children: [
  149. getTabIcon(index),
  150. Padding(
  151. padding: EdgeInsets.fromLTRB(0, 6.h, 0, 0),
  152. child: getTabTitle(index),
  153. )
  154. ],
  155. ),
  156. ),
  157. );
  158. }
  159. Widget? _bottomBar(){
  160. if(widget.isTabBar.value) {
  161. return Container(
  162. padding: EdgeInsets.fromLTRB(0, 7.h, 0, 7.h),
  163. decoration: BoxDecoration(
  164. color: const Color(0xFFFFFFFF),
  165. boxShadow: [
  166. BoxShadow(
  167. color: const Color(0x14000000),
  168. offset: Offset(0, -2.w),
  169. blurRadius: 3.w,
  170. ),
  171. ],
  172. ),
  173. child: Row(
  174. children:_titles.asMap().keys.map((index) =>tabBar(index)).toList(),
  175. ),
  176. );
  177. } else {
  178. return null;
  179. }
  180. }
  181. @override
  182. Widget build(BuildContext context) {
  183. return Scaffold(
  184. appBar: _getAppBar(),
  185. body: SafeArea(
  186. child: _buildChild(context),
  187. ),
  188. bottomNavigationBar: _bottomBar(),
  189. );
  190. }
  191. }