Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. import 'package:firstapp/pages/home_page.dart';
  2. import 'package:firstapp/pages/flight_ticket_page.dart';
  3. import 'package:flutter/material.dart';
  4.  
  5. class MainPage extends StatefulWidget {
  6. @override
  7. _MainPageState createState() => _MainPageState();
  8. }
  9.  
  10. class _MainPageState extends State<MainPage> {
  11. // กำหนด default page ไว้ที่ index 0 ซึ่งก็คือ HomePage
  12. int currentIndex = 0;
  13. // สร้าง List ของ pages สำหรับ drawer
  14. List pages = [
  15. HomePage(),
  16. FlightTicketPage(),
  17. ];
  18.  
  19. @override
  20. Widget build(BuildContext context) {
  21. // นี่ไง appBar
  22. Widget appBar = AppBar(
  23. // Center นี่ก็เป็น Widget นะเออ
  24. title: Center(
  25. // จะกำหนด margin ต้องใช้ Container จำไว้ มีลูกได้แค่ 1 ตัว
  26. child: Container(
  27. // margin ใช้ EdgeInserts กำหนดระยะนะจ๊ะ
  28. margin: EdgeInsets.only(bottom: 10.0, top: 5.0, right: 50.0, left: 0.0),
  29. // สร้างภาพด้วย Widget Image
  30. child: Image(
  31. image: AssetImage('assets/images/logo.png'),
  32. height: 65.0,
  33. ),
  34. ),
  35. ),
  36. // กำหนดสีให้ appBar แต่ไม่ต้องใส่ก็ได้ ก็เรากำหนดใน theme แล้วนี่
  37. backgroundColor: Theme.of(context).primaryColor,
  38. );
  39.  
  40. // ตัวอย่างการสร้าง TextStyle
  41. TextStyle menuStyle = TextStyle(fontFamily: 'Prompt', fontSize: 20.0, color: Colors.white);
  42.  
  43. // นี่เลย drawer หรือเมนูด้านข้างนั้นเอง
  44. Widget drawer = Drawer(
  45. // กำหนดสีพื้นหลังให้มันก็ใช้ Container นะง่ายดี
  46. child: Container(
  47. color: Theme.of(context).primaryColor,
  48. // สร้าง List ด้วย ListView มีลูกได้หลายตัว
  49. child: ListView(
  50. children: <Widget>[
  51. // ใช้ ListTile จะได้สวยๆ ประกอบด้วย leading, title, trailing
  52. ListTile(
  53. // สร้าง Icon ที่คลิกได้ด้วย IconButton
  54. trailing: IconButton(
  55. icon: Icon(
  56. // มี Icons ให้ใช้อยู่แล้ว เลือกได้ตามสบาย
  57. Icons.dehaze,
  58. color: Colors.white,
  59. ),
  60. // จะกดปุ่มใช่มั้ยใช้ onPressed สิ กดเสร็จแล้วปิด drawer ด้วย
  61. onPressed: () => Navigator.pop(context),
  62. ),
  63. ),
  64. ListTile(
  65. title: Text(
  66. 'หน้าแรก',
  67. style: menuStyle,
  68. ),
  69. onTap: () {
  70. // เปลี่ยน currentIndex ด้วย setState()
  71. setState(() {
  72. currentIndex = 0;
  73. });
  74. Navigator.pop(context);
  75. },
  76. ),
  77. ListTile(
  78. title: Text(
  79. 'ตั๋วเครื่องบิน',
  80. style: menuStyle,
  81. ),
  82. onTap: () {
  83. // เปลี่ยน currentIndex ด้วย setState()
  84. setState(() {
  85. currentIndex = 1;
  86. });
  87. Navigator.pop(context);
  88. },
  89. ),
  90. // ต้องการพื้นที่ว่างใช้อันนี้
  91. SizedBox(height: 30.0)
  92. ],
  93. ),
  94. ),
  95. );
  96.  
  97. // Scaffold ใช้ในการสร้าง appBar, drawer, body
  98. return Scaffold(
  99. appBar: appBar,
  100. drawer: drawer,
  101. // จะเปลี่ยน body ใช่มั้ย ก็เอา currentIndex มาใช้เลย
  102. body: pages[currentIndex],
  103. );
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement