更新於 2024/12/13閱讀時間約 3 分鐘

[Flutter]Drawer

class MyApp extends StatefulWidget {
@override
myAppState createState() => myAppState();
}

class myAppState extends State<MyApp> {
//目前選擇頁索引值
int currentIndex = 0; //預設值

final pages = [One(), Two(), Three()];

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: Text('DrawerDemo'),
),
drawer: Drawer(
child: ListView(
children: <Widget>[
const DrawerHeader(
decoration: BoxDecoration(
color: Colors.blue,
),
child: Text('Drawer Header'),
),
//選單
ListTile(
leading: new CircleAvatar(child: Icon(your_icon)),
title: Text('第一頁'),
onTap: () {
itemClick(0);
},
),
ListTile(
leading: new CircleAvatar(child: Icon(your_icon)),
title: Text('第二頁'),
onTap: () {
itemClick(1);
},
),
ListTile(
leading: new CircleAvatar(child: Icon(your_icon)),
title: Text('第三頁'),
onTap: () {
itemClick(2);
},
),
],
),
),
body: pages[currentIndex],
);
}

void itemClick(int index) {
setState(() {
currentIndex = index;
Navigator.of(context).pop();
});
}
}




分享至
成為作者繼續創作的動力吧!
© 2024 vocus All rights reserved.