'How to navigate tab from another dart file button tap event?
How to navigate tab from another dart file button tap event? I am using Tabmanager. It only works with the same dart file. I am having error in
TabManager._myTabbedPageKey.currentState._tabController
.animateTo(1);
main.dart
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
tabs: [
Tab(icon: Icon(Icons.directions_car)),
Tab(icon: Icon(Icons.directions_transit)),
],
),
title: Text('Tabs Demo'),
),
body: TabBarView(
children: [
new TabA(),
new TabB(),
],
),
),
),
);
}
}
class TabManager extends StatefulWidget {
static final _myTabbedPageKey = new GlobalKey<_MyAppState>();
@override
_TabManagerState createState() => _TabManagerState();
}
class _TabManagerState extends State<TabManager> {
@override
Widget build(BuildContext context) {
return new MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
home: new MyApp(
key: TabManager._myTabbedPageKey,
),
);
}
}
Here tabA.dart is a separate dart file. An error shows like
The getter '_myTabbedPageKey' isn't defined for the type 'TabManager'.
tabA.dart
class TabA extends StatefulWidget {
@override
_TabAState createState() => _TabAState();
}
class _TabAState extends State<TabA> {
@override
Widget build(BuildContext context) {
return Container(
child: GestureDetector(
onTap: () {
TabManager._myTabbedPageKey.currentState._tabController
.animateTo(1);
},
child: Text('taba')),
);
}
}
Solution 1:[1]
Try to This Code very similar
working demo
import 'package:flutter/material.dart';
void main() {
runApp(MyApp()); }
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
tabs: [
Tab(text: 'Home'),
Tab(text: 'Messages',),
Tab(text: 'Call'),
],
),
title: Text('MY App'),
),
body: TabBarView(
children: [
new Home(),
new Messages(),
new Call()
],
),
),
),
);
} }
class Home extends StatelessWidget {
@override Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child:
Text('Home',
style: TextStyle(fontSize: 21),)
)
)
);
} }
class Messages extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child:
Text('Meassage',
style: TextStyle(fontSize: 21),)
)
)
);
} }
class Call extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child:
Text('Call',
style: TextStyle(fontSize: 21),)
)
)
);
} }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |