base_view.dart 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 '../../pigeons/core_api.g.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 => true;
  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. CoreApi().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. Widget content = padding != null
  37. ? safeArea
  38. ? SafeArea(
  39. child: Padding(
  40. padding: padding!,
  41. child: buildContent(context),
  42. ),
  43. )
  44. : Padding(padding: padding!, child: buildContent(context))
  45. : safeArea
  46. ? SafeArea(child: buildContent(context))
  47. : buildContent(context);
  48. return Scaffold(
  49. backgroundColor: Get.reactiveTheme.scaffoldBackgroundColor,
  50. appBar: appBar,
  51. extendBody: extendBody,
  52. extendBodyBehindAppBar: extendBodyBehindAppBar,
  53. bottomNavigationBar: bottomNavigationBar,
  54. floatingActionButton: floatingActionButton,
  55. floatingActionButtonLocation: floatingActionButtonLocation,
  56. resizeToAvoidBottomInset: resizeToAvoidBottomInset,
  57. drawer: drawer,
  58. endDrawer: endDrawer,
  59. body: content,
  60. );
  61. });
  62. if (isPopScope) {
  63. return PopScope(
  64. canPop: false,
  65. onPopInvokedWithResult: (didPop, result) => onPopInvoked(didPop),
  66. child: view,
  67. );
  68. }
  69. return view;
  70. }
  71. }