Usage/Flutter
IndexedStack, bottomNavigation으로 화면 전환
MG_conny
2021. 8. 28. 17:43
indexedStack 사용법
- class 내에서 int _selectedIndex = 0; 선언
- index: _selectedIndex, 으로 보여줄 children 세팅
- bottomNavigation의 ontap으로 클릭시 보여줄 index 설정
class MainScreens extends StatefulWidget {
const MainScreens({Key? key}) : super(key: key);
@override
_MainScreensState createState() => _MainScreensState();
}
class _MainScreensState extends State<MainScreens> {
int _selectedIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: _selectedIndex,
children: [
Container(
color: Colors.orange[100],
child: Center(
child: Text(
'IndexedStack 1',
style: TextStyle(fontSize: 20, color: Colors.black),
),
),
),
Container(
color: Colors.redAccent[100],
child: Center(
child: Text(
'IndexedStack 2',
style: TextStyle(fontSize: 20, color: Colors.black),
),
),
)
],
),
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.home),
label: '홈',
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.chat_bubble),
label: '채팅',
)
],
onTap: (index) {
setState(() {
_selectedIndex = index;
});
},
currentIndex: _selectedIndex,
),
);
}
}
728x90