Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Change TabItem background when selected

I want to ask how to change tab item background color when the tab is selected?

sorry I'm a newbie in the flutter

is it better to use the bottom tab bar or tab bar ?

like this :

enter image description here

my code :

          bottomNavigationBar: new TabBar(
            tabs: [
              Tab(
                icon: new Icon(Icons.home),
              ),
              Tab(
                icon: new Icon(Icons.rss_feed),
              ),
              Tab(
                icon: new Icon(Icons.perm_identity),
              ),
              Tab(icon: new Icon(Icons.settings),)
            ],
            labelColor: Colors.yellow,
            indicatorWeight: 1.0,
            unselectedLabelColor: Colors.blue,
            indicatorSize: TabBarIndicatorSize.label,
            indicatorPadding: EdgeInsets.all(5.0),
            indicatorColor: Colors.red,
          ),
          backgroundColor: Colors.black,
        ),
      ),
    );
  }
}
like image 806
Evan Laksana Avatar asked Aug 21 '18 03:08

Evan Laksana


People also ask

How do you change the background color of a selected tab in flutter?

To change tab bar background color in Flutter, first, create a getter to return the TabBar widget and then wrap the TabBar widget inside the PreferredSize -> Material widget. Inside the Material add the color property and set the color of your choice.

How do I select tab programmatically in flutter?

Setting up TabBar in Flutter Here's the minimal code to get TabBar up and running: DefaultTabController( length: 3, child: Scaffold( appBar: AppBar( bottom: TabBar( tabs: [ Tab(icon: Icon(Icons. flight)), Tab(icon: Icon(Icons.


3 Answers

This is pretty simple and doesn't require implementing whole new custom tab bar. You only need to create a custom selection indicator, like this:

TabBar(
  ...
  indicator: SolidIndicator(),
)

And then SolidIndicator implementation:

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

/// Solid tab bar indicator.
class SolidIndicator extends Decoration {
  @override
  BoxPainter createBoxPainter([VoidCallback onChanged]) {
    return _SolidIndicatorPainter(this, onChanged);
  }
}

class _SolidIndicatorPainter extends BoxPainter {
  final SolidIndicator decoration;

  _SolidIndicatorPainter(this.decoration, VoidCallback onChanged)
      : assert(decoration != null),
        super(onChanged);

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    assert(configuration != null);
    assert(configuration.size != null);

    final Rect rect = offset & configuration.size;
    final Paint paint = Paint();
    paint.color = Colors.white;
    paint.style = PaintingStyle.fill;
    canvas.drawRect(rect, paint);
  }
}
like image 94
Aleksey Gureiev Avatar answered Nov 15 '22 11:11

Aleksey Gureiev


For an even easier solution use:

indicator: BoxDecoration(color: Colors.white),
like image 35
loreco Avatar answered Nov 15 '22 11:11

loreco


bottom: TabBar(
          tabs: [
            Tab(
                // icon: Icon(Icons.contacts),
                text: "All Threads"),
            Tab(text: "Inbox"),
            Tab(text: "Sent"),
          ],
          indicator: BoxDecoration(
            color: Color(0xffff00a8),
            // color: _hasBeenPressed ? Color(0xffffffff) : Color(0xffff00a8),
          ),
          unselectedLabelColor: Color(0xffff00a8),
          indicatorColor: Color(0xffff00a8),
          labelColor: Color(0xffffffff),

          // unselectedLabelColor: Colors.grey,
        ),
like image 20
Vinojan Isoft Avatar answered Nov 15 '22 09:11

Vinojan Isoft