map_appbar.dart 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import 'package:flutter/material.dart';
  2. class BMFAppBar extends StatelessWidget implements PreferredSizeWidget {
  3. const BMFAppBar({
  4. Key? key,
  5. this.title,
  6. this.titleStyle,
  7. this.backgroundColor,
  8. this.isBack = true,
  9. this.onBack,
  10. this.actions,
  11. this.bottom,
  12. }) : super(key: key);
  13. /// 标题
  14. final String? title;
  15. /// 标题Style
  16. final TextStyle? titleStyle;
  17. /// 背景色
  18. final Color? backgroundColor;
  19. /// 是否展示返回按钮
  20. final bool isBack;
  21. /// 返回按钮点击回调
  22. final VoidCallback? onBack;
  23. /// actions
  24. final List<Widget>? actions;
  25. /// 底部widget
  26. final PreferredSizeWidget? bottom;
  27. @override
  28. Size get preferredSize =>
  29. Size.fromHeight(kToolbarHeight + (bottom?.preferredSize.height ?? 0.0));
  30. @override
  31. Widget build(BuildContext context) {
  32. return AppBar(
  33. title: Text(
  34. title ?? 'title',
  35. style: titleStyle ??
  36. const TextStyle(
  37. color: Colors.white,
  38. fontSize: 17.0,
  39. fontWeight: FontWeight.w600,
  40. ),
  41. ),
  42. leading: isBack
  43. ? IconButton(
  44. icon: const Icon(Icons.arrow_back),
  45. onPressed: onBack != null
  46. ? () => onBack
  47. : () => onBackDefault(context))
  48. : null,
  49. actions: actions,
  50. backgroundColor:
  51. backgroundColor ?? Theme.of(context).appBarTheme.backgroundColor,
  52. elevation: 0,
  53. bottom: BMFAppBarBottom(child: bottom),
  54. );
  55. }
  56. void onBackDefault(BuildContext context) => Navigator.pop(context);
  57. }
  58. class BMFAppBarBottom extends StatelessWidget implements PreferredSizeWidget {
  59. const BMFAppBarBottom({Key? key, this.child}) : super(key: key);
  60. final Widget? child;
  61. @override
  62. Widget build(BuildContext context) {
  63. return Column(children: <Widget>[
  64. Container(
  65. width: double.infinity,
  66. height: 0,
  67. color: Colors.white,
  68. ),
  69. child ?? const SizedBox(height: 0),
  70. ]);
  71. }
  72. @override
  73. Size get preferredSize => Size.fromHeight(child != null ? 47 : 1);
  74. }