Move to next Tab on Button Click in Flutter

This Article is posted by seven.srikanth at 11/28/2018 7:25:59 PM




In this post, I've shared the code which will help you to move to next tab on button click in Flutter.

Here is the final main.dart code. All you need to do is to, copy paste this code into your newly created flutter project and run it.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final appTitle = 'Tabs Demo';
return MaterialApp(
title: appTitle,
home: MyHomePage(title: appTitle),
);
}
}

class MyHomePage extends StatefulWidget {
final String title;

const MyHomePage({Key key, this.title}) : super(key: key);

@override
State<StatefulWidget> createState() {
return _MyHomePageState();
}
}


class _MyHomePageState extends State<MyHomePage>
with SingleTickerProviderStateMixin {
int _tabIndex = 0;

TabController _tabController;

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

void _toggleTab() {
_tabIndex = _tabController.index + 1;
_tabController.animateTo(_tabIndex);
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text(widget.title),
bottom: TabBar(
controller: _tabController,
tabs: [
Tab(text: 'Tab 1'),
Tab(text: 'Tab 2'),
Tab(text: 'Tab 3'),
],
),
),
body: TabBarView(
controller: _tabController,
children: [
Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
leading: Icon(Icons.album),
title: Text('Hello 1'),
subtitle:
Text('Click on Next Button to go to Tab 2.'),
),
ButtonTheme.bar(
// make buttons use the appropriate styles for cards
child: ButtonBar(
children: <Widget>[
FlatButton(
child: const Text('Next'),
onPressed: _toggleTab,
),
],
),
),
],
),
),
Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
leading: Icon(Icons.album),
title: Text('Hello 2'),
subtitle:
Text('Click on Next Button to go to Tab 3'),
),
ButtonTheme.bar(
// make buttons use the appropriate styles for cards
child: ButtonBar(
children: <Widget>[
FlatButton(
child: const Text('Next'),
onPressed: _toggleTab,
),
],
),
),
],
),
),
Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
leading: Icon(Icons.album),
title: Text('Hello 3'),
subtitle:
Text('The End'),
),
ButtonTheme.bar(
// make buttons use the appropriate styles for cards
child: ButtonBar(
children: <Widget>[
FlatButton(
child: const Text('Done'),
onPressed: _toggleTab,
),
],
),
),
],
),
),
],
),
));
}
}


Hope this is helpful.

Thanks,
Srikanth

Tags: Move to next Tab on Button Click in Flutter








1 Comments
Login to comment.
Recent Comments

ashokmedidi9 at 11/26/2020

Tanks a lot ..mate really saved my time

Login to Reply.



© 2018 - Fluttercentral | Email to me - seven.srikanth@gmail.com