my_appbar.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. Color textColor = Colors.black,
  17. Color backgroundColor = Colors.white,
  18. SystemUiOverlayStyle? systemUiOverlayStyle,
  19. List<Widget>? actions}) {
  20. return AppBar(
  21. //设置变色与背景颜色一致,避免滚动的时候AppBar变色
  22. backgroundColor: DarkThemeUtil.multiColors(backgroundColor, darkColor: ColorConstants.darkBlackItem),
  23. surfaceTintColor: DarkThemeUtil.multiColors(backgroundColor, darkColor: ColorConstants.darkBlackItem),
  24. systemOverlayStyle: systemUiOverlayStyle,
  25. leading: Container(
  26. padding: const EdgeInsets.only(top: 3.5),
  27. child: IconButton(
  28. icon: Get.isDarkMode
  29. ? MyAssetImage(backIconPath ?? Assets.baseLibWhiteBack, width: backIconWidth ?? 11, height: backIconHeight ?? 18)
  30. : MyAssetImage(backIconPath ?? Assets.baseLibBlackBack, width: backIconWidth ?? 11, height: backIconHeight ?? 18),
  31. onPressed: () {
  32. if (backCallback != null) {
  33. backCallback();
  34. } else {
  35. Get.back();
  36. // Navigator.pop(context);
  37. }
  38. },
  39. ),
  40. ),
  41. centerTitle: true,
  42. title: Text(
  43. title,
  44. style: TextStyle(color: Get.isDarkMode ? Colors.white : textColor, fontSize: 18, fontWeight: FontWeight.w600),
  45. ),
  46. actions: actions,
  47. elevation: 0.0,
  48. bottom: PreferredSize(
  49. preferredSize: const Size.fromHeight(0.5),
  50. child: Divider(
  51. height: 0.5,
  52. indent: 0.0,
  53. color: DarkThemeUtil.multiColors(ColorConstants.dividerA6, darkColor: ColorConstants.darkBlackItemDivider),
  54. )),
  55. );
  56. }
  57. //自定义高度快
  58. static SizedBox rowHeight({double height = 30}) {
  59. return SizedBox(height: height);
  60. }
  61. //自定义宽度快
  62. static SizedBox rowWidth({double width = 30}) {
  63. return SizedBox(width: width);
  64. }
  65. /// 带搜索的AppBar
  66. static AppBar appSearchBar(BuildContext context,
  67. {void Function()? backCallback,
  68. String? backPath,
  69. Color textColor = Colors.black,
  70. Color backgroundColor = Colors.white,
  71. ValueChanged<String>? onSearch,
  72. ValueChanged<String>? onChanged,
  73. VoidCallback? onClear,
  74. List<Widget>? actions}) {
  75. return AppBar(
  76. backgroundColor: DarkThemeUtil.multiColors(backgroundColor, darkColor: ColorConstants.darkBlackItem),
  77. surfaceTintColor: DarkThemeUtil.multiColors(backgroundColor, darkColor: ColorConstants.darkBlackItem),
  78. // 标题与其他控件的间隔
  79. titleSpacing: 0,
  80. leadingWidth: 40,
  81. leading: Container(
  82. padding: const EdgeInsets.only(top: 3.5),
  83. child: IconButton(
  84. icon: Get.isDarkMode
  85. ? MyAssetImage(backPath ?? Assets.baseLibWhiteBack, width: 11, height: 18)
  86. : MyAssetImage(backPath ?? Assets.baseLibBlackBack, width: 11, height: 18),
  87. onPressed: () {
  88. if (backCallback != null) {
  89. backCallback();
  90. } else {
  91. Get.back();
  92. // Navigator.pop(context);
  93. }
  94. },
  95. ),
  96. ),
  97. centerTitle: true,
  98. title: SearchAppBar(
  99. onSearch: onSearch,
  100. onClear: onClear,
  101. onChanged: onChanged,
  102. borderRadius: 13,
  103. height: 28,
  104. ),
  105. actions: actions,
  106. elevation: 0.0,
  107. bottom: PreferredSize(
  108. preferredSize: const Size.fromHeight(0.5),
  109. child: Divider(
  110. height: 1.0,
  111. indent: 0.0,
  112. color: DarkThemeUtil.multiColors(ColorConstants.dividerA6, darkColor: ColorConstants.darkBlackItemDivider),
  113. )),
  114. );
  115. }
  116. }