my_appbar.dart 3.8 KB

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