my_appbar.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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: IconButton(
  26. icon: MyAssetImage(backIconPath ?? Assets.baseLibWhiteBack, width: backIconWidth ?? 11, height: backIconHeight ?? 18),
  27. onPressed: () {
  28. if (backCallback != null) {
  29. backCallback();
  30. } else {
  31. Get.back();
  32. // Navigator.pop(context);
  33. }
  34. },
  35. ),
  36. centerTitle: true,
  37. title: Text(
  38. title,
  39. style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.w500),
  40. ),
  41. actions: actions,
  42. elevation: 0.0,
  43. bottom: PreferredSize(
  44. preferredSize: const Size.fromHeight(0.5),
  45. child: Divider(
  46. height: 0.5,
  47. indent: 0.0,
  48. color: ColorConstants.dividerBar,
  49. )),
  50. );
  51. }
  52. //自定义高度快
  53. static SizedBox rowHeight({double height = 30}) {
  54. return SizedBox(height: height);
  55. }
  56. //自定义宽度快
  57. static SizedBox rowWidth({double width = 30}) {
  58. return SizedBox(width: width);
  59. }
  60. /// 带搜索的AppBar
  61. static AppBar appSearchBar(BuildContext context,
  62. {void Function()? backCallback,
  63. String? value,
  64. String? hintText,
  65. Color textColor = Colors.white,
  66. Color backgroundColor = Colors.transparent,
  67. ValueChanged<String>? onSearch,
  68. ValueChanged<String>? onChanged,
  69. SystemUiOverlayStyle? systemUiOverlayStyle,
  70. TextEditingController? controller,
  71. List<Widget>? actions}) {
  72. return AppBar(
  73. backgroundColor: backgroundColor,
  74. surfaceTintColor: backgroundColor,
  75. systemOverlayStyle: systemUiOverlayStyle,
  76. // 标题与其他控件的间隔
  77. titleSpacing: 0,
  78. leading: IconButton(
  79. icon: MyAssetImage(Assets.baseLibWhiteBack, width: 11, height: 18),
  80. onPressed: () {
  81. if (backCallback != null) {
  82. backCallback();
  83. } else {
  84. Get.back();
  85. // Navigator.pop(context);
  86. }
  87. },
  88. ),
  89. centerTitle: true,
  90. title: SearchAppBar(
  91. onSearch: onSearch,
  92. onChanged: onChanged,
  93. value: value,
  94. hintText: hintText,
  95. controller: controller,
  96. ),
  97. actions: actions,
  98. elevation: 0.0,
  99. bottom: PreferredSize(
  100. preferredSize: const Size.fromHeight(0.5),
  101. child: Divider(
  102. height: 0.5,
  103. indent: 0.0,
  104. color: ColorConstants.dividerBar,
  105. )),
  106. );
  107. }
  108. }