I'm working in a flutter app. I have a bottom bar with six children. Two of them are TabBar Screens but I have an issue with them. Any time I change the selected Tab in the TabBar/TabBarView and move to other Screen in the bottom bar navigation and return to the previous Screen with the Tabs the TabBarView shows the last selected tab.
With code:
return SafeArea(
child: DefaultTabController(
length: 4,
initialIndex: 0,
key: widget.key,
child: Scaffold(
//backgroundColor: Colors.white,
appBar: ColoredTabBar(
color: Theme.of(context).primaryColorLight,
tabBar: TabBar(
unselectedLabelStyle: TextStyle(fontSize: textSize*0.8),
indicator: BoxDecoration(
borderRadius: BorderRadius.only(topLeft: Radius.circular(00),topRight: Radius.circular(00)),
color: Theme.of(context).primaryColor,
//border: Border.all(),
),
//isScrollable: true,
labelStyle: TextStyle(fontSize: textSize),
unselectedLabelColor: Theme.of(context).textTheme.body1.color,
labelColor: Theme.of(context).unselectedWidgetColor,
tabs: [
Tab(text: "Paquetes"),
Tab(text: "Micro"),
Tab(text: "Recargas"),
Tab(text: "A la medida"),
],
),
mywalletResponse: widget.wallet,
numero: widget.defaultNumber,
onPressed: widget.onPressed,
onTap: widget.onTap,
),
body: TabBarView(
//controller: _tabController2,
key: widget.key,
children: [
CombosScreen(),
MicroScreen(),
RecargasScreen(),
CustomScreen(), // tercera pagina: notificaciones
],
),
)))
;
ColoredTabBar widget is:
class ColoredTabBar extends Container implements PreferredSizeWidget{
ColoredTabBar({@required this.color, @required this.tabBar, @required this.mywalletResponse, @required this.numero, @required this.onTap, @required this.onPressed});
final Color color;
final TabBar tabBar;
final WalletResponse mywalletResponse;
final String numero;
final VoidCallback onTap;
final VoidCallback onPressed;
@override
Size get preferredSize => Size.fromHeight(103.0);
@override
Widget build(BuildContext context) => Container(
color: color,
child: Column(
children: <Widget>[
Header(number: numero, walletModel: mywalletResponse, onTap: onTap, onPressed: onPressed,),
tabBar,
],
)
);
}
So for example, I go to the Widget with the defaultTabController and select the widget at index 2, later I move to another widget in the bottom navigation bar and later I return to the previous widget with the defaultTabController. The selected tab is the one at index 0, but the TabBarView shows the widget at index 2.
Do you know how to initialize the index in a way that the tab and the widget is the same?