flutter tab 与pageview的联动使用

 简介: 这个案例用来作为侧边分类列表来制作,使用tab控制pageview的页面,使用了animateToPage有一个动画效果

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

class CategoryScreen extends StatefulWidget {
  const CategoryScreen({Key? key}) : super(key: key);

  @override
  State<CategoryScreen> createState() => _CategoryScreenState();
}

class _CategoryScreenState extends State<CategoryScreen> with SingleTickerProviderStateMixin{
  late TabController _tabController;
  late PageController _pageController;
  List _list = [];
  @override
  void initState() {
    for(int i = 0; i < 4; i++){
      _list.add("TabItem $i");
    }
    _pageController = PageController();
    _tabController = TabController(length: _list.length, vsync: this);
    super.initState();
  }
  int currentIndex = 0;
  @override
  Widget build(BuildContext context) {
    final Size size = MediaQuery.of(context).size;
    final double topPadding = MediaQuery.of(context).padding.top;

    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: Container(
        height: double.infinity,
        width: double.infinity,
        child: Column(
          children: [
            SizedBox(height: topPadding),
            Container(
              height: (size.height - topPadding),
              width: size.width,
              color: Colors.grey.withOpacity(0.2),
              child: Row(
                children: [
                  Container(
                    width: 80,
                    padding: EdgeInsets.only(right: 10),
                    decoration:BoxDecoration(
                      color:Colors.white,
                      borderRadius: BorderRadius.only(
                        topRight: Radius.circular(10),
                        bottomRight: Radius.circular(10)
                      )
                    ),
                    alignment: Alignment.topLeft,
                    child: RotatedBox(
                      quarterTurns: 1,
                      child: TabBar(
                        onTap: (value){
                          currentIndex = value;
                          // _pageController.jumpToPage(currentIndex);
                          _pageController.animateToPage(currentIndex, duration: Duration(milliseconds: 500), curve: Curves.easeInOut);
                          setState(() {
                          });
                        },
                        indicatorWeight: 5,
                        isScrollable: true,
                        controller: _tabController,
                        labelColor: Colors.black,
                        unselectedLabelColor: Colors.grey,
                        tabs: List.generate(_list.length, (index){
                          return RotatedBox(
                            quarterTurns: 3,
                            child: Tab(
                              child: Text(_list[index]),
                            ),
                          );
                        }),
                      ),
                    ),
                  ),
                  Container(
                      height: (size.height - topPadding),
                      width: 300,
                      child: PageView.builder(
                        scrollDirection: Axis.vertical,
                        itemCount: _list.length,
                        onPageChanged: (value){
                          _tabController.animateTo(value);
                          setState(() {
                          });
                        },
                        itemBuilder: (_,index){
                          return Container(
                            width: 150,
                            height: 100,
                            color: Colors.blue[index % 9 * 100],
                          );
                        },
                        controller: _pageController,
                      )
                  )
                ],
              )
            )
          ],
        ),
      ),
    );
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值