简体   繁体   English

如何在Flutter中实现此类自定义标签设计? [等候接听]

[英]How can i achieve this type of custom tab design in flutter? [on hold]

我正在尝试像下面这样在抖动中进行设计。

I am trying to design like this image in flutter. 我试图像这样的图像设计扑朔迷离。

There are some basic properties provided in the TabBar but the are not enough in your use case. TabBar提供了一些基本属性,但在您的用例中还不够。 You have to create a TabController and an index variable. 您必须创建一个TabController和一个索引变量。 Whenever the switch happened TabController have to set the index & background of the tab is needs to be set according to that index. 每当切换发生时, TabController必须设置索引,并且必须根据该索引设置标签的背景。

Editing my code for more optimise answer with border radius 编辑我的代码以使用边界半径更优化答案

Example Code 范例程式码


class _TabDemoState extends State<TabDemo> with SingleTickerProviderStateMixin {

  TabController _tabController;

  int _selectedTab = 0;

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

    _tabController = TabController(vsync: this, length: 3);

    _tabController.addListener((){
      if (!_tabController.indexIsChanging){
        setState(() {
          _selectedTab = _tabController.index; 
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Tab Demo"),
      ),
      backgroundColor: Colors.white,
      body: DefaultTabController(
          length: 3,
          child: Column(
            children: <Widget>[
              Material(
                color: Colors.grey.shade300,
                child: TabBar(
                  unselectedLabelColor: Colors.blue,
                  labelColor: Colors.blue,
                  indicatorColor: Colors.white,
                  controller: _tabController,
                  labelPadding: const EdgeInsets.all(0.0),
                  tabs: [
                    _getTab(0, Icon(Icons.directions_car)),
                    _getTab(1, Icon(Icons.directions_transit)),
                    _getTab(2, Icon(Icons.directions_bike)),
                  ],
                ),
              ),
              Expanded(
                child: TabBarView(
                  physics: NeverScrollableScrollPhysics(),
                  controller: _tabController,
                  children: [
                    Icon(Icons.directions_car),
                    Icon(Icons.directions_transit),
                    Icon(Icons.directions_bike),
                  ],
                ),
              ),
            ],
          )),
    );
  }


  _getTab(index, child) {
    return Tab(
      child: SizedBox.expand(
        child: Container(
          child: child,
          decoration: BoxDecoration(
              color:
                  (_selectedTab == index ? Colors.white : Colors.grey.shade300),
              borderRadius: _generateBorderRadius(index)),
        ),
      ),
    );
  }

  _generateBorderRadius(index) {
    if ((index + 1) == _selectedTab)
      return BorderRadius.only(bottomRight: Radius.circular(10.0));
    else if ((index - 1) == _selectedTab)
      return BorderRadius.only(bottomLeft: Radius.circular(10.0));
    else
      return BorderRadius.zero;
  }
}

Note - In this I have faced an issue. 注意 -在此我遇到了一个问题。 Its when you fast swipe on from left to right that _tabContoller values returns two values first it gives you index-2 value than index-1 value which is expected. 当您从左向右快速滑动时, _tabContoller值首先返回两个值,它会为您提供index-2值,而不是预期的index-1值。 I don't know why this is happening but to resolve this issue I have to disable the scroll on TabBarView . 我不知道为什么会这样,但是要解决此问题,我必须禁用TabBarView上的滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM