/*
 * @Author: XianKaiQun
 * @Date: 2020-04-27 16:10:18
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-04-03 17:26:55
 * @Descripttion:
 */
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class WisTabPage extends StatefulWidget {
  const WisTabPage({
    Key? key,
    this.initialIndex = 0,
    this.isScrollable = false,
    this.isTabWidth = false,
    this.indicatorPadding,
    this.indicatorWeight,
    this.indicator,
    this.labelPadding,
    this.labelStyle,
    this.unselectedLabelStyle,
    this.color = Colors.white,
    this.divider = true,
    required this.tabs,
    required this.views,
    this.onChange,
    this.isMarginBottom = true,
  })  : assert((tabs.length) != 0, 'tabs数量不能为0'),
        assert((views.length) != 0, 'views数量不能为0'),
        assert(tabs.length == views.length, 'tabs数量必须跟views数量相等'),
        super(key: key);

  ///初始化的下标
  final int initialIndex;
  final List<Widget> tabs;
  final List<Widget> views;

  ///tabbar是否能够滚动
  final bool isScrollable;
  final bool isTabWidth;

  final EdgeInsetsGeometry? indicatorPadding;
  final double? indicatorWeight;
  final Decoration? indicator;
  final EdgeInsetsGeometry? labelPadding;
  final TextStyle? unselectedLabelStyle;
  final Color? color;
  final bool? divider;
  final TextStyle? labelStyle;

  //底部是否需要margin，默认需要
  final bool isMarginBottom;

  ///回调
  final Function(int? index)? onChange;
  @override
  _WisTabPageState createState() => _WisTabPageState();
}

class _WisTabPageState extends State<WisTabPage>
    with SingleTickerProviderStateMixin {
  TabController? _controller;
  int? index;

  int get length => widget.tabs.length;
  @override
  void initState() {
    super.initState();
    index = widget.initialIndex;
    _controller = TabController(
      vsync: this,
      length: length,
      initialIndex: widget.initialIndex,
    );
    _controller!.addListener(() {
      if (_controller!.index != index) {
        index = _controller!.index;
        if (widget.onChange != null) {
          widget.onChange!(index);
        }
      }
    });
  }

  @override
  void dispose() {
    _controller!.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
        Container(
          color: widget.color ?? Colors.white,
          padding: widget.isMarginBottom
              ? EdgeInsets.zero
              : EdgeInsets.only(bottom: 8.pt),
          child: TabBar(
            controller: _controller,
            labelColor: colorScheme.text,
            isScrollable: widget.isScrollable,
            indicatorColor: colorScheme.primary,
            unselectedLabelColor: colorScheme.minor,
            indicatorSize: widget.isTabWidth
                ? TabBarIndicatorSize.tab
                : TabBarIndicatorSize.label,
            indicatorPadding: widget.indicatorPadding ?? EdgeInsets.zero,
            indicatorWeight: widget.indicatorWeight ?? 2.0,
            labelPadding:
                widget.labelPadding ?? EdgeInsets.symmetric(vertical: 10.pt),
            indicator: widget.indicator ??
                RoundUnderlineTabIndicator(
                  width: 25.pt,
                ),
            labelStyle: widget.labelStyle ??
                TextStyle(
                  fontWeight: FontWeight.w600,
                  fontSize: 18.pt,
                ),
            unselectedLabelStyle: widget.unselectedLabelStyle ??
                TextStyle(
                  fontWeight: FontWeight.normal,
                  fontSize: 16.pt,
                ),
            tabs: [
              ...widget.tabs,
            ],
            dividerHeight: 0,
            dividerColor: Colors.transparent,
          ),
        ),
        if (widget.divider == true)
          Divider(
            color: colorScheme.divider2,
          ),
        TabBarView(
          controller: _controller,
          children: [
            ...widget.views,
          ],
        ).asExpanded(),
      ],
    );
  }
}
