
import 'package:flutter/material.dart';
import 'package:twong/widgets/app_bar.dart';

class OrderList extends StatefulWidget {
  final int index;
  OrderList({Key key, this.index}): super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _OrderListState(this.index == null ? 0 : this.index);
  }
}

class _OrderListState extends State<OrderList> with SingleTickerProviderStateMixin {
  final int index;
  TabController tabController;
  List<Widget> tabs = List<Widget>();

  _OrderListState(this.index);

  @override
  void initState() {
    super.initState();
    tabs = <Widget>[
      Tab(text: '全部'),
      Tab(text: '待付款'),
      Tab(text: '待发货'),
      Tab(text: '待收货'),
      Tab(text: '待评价'),
    ];
    this.tabController = TabController(length: tabs.length, vsync: this, initialIndex: this.index);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: DAppBar("全部订单"),
      body: SafeArea(
        child: CustomScrollView(
          physics: ClampingScrollPhysics(),
          slivers: <Widget>[
            SliverPersistentHeader(
              pinned: true,
              delegate: _StickyTabBarDelegate(
                child: TabBar(
                  labelColor: Colors.black,
                  controller: this.tabController,
                  tabs: tabs,
                ),
              ),
            ),
            SliverFillRemaining(
              child: TabBarView(
                controller: this.tabController,
                children: <Widget>[
                  Center(child: Text('全部订单列表')),
                  Center(child: Text('待付款列表')),
                  Center(child: Text('待发货列表')),
                  Center(child: Text('待收货列表')),
                  Center(child: Text('待评价列表')),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class _StickyTabBarDelegate extends SliverPersistentHeaderDelegate {
  final TabBar child;
  _StickyTabBarDelegate({@required this.child});

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) => child;
  @override
  double get maxExtent => this.child.preferredSize.height;
  @override
  double get minExtent => this.child.preferredSize.height;
  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) => true;
}