import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'empty_controller.dart';
import 'global_key_factory.dart';


/*
 * 页面的基类，StatefulWidget 类型
 * 只用于路由中定义的页面，如果是自定义布局或者PageView的子页面则无需继承
 * 如果没有传Key,则自动管理Key
 */
abstract class BaseStatefulPage<C extends GetxController> extends StatefulWidget {
  BaseStatefulPage({Key? key}) : super(key: key ?? GlobalKeyFactory.createGlobalKey()) {
    _createController();
  }

  C? _controller;

  C get controller => _controller ?? EmptyController() as C ;

  void _createController() {
    try {
      _controller = GetInstance().put<C>(createRawController(), tag: key?.toString());
    } catch (e) {
      e.printError();
      _controller = null;
    }
  }

  C createRawController();
}
