import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/pages/main_page/main_page.dart';

class MinePage extends StatefulWidget {
  @override
  _MinePageState createState() => _MinePageState();
}

class _MinePageState extends State<MinePage> with TickerProviderStateMixin {
  TabController _tabController;
  Map<String, dynamic> _datas = Map();

  @override
  void initState() {
    _tabController = TabController(length: 3, vsync: this);
    // 模拟数据
    List<Map<String, dynamic>> list = List();
    list.add(
        Map<String, dynamic>.from({'mod_name': 'index_search', 'mod_id': 5}));
    list.add(
        Map<String, dynamic>.from({'mod_name': 'index_carousel', 'mod_id': 6}));
    list.add(Map<String, dynamic>.from({'mod_name': 'multi_nav', 'mod_id': 7}));
    list.add(Map<String, dynamic>.from(
        {'mod_name': 'index_banner_one', 'mod_id': 8}));
    list.add(Map<String, dynamic>.from(
        {'mod_name': 'index_banner_two', 'mod_id': 9}));
    list.add(Map<String, dynamic>.from(
        {'mod_name': 'index_recommend_list', 'mod_id': 10}));
    _datas['components'] = list;
    super.initState();
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> _silverBuilder(BuildContext context, bool innerBoxIsScrolled) {
      return <Widget>[
        SliverAppBar(),
        SliverPersistentHeader(
          pinned: true,
          delegate: _SilverAppBarDelegate(
            Container(
              color: Colors.amber,
              child: TabBar(
                labelColor: Colors.blue,
                unselectedLabelColor: Colors.black,
                controller: _tabController,
                tabs: <Widget>[
                  new Tab(
                    text: "tab10",
                  ),
                  new Tab(
                    text: "tab2",
                  ),
                  new Tab(
                    text: "tab3",
                  ),
                ],
              ),
            ),
          ),
        ),
      ];
    }

    return Scaffold(
      body: SafeArea(
          child: NestedScrollView(
        headerSliverBuilder: _silverBuilder,

        body: TabBarView(
          controller: _tabController,
          children: <Widget>[
            MainPage(_datas),
            ListView.builder(
                itemCount: 50,
                itemBuilder: (context, count) {
                  return Container(
                    height: 50,
                    color: Colors.primaries[count % Colors.primaries.length],
                    child: Text('aaaaa ${count.toString()}'),
                  );
                }),
            MainPage(_datas),
          ],
        ),
      )),
    );
  }
}

class _SilverAppBarDelegate extends SliverPersistentHeaderDelegate {
  _SilverAppBarDelegate(this.child);

  final Widget child;

  @override
  double get minExtent => 44;

  @override
  double get maxExtent => 44;

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return new Container(
      child: child,
    );
  }

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return false;
  }
}
