Reputation: 273
I have defined the TabController but it says no controller define for Widget.type I have also try without controller but same error occur. The error is:
**'No TabController for ${widget.runtimeType}.\n'
'When creating a ${widget.runtimeType}, you must either provide an explicit '
'TabController using the "controller" property, or you must ensure that there
'is a DefaultTabController above the ${widget.runtimeType}.\n'
'In this case, there was neither an explicit controller nor a default controller.',**
Code
class _homePageState extends State<homePage>
with SingleTickerProviderStateMixin {
late TabController _controller;
static const List<Widget> _tabs = [
Tab(
icon: Icon(Icons.home),
),
Tab(
icon: Icon(Icons.tv),
),
Tab(
icon: Icon(Icons.supervised_user_circle),
)
];
@override
void initState() {
super.initState();
_controller = TabController(length: 3, vsync: this, initialIndex: 0);
_controller.animateTo(2);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("title"),
actions: [
InkWell(
child: Padding(
padding: EdgeInsets.all(8),
child: Icon(Icons.search),
),
onTap: () {},
),
Padding(
padding: EdgeInsets.all(8),
child: Icon(Icons.messenger),
),
],
bottom: TabBar(controller: _controller, tabs: _tabs),
),
body: TabBarView(
children: [Text("data"), Text("list"), Text("list")],
));
}
}
Thanks in advance.
Upvotes: 0
Views: 99
Reputation: 63709
Include TabBarView(controller: _controller
body: TabBarView(
controller: _controller
children: [
Text("data"),
Text("list"),
Text("list")
Full widget
class homePage extends StatefulWidget {
const homePage({Key? key}) : super(key: key);
@override
State<homePage> createState() => _homePageState();
}
class _homePageState extends State<homePage>
with SingleTickerProviderStateMixin {
late TabController _controller;
static const List<Widget> _tabs = [
Tab(
icon: Icon(Icons.home),
),
Tab(
icon: Icon(Icons.tv),
),
Tab(
icon: Icon(Icons.supervised_user_circle),
)
];
@override
void initState() {
super.initState();
_controller = TabController(length: 3, vsync: this, initialIndex: 0);
_controller.animateTo(2);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("title"),
actions: [
InkWell(
child: Padding(
padding: EdgeInsets.all(8),
child: Icon(Icons.search),
),
onTap: () {},
),
Padding(
padding: EdgeInsets.all(8),
child: Icon(Icons.messenger),
),
],
bottom: TabBar(controller: _controller, tabs: _tabs),
),
body: TabBarView(
controller: _controller,
children: [Text("data"), Text("list"), Text("list")],
));
}
}
Upvotes: 1