DefaultTabBarController with flutter. Obviously the tabcontroller isn’t getting anything. The following code is probably what you’re looking for:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class MyApp extends StatefulWidget { static final _myTabbedPageKey = new GlobalKey<_MyTabbedPageState>(); @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { @override Widget build(BuildContext context) { return new MaterialApp( title: 'Flutter Demo', home: new MyTabbedPage( key: MyApp._myTabbedPageKey, ), ); } } class MyTabbedPage extends StatefulWidget { const MyTabbedPage({Key key}) : super(key: key); @override _MyTabbedPageState createState() => new _MyTabbedPageState(); } class _MyTabbedPageState extends State<MyTabbedPage> with SingleTickerProviderStateMixin { final List<Tab> myTabs = <Tab>[ new Tab(text: 'LEFT'), new Tab(text: 'RIGHT'), ]; TabController _tabController; @override void initState() { super.initState(); _tabController = new TabController(vsync: this, length: myTabs.length); } @override void dispose() { _tabController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar( title: new Text("Tab demo"), bottom: new TabBar( controller: _tabController, tabs: myTabs, ), ), body: new TabBarView( controller: _tabController, children: [ LoginApp(), SignUpApp(), ], ), ); } } class LoginApp extends StatefulWidget { @override _LoginAppState createState() => _LoginAppState(); } class _LoginAppState extends State<LoginApp> { @override Widget build(BuildContext context) { return Padding( padding: EdgeInsets.only(top: 30, bottom: 30), child: new RichText( text: TextSpan( text: "Don't have an account? ", style: DefaultTextStyle.of(context).style, children: <TextSpan>[ new TextSpan( text: 'Sign Up', style: new TextStyle( fontWeight: FontWeight.bold), recognizer: TapGestureRecognizer() ..onTap = () => MyApp._myTabbedPageKey.currentState._tabController.animateTo((MyApp._myTabbedPageKey.currentState._tabController.index + 1) % 2), ), ]), )); } } class SignUpApp extends StatefulWidget { @override _SignUpAppState createState() => _SignUpAppState(); } class _SignUpAppState extends State<SignUpApp> { @override Widget build(BuildContext context) { return Text('godbye'); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.