base_view.dart 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import '../../config/theme/light_theme_colors.dart';
  4. import '../../config/theme/theme_extensions/theme_extension.dart';
  5. import '../controllers/base_core_api.dart';
  6. abstract class BaseView<T> extends GetView<T> {
  7. const BaseView({super.key});
  8. // 子类可以重写这些属性来自定义视图
  9. PreferredSizeWidget? get appBar => null;
  10. Widget? get bottomNavigationBar => null;
  11. Widget? get floatingActionButton => null;
  12. FloatingActionButtonLocation? get floatingActionButtonLocation => null;
  13. bool get showBackground => true;
  14. Color? get backgroundColor => LightThemeColors.backgroundColor;
  15. bool get extendBody => false;
  16. bool get extendBodyBehindAppBar => false;
  17. bool get safeArea => false;
  18. EdgeInsets? get padding => null;
  19. bool get resizeToAvoidBottomInset => true;
  20. Widget? get drawer => null;
  21. Widget? get endDrawer => null;
  22. bool get isPopScope => false;
  23. // 添加这个方法来处理返回事件
  24. void onPopInvoked(bool didPop) {
  25. if (!didPop) {
  26. BaseCoreApi().moveTaskToBack();
  27. }
  28. }
  29. // 子类必须实现这个方法来提供视图内容
  30. Widget buildContent(BuildContext context);
  31. @override
  32. Widget build(BuildContext context) {
  33. // 使用 Obx 包装整个视图以响应主题变化
  34. Widget view = Obx(() {
  35. // 在 Obx 内部构建 content,确保使用 Get.reactiveTheme 的子视图能响应主题变化
  36. return Scaffold(
  37. backgroundColor: Get.reactiveTheme.scaffoldBackgroundColor,
  38. appBar: appBar,
  39. extendBody: extendBody,
  40. //extendBodyBehindAppBar: extendBodyBehindAppBar,
  41. extendBodyBehindAppBar: false,
  42. bottomNavigationBar: bottomNavigationBar,
  43. floatingActionButton: floatingActionButton,
  44. floatingActionButtonLocation: floatingActionButtonLocation,
  45. resizeToAvoidBottomInset: resizeToAvoidBottomInset,
  46. drawer: drawer,
  47. endDrawer: endDrawer,
  48. body: buildContent(context),
  49. );
  50. });
  51. if (isPopScope) {
  52. return PopScope(
  53. canPop: false,
  54. onPopInvokedWithResult: (didPop, result) => onPopInvoked(didPop),
  55. child: view,
  56. );
  57. }
  58. return view;
  59. }
  60. }