Created
June 14, 2018 13:11
-
-
Save alexd1971/f12e2e402cd1bad0f33d7dc4693dbca8 to your computer and use it in GitHub Desktop.
Flutter TabView problem example
This file contains 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(new MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
title: 'Flutter Demo', | |
theme: new ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: new MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key}) : super(key: key); | |
@override | |
_MyHomePageState createState() => new _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin { | |
int _tabIndex = 0; | |
List<Tab> _tabs = [ | |
Tab(text: 'Tab1'), | |
Tab(text: 'Tab2'), | |
Tab(text: 'Tab3') | |
]; | |
TabController _tabController; | |
GlobalKey<FormState> _form1State = GlobalKey<FormState>(); | |
GlobalKey<FormState> _form2State = GlobalKey<FormState>(); | |
@override | |
void initState() { | |
_tabController = TabController(length: _tabs.length, vsync: this, initialIndex: _tabIndex); | |
super.initState(); | |
} | |
@override | |
void dispose() { | |
_tabController.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text('TabView Test'), | |
bottom: TabBar( | |
controller: _tabController, | |
tabs: _tabs | |
), | |
), | |
body: TabBarView( | |
controller: _tabController, | |
children: <Widget>[ | |
Form( | |
key: _form1State, | |
child: TextFormField(), | |
), | |
Form( | |
key: _form2State, | |
child: TextFormField(), | |
), | |
Center( | |
child: Text('Tab3'), | |
) | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment