liuyuqi-dellpc 1 year ago
parent
commit
acfa90fba7
2 changed files with 53 additions and 14 deletions
  1. 1 3
      lib/main.dart
  2. 52 11
      lib/pages/index_page.dart

+ 1 - 3
lib/main.dart

@@ -1,6 +1,5 @@
 import 'package:flutter/material.dart';
 import 'package:flutter_google_map/pages/index_page.dart';
-import 'package:flutter_google_map/pages/tabs/home_page.dart';
 
 /// Description:  enter point
 /// Time       : 07/06/2023 Thursday
@@ -19,10 +18,9 @@ class MyApp extends StatelessWidget {
       title: 'Google Map',
       debugShowCheckedModeBanner: false,
       theme: ThemeData(
-   
         primarySwatch: Colors.blue,
       ),
-      home: const IndexPage(),
+      home: IndexPage(),
     );
   }
 }

+ 52 - 11
lib/pages/index_page.dart

@@ -1,14 +1,38 @@
 import 'package:flutter/material.dart';
+import 'package:flutter_google_map/pages/tabs/business_page.dart';
+import 'package:flutter_google_map/pages/tabs/home_page.dart';
+import 'package:flutter_google_map/pages/tabs/mine_page.dart';
+import 'package:flutter_google_map/pages/tabs/taxi_page.dart';
 
 class IndexPage extends StatefulWidget {
-  const IndexPage({super.key});
+  int pageIndex;
+  IndexPage({super.key, this.pageIndex = 0});
 
   @override
   State<IndexPage> createState() => _IndexPageState();
 }
 
 class _IndexPageState extends State<IndexPage> {
-  int index = 0;
+  final List<BottomNavigationBarItem> bottomTabs = const [
+    BottomNavigationBarItem(icon: Icon(Icons.home), label: '出行'),
+    BottomNavigationBarItem(icon: Icon(Icons.business), label: '周边惠'),
+    BottomNavigationBarItem(icon: Icon(Icons.school), label: '打车'),
+    BottomNavigationBarItem(icon: Icon(Icons.school), label: '我的'),
+  ];
+
+  final List<Widget> pages = const [
+    HomePage(),
+    BusinessPage(),
+    TaxiPage(),
+    MinePage(),
+  ];
+
+  int currentIndex = 0;
+
+  Size get size => MediaQuery.of(context).size;
+
+  final pageController = PageController();
+
   @override
   Widget build(BuildContext context) {
     return Scaffold(
@@ -16,12 +40,17 @@ class _IndexPageState extends State<IndexPage> {
         title: const Text('Maps Sample App'),
         backgroundColor: Colors.green[700],
       ),
-      bottomNavigationBar: BottomNavigationBar(items: [
-        BottomNavigationBarItem(icon: Icon(Icons.home), label: '出行'),
-        BottomNavigationBarItem(icon: Icon(Icons.business), label: '周边惠'),
-        BottomNavigationBarItem(icon: Icon(Icons.school), label: '打车'),
-        BottomNavigationBarItem(icon: Icon(Icons.school), label: '我的'),
-      ]),
+      bottomNavigationBar: BottomNavigationBar(
+        items: bottomTabs,
+        currentIndex: currentIndex,
+        type: BottomNavigationBarType.fixed,
+        onTap: (index) {
+          setState(() {
+            currentIndex = index;
+            pageController.jumpToPage(index);
+          });
+        },
+      ),
       drawer: Drawer(
         child: ListView(
           padding: EdgeInsets.zero,
@@ -47,11 +76,23 @@ class _IndexPageState extends State<IndexPage> {
           ],
         ),
       ),
-      body: buildContent(index),
+      body: _getPageBody(context),
     );
   }
 
-  buildContent(int index) {
-    return Placeholder();
+  @override
+  bool get wantKeepAlive => true;
+
+  Widget _getPageBody(BuildContext context) {
+    return PageView(
+      controller: pageController,
+      physics: const NeverScrollableScrollPhysics(),
+      onPageChanged: (index) {
+        setState(() {
+          currentIndex = index;
+        });
+      },
+      children: pages,
+    );
   }
 }