Created
October 23, 2021 17:25
-
-
Save alexcmgit/84c7cd8a1825db8266042e1107381c9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const TabBarDemo()); | |
} | |
class TabBarDemo extends StatefulWidget { | |
const TabBarDemo({Key? key}) : super(key: key); | |
@override | |
_TabBarDemoState createState() => _TabBarDemoState(); | |
} | |
class _TabBarDemoState extends State<TabBarDemo> | |
with SingleTickerProviderStateMixin { | |
late final TabController _controller; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = TabController(length: 3, vsync: this, initialIndex: 0); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar( | |
bottom: TabBar( | |
controller: _controller, | |
tabs: [ | |
Tab( | |
child: AnimatedBuilder( | |
animation: _controller, | |
builder: (context, chiild) { | |
return Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
const Icon(Icons.directions_car), | |
if (_controller.index == 0) const MyCustomWidget(), | |
], | |
); | |
}, | |
), | |
), | |
Tab( | |
child: AnimatedBuilder( | |
animation: _controller, | |
builder: (context, chiild) { | |
return Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
const Icon(Icons.directions_transit), | |
if (_controller.index == 1) const MyCustomWidget(), | |
], | |
); | |
}, | |
), | |
), | |
Tab( | |
child: AnimatedBuilder( | |
animation: _controller, | |
builder: (context, chiild) { | |
return Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
const Icon(Icons.directions_bike), | |
if (_controller.index == 2) const MyCustomWidget(), | |
], | |
); | |
}, | |
), | |
), | |
], | |
), | |
title: const Text('Tabs Demo'), | |
), | |
body: TabBarView( | |
controller: _controller, | |
children: const [ | |
Icon(Icons.directions_car), | |
Icon(Icons.directions_transit), | |
Icon(Icons.directions_bike), | |
], | |
), | |
), | |
); | |
} | |
} | |
class MyCustomWidget extends StatelessWidget { | |
const MyCustomWidget({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
color: Colors.red, | |
child: const Text('Your CustomWidget'), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment