my_appbar.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:get/get.dart';
  4. import 'package:cs_resources/generated/assets.dart';
  5. import 'package:cs_resources/constants/color_constants.dart';
  6. import 'my_load_image.dart';
  7. import 'search_app_bar.dart';
  8. import 'utils/dark_theme_util.dart';
  9. class MyAppBar {
  10. ///封装的默认的AppBar,Light默认模式下,黑色图标+黑色文本,Dark模式下,白色图标+白色文本
  11. static AppBar appBar(BuildContext context, String title,
  12. {void Function()? backCallback,
  13. String? backIconPath,
  14. double? backIconWidth,
  15. double? backIconHeight,
  16. String? subTitle,
  17. Color? subTitleColor,
  18. Color textColor = Colors.white,
  19. Color backgroundColor = Colors.transparent,
  20. SystemUiOverlayStyle? systemUiOverlayStyle,
  21. List<Widget>? actions}) {
  22. return AppBar(
  23. //设置变色与背景颜色一致,避免滚动的时候AppBar变色
  24. backgroundColor: backgroundColor,
  25. surfaceTintColor: backgroundColor,
  26. systemOverlayStyle: systemUiOverlayStyle,
  27. leading: IconButton(
  28. icon: MyAssetImage(backIconPath ?? Assets.baseLibWhiteBack, width: backIconWidth ?? 11, height: backIconHeight ?? 18),
  29. onPressed: () {
  30. if (backCallback != null) {
  31. backCallback();
  32. } else {
  33. Get.back();
  34. // Navigator.pop(context);
  35. }
  36. },
  37. ),
  38. centerTitle: true,
  39. title: subTitle == null
  40. ? Text(
  41. title,
  42. style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.w500),
  43. )
  44. : Column(
  45. mainAxisSize: MainAxisSize.max,
  46. mainAxisAlignment: MainAxisAlignment.center,
  47. crossAxisAlignment: CrossAxisAlignment.center,
  48. children: [
  49. Text(
  50. title,
  51. style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.w500),
  52. ),
  53. Text(
  54. subTitle,
  55. style: TextStyle(color: subTitleColor ?? Colors.white, fontSize: 15, fontWeight: FontWeight.w400),
  56. )
  57. ],
  58. ),
  59. actions: actions,
  60. elevation: 0.0,
  61. bottom: PreferredSize(
  62. preferredSize: const Size.fromHeight(0.5),
  63. child: Divider(
  64. height: 0.5,
  65. indent: 0.0,
  66. color: ColorConstants.dividerBar,
  67. )),
  68. );
  69. }
  70. //自定义高度快
  71. static SizedBox rowHeight({double height = 30}) {
  72. return SizedBox(height: height);
  73. }
  74. //自定义宽度快
  75. static SizedBox rowWidth({double width = 30}) {
  76. return SizedBox(width: width);
  77. }
  78. /// 带搜索的AppBar
  79. static AppBar appSearchBar(BuildContext context,
  80. {void Function()? backCallback,
  81. String? value,
  82. String? hintText,
  83. Color textColor = Colors.white,
  84. Color backgroundColor = Colors.transparent,
  85. ValueChanged<String>? onSearch,
  86. ValueChanged<String>? onChanged,
  87. SystemUiOverlayStyle? systemUiOverlayStyle,
  88. TextEditingController? controller,
  89. List<Widget>? actions}) {
  90. return AppBar(
  91. backgroundColor: backgroundColor,
  92. surfaceTintColor: backgroundColor,
  93. systemOverlayStyle: systemUiOverlayStyle,
  94. // 标题与其他控件的间隔
  95. titleSpacing: 0,
  96. leading: IconButton(
  97. icon: MyAssetImage(Assets.baseLibWhiteBack, width: 11, height: 18),
  98. onPressed: () {
  99. if (backCallback != null) {
  100. backCallback();
  101. } else {
  102. Get.back();
  103. // Navigator.pop(context);
  104. }
  105. },
  106. ),
  107. centerTitle: true,
  108. title: SearchAppBar(
  109. onSearch: onSearch,
  110. onChanged: onChanged,
  111. value: value,
  112. hintText: hintText,
  113. controller: controller,
  114. ),
  115. actions: actions,
  116. elevation: 0.0,
  117. bottom: PreferredSize(
  118. preferredSize: const Size.fromHeight(0.5),
  119. child: Divider(
  120. height: 0.5,
  121. indent: 0.0,
  122. color: ColorConstants.dividerBar,
  123. )),
  124. );
  125. }
  126. }