Home > front end >  Is it possible to change the size of the selected tab bar icon in Flutter?
Is it possible to change the size of the selected tab bar icon in Flutter?

Time:10-06

I'm working on a Flutter project, and got a problem with my TabBar. I would like to increase the size of the selected icon tabBar. Is it just possible ? I see that we can increase text size, but it doesn't work, of course, with an Icon.

Here is the code I am using :

return DefaultTabController(
      length: 5,
      child: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
              colors: [
                Theme.of(context).primaryColor,
                const Color.fromRGBO(0, 60, 99, 1.0)
              ]),
        ),
        child: Scaffold(
          backgroundColor: Colors.transparent,
          appBar: AppBar(
            backgroundColor: Colors.transparent,
            shadowColor: Colors.transparent,
            flexibleSpace: Column(
              mainAxisAlignment: MainAxisAlignment.end,
              children: const [
                TabBar(
                  tabs: [
                    Tab(
                      icon: Icon(
                        CupertinoIcons.add,
                      ),
                    ),
                    Tab(
                      icon: Icon(CupertinoIcons.add),
                    ),
                    Tab(
                      icon: Icon(CupertinoIcons.add),
                    ),
                    Tab(
                      icon: Icon(CupertinoIcons.add),
                    ),
                    Tab(
                      icon: Icon(CupertinoIcons.add), 
                    )
                  ],
                  unselectedLabelColor: Color.fromRGBO(142, 142, 147, 1),
                  labelColor: Color.fromRGBO(0, 60, 255, 1),
                  unselectedLabelStyle: TextStyle(fontSize: 15), // Test with text
                  labelStyle: TextStyle(
                    fontSize: 20,
                  ),
                ),
              ],
            ),
          ),
          body: TabBarView(
            physics: NeverScrollableScrollPhysics(),
            children: [
              FirstScreen(),
              SecondScreen(),
              ThirdScreen(),
              FourthScreen(),
              FifthScreen()
            ],
          ),
        ),
      ),
    );

I'm really stuck, I hope there is a solution!

CodePudding user response:

try this:

int _selectedTab = 0;

return DefaultTabController(
      length: 5,
      child: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
              colors: [
                Theme.of(context).primaryColor,
                const Color.fromRGBO(0, 60, 99, 1.0)
              ]),
        ),
        child: Scaffold(
          backgroundColor: Colors.transparent,
          appBar: AppBar(
            backgroundColor: Colors.transparent,
            shadowColor: Colors.transparent,
            flexibleSpace: Column(
              mainAxisAlignment: MainAxisAlignment.end,
              children: const [
                TabBar(
                  onTap: (index) {
                    _selectedTab = index;
                    setState((){});
                  },
                  tabs: [
                    Tab(
                      icon: Icon(
                        CupertinoIcons.add,
                        size: _selectedTab == 0 ? 30 : 18
                      ),
                    ),
                    Tab(
                      icon: Icon(
                        CupertinoIcons.add,
                        size: _selectedTab == 1 ? 30 : 18
                      ),
                    ),
                    Tab(
                      icon: Icon(
                        CupertinoIcons.add,
                        size: _selectedTab == 2 ? 30 : 18
                      ),
                    ),
                    Tab(
                      icon: Icon(
                        CupertinoIcons.add,
                        size: _selectedTab == 3 ? 30 : 18
                      ),
                    ),
                    Tab(
                      icon: Icon(
                        CupertinoIcons.add,
                        size: _selectedTab == 4 ? 30 : 18
                      ),
                    )
                  ],
                  unselectedLabelColor: Color.fromRGBO(142, 142, 147, 1),
                  labelColor: Color.fromRGBO(0, 60, 255, 1),
                  unselectedLabelStyle: TextStyle(fontSize: 15), // Test with text
                  labelStyle: TextStyle(
                    fontSize: 20,
                  ),
                ),
              ],
            ),
          ),
          body: TabBarView(
            physics: NeverScrollableScrollPhysics(),
            children: [
              FirstScreen(),
              SecondScreen(),
              ThirdScreen(),
              FourthScreen(),
              FifthScreen()
            ],
          ),
        ),
      ),
    );

CodePudding user response:

You need a TabController and for that you should add TickerProviderStateMixin to your class. Then set controller to Tabbar and use tabController.index in a condition to size your Icon. But it won't work when you switch tabs so you should add a listener and setState. here's the code:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:task_manager_v3/Utilities/CBase.dart';

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

  @override
  _TestState createState() => _TestState();
}

class _TestState extends State<Test> with TickerProviderStateMixin {
  TabController? tabController;
  @override
  void initState() {
    super.initState();
    tabController = TabController(length: 5, vsync: this);
    tabController!.addListener(() {
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 5,
      child: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
              colors: [
                Theme.of(context).primaryColor,
                const Color.fromRGBO(0, 60, 99, 1.0)
              ]),
        ),
        child: Scaffold(
          backgroundColor: Colors.transparent,
          appBar: AppBar(
            backgroundColor: Colors.transparent,
            shadowColor: Colors.transparent,
            flexibleSpace: Column(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                TabBar(
                  controller: tabController,
                  tabs: [
                    Tab(
                      icon: Icon(
                        CupertinoIcons.add,
                        size: tabController!.index == 0 ? 20 : 10,
                      ),
                    ),
                    Tab(
                      icon: Icon(
                        CupertinoIcons.add,
                        size: tabController!.index == 1 ? 20 : 10,
                      ),
                    ),
                    Tab(
                      icon: Icon(
                        CupertinoIcons.add,
                        size: tabController!.index == 2 ? 20 : 10,
                      ),
                    ),
                    Tab(
                      icon: Icon(
                        CupertinoIcons.add,
                        size: tabController!.index == 3 ? 20 : 10,
                      ),
                    ),
                    Tab(
                      icon: Icon(
                        CupertinoIcons.add,
                        size: tabController!.index == 4 ? 20 : 10,
                      ),
                    )
                  ],
                  unselectedLabelColor: Color.fromRGBO(142, 142, 147, 1),
                  labelColor: Color.fromRGBO(0, 60, 255, 1),
                  unselectedLabelStyle:
                      TextStyle(fontSize: 15), // Test with text
                  labelStyle: TextStyle(
                    fontSize: 20,
                  ),
                ),
              ],
            ),
          ),
          body: TabBarView(
            physics: NeverScrollableScrollPhysics(),
            children: [
          FirstScreen(),
          SecondScreen(),
          ThirdScreen(),
          FourthScreen(),
          FifthScreen()
            ],
          ),
        ),
      ),
    );
  }
}
  • Related