| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import '../../config/theme/light_theme_colors.dart';
- import '../../config/theme/theme_extensions/theme_extension.dart';
- import '../../pigeons/core_api.g.dart';
- abstract class BaseView<T> extends GetView<T> {
- const BaseView({super.key});
- // 子类可以重写这些属性来自定义视图
- PreferredSizeWidget? get appBar => null;
- Widget? get bottomNavigationBar => null;
- Widget? get floatingActionButton => null;
- FloatingActionButtonLocation? get floatingActionButtonLocation => null;
- bool get showBackground => true;
- Color? get backgroundColor => LightThemeColors.backgroundColor;
- bool get extendBody => false;
- bool get extendBodyBehindAppBar => false;
- bool get safeArea => true;
- EdgeInsets? get padding => null;
- bool get resizeToAvoidBottomInset => true;
- Widget? get drawer => null;
- Widget? get endDrawer => null;
- bool get isPopScope => false;
- // 添加这个方法来处理返回事件
- void onPopInvoked(bool didPop) {
- if (!didPop) {
- CoreApi().moveTaskToBack();
- }
- }
- // 子类必须实现这个方法来提供视图内容
- Widget buildContent(BuildContext context);
- @override
- Widget build(BuildContext context) {
- // 使用 Obx 包装整个视图以响应主题变化
- Widget view = Obx(() {
- // 在 Obx 内部构建 content,确保使用 Get.reactiveTheme 的子视图能响应主题变化
- Widget content = padding != null
- ? safeArea
- ? SafeArea(
- child: Padding(
- padding: padding!,
- child: buildContent(context),
- ),
- )
- : Padding(padding: padding!, child: buildContent(context))
- : safeArea
- ? SafeArea(child: buildContent(context))
- : buildContent(context);
- return Scaffold(
- backgroundColor: Get.reactiveTheme.scaffoldBackgroundColor,
- appBar: appBar,
- extendBody: extendBody,
- extendBodyBehindAppBar: extendBodyBehindAppBar,
- bottomNavigationBar: bottomNavigationBar,
- floatingActionButton: floatingActionButton,
- floatingActionButtonLocation: floatingActionButtonLocation,
- resizeToAvoidBottomInset: resizeToAvoidBottomInset,
- drawer: drawer,
- endDrawer: endDrawer,
- body: content,
- );
- });
- if (isPopScope) {
- return PopScope(
- canPop: false,
- onPopInvokedWithResult: (didPop, result) => onPopInvoked(didPop),
- child: view,
- );
- }
- return view;
- }
- }
|