
import 'package:farmer_client/widgets/layout/bottomBar/BottomBar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'barList.dart';

abstract class BasicPage extends StatefulWidget {
  // 导航标题
  // 用法 naviTitle = xxxx
  final _title = Rx<String>("");
  set naviTitle(String t) {
    _title.value = t;
  }

  // 当前活动 tab
  // 如果非 tab 页, 则不传
  int? tabIndex;

  // 允许滚动 - 不是响应式的
  bool canScroll;

  //
  BasicPage({Key? key, this.canScroll = true, this.tabIndex }) : super(key: key);

  @protected
  Widget builder(BuildContext context);

  // 将要展示
  @protected
  @mustCallSuper
  void beforeShow() {
    assert(() {
      print("===========beforeShow===========");
      return true;
    }());
  }

  @protected
  @mustCallSuper
  void beforeHide() {
    assert(() {
      print("===========beforeHide===========");
      return true;
    }());
  }

  @protected
  @mustCallSuper
  void beforeUnmounted() {
    assert(() {
      print("===========beforeUnmounted===========");
      return true;
    }());
  }

  @override
  State<BasicPage> createState() => _BasicPageState();
}

class _BasicPageState extends State<BasicPage> {

  @override
  void initState() {
    super.initState();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    widget.beforeShow();
  }

  @override
  void deactivate() {
    super.deactivate();
    widget.beforeHide();
  }

  @override
  void dispose() {
    super.dispose();
    widget.beforeUnmounted();
  }

  // appBar 的样式请查询 theme 文件
  PreferredSizeWidget? _getAppBar() {
    return AppBar(title: Obx(() => Text(widget._title.value)));
  }

  Widget _buildChild(BuildContext context) {
    if (widget.canScroll) {
      return SingleChildScrollView(
        child: widget.builder(context),
      );
    } else {
      return widget.builder(context);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _getAppBar(),
      body: SafeArea(
        child: _buildChild(context),
      ),
      bottomNavigationBar: widget.tabIndex == null ? null : BottomBar(list: BarList, current: widget.tabIndex!),
    );
  }
}

