base_stateful_page.dart 851 B

1234567891011121314151617181920212223242526272829303132
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import 'empty_controller.dart';
  4. import 'global_key_factory.dart';
  5. /*
  6. * 页面的基类,StatefulWidget 类型
  7. * 只用于路由中定义的页面,如果是自定义布局或者PageView的子页面则无需继承
  8. * 如果没有传Key,则自动管理Key
  9. */
  10. abstract class BaseStatefulPage<C extends GetxController> extends StatefulWidget {
  11. BaseStatefulPage({Key? key}) : super(key: key ?? GlobalKeyFactory.createGlobalKey()) {
  12. _createController();
  13. }
  14. C? _controller;
  15. C get controller => _controller ?? EmptyController() as C ;
  16. void _createController() {
  17. try {
  18. _controller = GetInstance().put<C>(createRawController(), tag: key?.toString());
  19. } catch (e) {
  20. e.printError();
  21. _controller = null;
  22. }
  23. }
  24. C createRawController();
  25. }