Flutter(Dart)
[Error] No TabController for TabBar
코코리니
2022. 9. 29. 16:20
반응형
TabBar 위젯을 사용하면서 controller를 지정해 주지 않으면 발생하는 오류.
controller 는 직접 만들어서 적용해도 되지만 DefaultTabController위젯을 사용해도 된다.
// DefaultController 사용예시
@override
Widget build(BuildContext context){
return DefaultTabController(
child: Scaffold(
// 기존과 동일하게 작성
)
);
}
//////////////////////////////////////////////
// StatefulWidget controller 예시
late TabController _tabController;
@override
void initState() {
super.initState();
_tabController = TabController(vsync: this, length: myTabs.length);
}
@override
Widget build(BuildContext context){
return DefaultTabController(
child: Scaffold(
.
.
.
child: TabBar(
controller: _tabController,
)
)
);
}
https://api.flutter.dev/flutter/material/TabController-class.html
TabController class - material library - Dart API
Coordinates tab selection between a TabBar and a TabBarView. The index property is the index of the selected tab and the animation represents the current scroll positions of the tab bar and the tab bar view. The selected tab's index can be changed with ani
api.flutter.dev
반응형