base_view.dart 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. bottomNavigationBar: bottomNavigationBar,
  42. floatingActionButton: floatingActionButton,
  43. floatingActionButtonLocation: floatingActionButtonLocation,
  44. resizeToAvoidBottomInset: resizeToAvoidBottomInset,
  45. drawer: drawer,
  46. endDrawer: endDrawer,
  47. body: buildContent(context),
  48. );
  49. });
  50. if (isPopScope) {
  51. return PopScope(
  52. canPop: false,
  53. onPopInvokedWithResult: (didPop, result) => onPopInvoked(didPop),
  54. child: view,
  55. );
  56. }
  57. return view;
  58. }
  59. }