From 87868cee4db15f1305006e1b0ba91a3b6cd0eb74 Mon Sep 17 00:00:00 2001 From: NATHANIEL ENDICK Date: Sat, 25 Feb 2023 18:15:14 -0500 Subject: [PATCH] Persisent navbar --- lib/home.dart | 91 ++++++++++++++++++++++++--------------------------- lib/main.dart | 88 +++++++++++++++++++++++++++++++++++++++++-------- lib/sell.dart | 18 ++++++++++ 3 files changed, 135 insertions(+), 62 deletions(-) create mode 100644 lib/sell.dart diff --git a/lib/home.dart b/lib/home.dart index 20bf802..132742a 100644 --- a/lib/home.dart +++ b/lib/home.dart @@ -1,31 +1,10 @@ import 'package:firebase_auth/firebase_auth.dart'; +import 'package:ruswipeshare/main.dart'; import 'package:flutter/material.dart'; class HomeScreen extends StatelessWidget { const HomeScreen({super.key}); - @override - Widget build(BuildContext context) { - return const StatefulHome(); - } -} - -class StatefulHome extends StatefulWidget { - const StatefulHome({super.key}); - - @override - State createState() => _StatefulHomeState(); -} - -class _StatefulHomeState extends State { - int _selectedIndex = 0; - - void _onItemTapped(int index) { - setState(() { - _selectedIndex = index; - }); - } - @override Widget build(BuildContext context) { return Scaffold( @@ -46,32 +25,48 @@ class _StatefulHomeState extends State { ], ), ), - bottomNavigationBar: BottomNavigationBar( - type: BottomNavigationBarType.fixed, - backgroundColor: Colors.red, - selectedItemColor: Colors.black, - unselectedItemColor: Colors.black54, - items: const [ - BottomNavigationBarItem( - icon: Icon(Icons.shopping_cart), - label: 'Buy', - ), - BottomNavigationBarItem( - icon: Icon(Icons.attach_money), - label: 'Sell', - ), - BottomNavigationBarItem( - icon: Icon(Icons.person), - label: 'Profile', - ), - BottomNavigationBarItem( - icon: Icon(Icons.settings), - label: 'Settings', - ), - ], - currentIndex: _selectedIndex, - onTap: _onItemTapped, - ), + bottomNavigationBar: MyNavBar(), ); } } + +// class _HomeScreenState extends State { +// int _selectedIndex = 0; + +// void _onItemTapped(int index) { +// if (_selectedIndex == index) return; + +// setState(() { +// _selectedIndex = index; +// }); + +// Navigator.push( +// context, +// MaterialPageRoute(builder: (context) => pages.elementAt(index)), +// ); +// } + +// @override +// Widget build(BuildContext context) { +// return Scaffold( +// appBar: AppBar( +// title: const Text('FlutterFire UI'), +// ), +// body: Center( +// child: Column( +// mainAxisAlignment: MainAxisAlignment.center, +// children: [ +// const Text('You are logged in!'), +// ElevatedButton( +// onPressed: () { +// FirebaseAuth.instance.signOut(); +// }, +// child: const Text('Sign out'), +// ), +// ], +// ), +// ), +// bottomNavigationBar: const MyNavBar(), +// ); +// } +// } diff --git a/lib/main.dart b/lib/main.dart index b53fdc1..8ddfe1b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,27 +1,87 @@ import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/material.dart'; import 'package:ruswipeshare/home.dart'; +import 'package:ruswipeshare/sell.dart'; import 'firebase_options.dart'; import 'authGate.dart'; void main() async { - WidgetsFlutterBinding.ensureInitialized(); - await Firebase.initializeApp( - options: DefaultFirebaseOptions.android, - ); +// WidgetsFlutterBinding.ensureInitialized(); +// await Firebase.initializeApp( +// options: DefaultFirebaseOptions.android, +// ); runApp(const MyApp()); } class MyApp extends StatelessWidget { - const MyApp({super.key}); - @override - Widget build(BuildContext context) { - return MaterialApp( - theme: ThemeData( - primarySwatch: Colors.blue, - ), - home: const AuthGate(), - ); - } + const MyApp({super.key}); + @override + Widget build(BuildContext context) { + return MaterialApp( + theme: ThemeData( + primarySwatch: Colors.blue, + ), + home: const HomeScreen(), + ); + } +} + +class MyNavBar extends StatefulWidget { + MyNavBar({super.key}); + + @override + State createState() => MyNavBarState(); +} + +class MyNavBarState extends State { + final pages = [ + const SellScreen(), + const SellScreen(), + ]; + + int _selectedIndex = 0; + + void _onItemTapped(int index) { + if (runtimeType == pages.elementAt(index).runtimeType) return; + + setState(() { + _selectedIndex = index; + }); + + Navigator.push( + context, + MaterialPageRoute(builder: (context) => pages.elementAt(index)), + ); + } + + @override + Widget build(BuildContext context) { + return BottomNavigationBar( + type: BottomNavigationBarType.fixed, + backgroundColor: Colors.red, + selectedItemColor: Colors.black, + unselectedItemColor: Colors.black54, + items: const [ + BottomNavigationBarItem( + icon: Icon(Icons.shopping_cart), + label: 'Buy', + ), + BottomNavigationBarItem( + icon: Icon(Icons.attach_money), + label: 'Sell', + ), + BottomNavigationBarItem( + icon: Icon(Icons.person), + label: 'Profile', + ), + BottomNavigationBarItem( + icon: Icon(Icons.settings), + label: 'Settings', + ), + ], + currentIndex: _selectedIndex, + onTap: _onItemTapped, + ); + } } diff --git a/lib/sell.dart b/lib/sell.dart new file mode 100644 index 0000000..0a56024 --- /dev/null +++ b/lib/sell.dart @@ -0,0 +1,18 @@ +import 'package:firebase_auth/firebase_auth.dart'; +import 'package:flutter/material.dart'; +import 'package:ruswipeshare/home.dart'; +import 'package:ruswipeshare/main.dart'; + +class SellScreen extends StatelessWidget { + const SellScreen({super.key}); + + @override + Widget build(BuildContext context) { + return Scaffold( + body: Center( + child: const Text("Hi"), + ), + bottomNavigationBar: MyNavBar(), + ); + } +}