【问题标题】:How to change the shape of Tabs() when TabBarView() is swiped in Flutter?Flutter中滑动TabBarView()时如何改变Tabs()的形状?
【发布时间】:2021-08-16 04:00:29
【问题描述】:

我一直在尝试改变TabBar()Tabs() 的形状,我使用TabBar() 中的onTap:(){} 属性做到了。

但问题是我在TabBarView() 中滑动页面时想要相同的功能,所以我也尝试这样做,但我做不到。

当我在TabBarView() 中滑动页面时,我想改变Tabs() 的形状。

下面是我的代码实现:

import 'package:flutter/material.dart';

class Demo extends StatefulWidget with PreferredSizeWidget {
  @override
  _DemoState createState() => _DemoState();

  @override
  Size get preferredSize => AppBar().preferredSize;
}

class _DemoState extends State<Demo> with SingleTickerProviderStateMixin {
  late TabController _tabController;
  late int _index;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 3, vsync: this, initialIndex: 1);
  }

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

  @override
  Widget build(BuildContext context) {
    _index = _tabController.index;

    return SafeArea(
      child: Scaffold(
        body: TabBarView(
          controller: _tabController,
          children: [
            Pages(pageName: "Page 0"),
            Pages(pageName: "Page 1"),
            Pages(pageName: "Page 2"),
          ],
        ),
        appBar: AppBar(
          elevation: 0.0,
          bottom: TabBar(
            onTap: (value) {
              setState(() {
                value = _index;
                print("TabIndex: $value");
              });
            },
            labelColor: Colors.black,
            indicator: BoxDecoration(
              color: Colors.white,
              borderRadius: _index == 0
                  ? BorderRadius.only(topRight: Radius.circular(10.0))
                  : _index == 1
                      ? BorderRadius.only(
                          topLeft: Radius.circular(10.0),
                          topRight: Radius.circular(10.0))
                      : BorderRadius.only(topLeft: Radius.circular(10.0)),
            ),
            controller: _tabController,
            tabs: [
              Tab(text: "Tab0"),
              Tab(text: "Tab1"),
              Tab(text: "Tab2"),
            ],
          ),
        ),
      ),
    );
  }
}

class Pages extends StatelessWidget {
  late final String pageName;
  Pages({required this.pageName});
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Text(
          pageName,
          style: TextStyle(
            color: Colors.black,
            fontWeight: FontWeight.bold,
            fontSize: 60.0,
          ),
        ),
      ),
    );
  }
}

我在这里缺少什么?能不能告诉我!!

【问题讨论】:

    标签: android ios flutter dart tabbar


    【解决方案1】:

    可以通过添加addListners()_tabController 来解决这种混淆。

    以下是我的回答:

    我从TabBar() 中删除了onTap:(){} 属性,因为它不是必需的,并且在initState(){} 中我添加了addListener() 方法和_tabController。这解决了所有问题,Tabs() 样式可以监听来自TabBarView() 的滑动功能

    import 'package:flutter/material.dart';
    
    class Demo extends StatefulWidget with PreferredSizeWidget {
      @override
      _DemoState createState() => _DemoState();
    
      @override
      Size get preferredSize => AppBar().preferredSize;
    }
    
    class _DemoState extends State<Demo> with SingleTickerProviderStateMixin {
      late TabController _tabController;
      int _selectedIndex = 1;
    
      @override
      void initState() {
        super.initState();
        _tabController = TabController(length: 3, vsync: this, initialIndex: 1);
    
        _tabController.addListener(() {
          setState(() {
            _selectedIndex = _tabController.index;
          });
          print("Current Index: $_selectedIndex");
        });
      }
    
      @override
      void dispose() {
        _tabController.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return SafeArea(
          child: Scaffold(
            body: TabBarView(
              controller: _tabController,
              children: [
                Pages(pageName: "Page 0"),
                Pages(pageName: "Page 1"),
                Pages(pageName: "Page 2"),
              ],
            ),
            appBar: AppBar(
              elevation: 0.0,
              bottom: TabBar(
                labelColor: Colors.black,
                indicator: BoxDecoration(
                  color: Colors.white,
                  borderRadius: _selectedIndex == 0
                      ? BorderRadius.only(topRight: Radius.circular(10.0))
                      : _selectedIndex == 1
                          ? BorderRadius.only(
                              topLeft: Radius.circular(10.0),
                              topRight: Radius.circular(10.0))
                          : BorderRadius.only(topLeft: Radius.circular(10.0)),
                ),
                controller: _tabController,
                tabs: [
                  Tab(text: "Tab0"),
                  Tab(text: "Tab1"),
                  Tab(text: "Tab2"),
                ],
              ),
            ),
          ),
        );
      }
    }
    
    class Pages extends StatelessWidget {
      late final String pageName;
      Pages({required this.pageName});
      @override
      Widget build(BuildContext context) {
        return Container(
          child: Center(
            child: Text(
              pageName,
              style: TextStyle(
                color: Colors.black,
                fontWeight: FontWeight.bold,
                fontSize: 60.0,
              ),
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-19
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 2020-05-02
      • 2019-10-30
      • 2019-10-13
      相关资源
      最近更新 更多