vb_app_bar.dart 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import 'package:chicken_farm/routes/app_routes.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:go_router/go_router.dart';
  4. class VberAppBar extends StatelessWidget implements PreferredSizeWidget {
  5. final String title;
  6. final bool showLeftButton;
  7. final List<Widget>? actions;
  8. final VoidCallback? onLeftPressed;
  9. final bool alwaysGoToHome;
  10. const VberAppBar({
  11. super.key,
  12. required this.title,
  13. this.showLeftButton = true,
  14. this.actions,
  15. this.onLeftPressed,
  16. this.alwaysGoToHome = false,
  17. });
  18. @override
  19. Widget build(BuildContext context) {
  20. return AppBar(
  21. title: Text(title),
  22. centerTitle: true,
  23. leading: showLeftButton
  24. ? IconButton(
  25. icon: Icon(alwaysGoToHome ? Icons.home : Icons.arrow_back),
  26. onPressed: () {
  27. if (onLeftPressed != null) {
  28. onLeftPressed!();
  29. } else if (alwaysGoToHome) {
  30. context.go(AppRouteNames.home);
  31. } else if (context.canPop()) {
  32. // 如果可以返回,则返回上一页
  33. context.pop();
  34. } else {
  35. context.go(AppRouteNames.home);
  36. }
  37. },
  38. )
  39. : null,
  40. actions: actions,
  41. );
  42. }
  43. @override
  44. Size get preferredSize => const Size.fromHeight(kToolbarHeight);
  45. }