From ae88847543f393ea1e5ec50f0513776719845b26 Mon Sep 17 00:00:00 2001 From: Pete Gregory Date: Wed, 6 Aug 2025 00:53:37 +0100 Subject: [PATCH] fix navbar matching when using global back button --- lib/components/login/login.dart | 22 +++++++++++++++++- lib/components/pages/newEntry.dart | 10 +++++++++ lib/components/pages/trips.dart | 33 +++++++++++++++++++++++++++ lib/main.dart | 36 +++++++++++++++++++++++------- 4 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 lib/components/pages/newEntry.dart create mode 100644 lib/components/pages/trips.dart diff --git a/lib/components/login/login.dart b/lib/components/login/login.dart index 42808eb..b505d23 100644 --- a/lib/components/login/login.dart +++ b/lib/components/login/login.dart @@ -113,18 +113,29 @@ class _LoginPanelContentState extends State { final _usernameController = TextEditingController(); final _passwordController = TextEditingController(); + bool _loggingIn = false; + void login() async { final username = _usernameController.text; final password = _passwordController.text; final auth = context.read(); + setState(() { + _loggingIn = true; + }); try { auth.login(username, password); print('Login successful'); + setState(() { + _loggingIn = false; + }); } catch (e) { // Handle error print('Login failed: $e'); + setState(() { + _loggingIn = false; + }); ScaffoldMessenger.of( context, ).showSnackBar(SnackBar(content: Text('Login failed'))); @@ -133,6 +144,15 @@ class _LoginPanelContentState extends State { @override Widget build(BuildContext context) { + Widget loginButtonContent = Text("Login"); + if (_loggingIn) { + loginButtonContent = Padding( + padding: const EdgeInsets.all(8.0), + child: CircularProgressIndicator(), + ); + } else { + loginButtonContent = Text("Login"); + } return Column( mainAxisAlignment: MainAxisAlignment.center, mainAxisSize: MainAxisSize.min, @@ -164,7 +184,7 @@ class _LoginPanelContentState extends State { mainAxisAlignment: MainAxisAlignment.center, spacing: 10, children: [ - FilledButton(onPressed: login, child: Text("Login")), + FilledButton(onPressed: login, child: loginButtonContent), ElevatedButton( onPressed: widget.registerCb, child: Text("Register"), diff --git a/lib/components/pages/newEntry.dart b/lib/components/pages/newEntry.dart new file mode 100644 index 0000000..cd87422 --- /dev/null +++ b/lib/components/pages/newEntry.dart @@ -0,0 +1,10 @@ +import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; +import 'package:mileograph_flutter/services/dataService.dart'; + +class NewEntryPage extends StatelessWidget { + Widget build(BuildContext context) { + final data = context.watch(); + return Center(child: Text("New Entry Page")); + } +} diff --git a/lib/components/pages/trips.dart b/lib/components/pages/trips.dart new file mode 100644 index 0000000..c733214 --- /dev/null +++ b/lib/components/pages/trips.dart @@ -0,0 +1,33 @@ +import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; +import 'package:mileograph_flutter/services/dataService.dart'; + +class TripsPage extends StatelessWidget { + Widget build(BuildContext context) { + final data = context.watch(); + return ListView.builder( + itemCount: data.legs.length, + itemBuilder: (context, index) { + final leg = data.legs[index]; + return Card( + margin: EdgeInsets.symmetric(horizontal: 16, vertical: 8), + child: Padding( + padding: EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + '${leg.start} → ${leg.end}', + style: TextStyle(fontSize: 16), + ), + Text('Mileage: ${leg.mileage.toStringAsFixed(2)} km'), + Text('Headcode: ${leg.headcode}'), + Text('Begin: ${leg.beginTime}'), + ], + ), + ), + ); + }, + ); + } +} diff --git a/lib/main.dart b/lib/main.dart index b9305ae..789cb6f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,7 +1,9 @@ import 'package:flutter/material.dart'; import 'package:dynamic_color/dynamic_color.dart'; import 'package:mileograph_flutter/components/pages/calculator.dart'; +import 'package:mileograph_flutter/components/pages/newEntry.dart'; import 'package:mileograph_flutter/components/pages/traction.dart'; +import 'package:mileograph_flutter/components/pages/trips.dart'; import 'package:provider/provider.dart'; import 'package:mileograph_flutter/components/pages/legs.dart'; @@ -85,8 +87,14 @@ class MyApp extends StatelessWidget { routes: [ GoRoute(path: '/', builder: (_, __) => const Dashboard()), GoRoute(path: '/calculator', builder: (_, __) => CalculatorPage()), + GoRoute( + path: '/calculator/details', + builder: (_, __) => CalculatorPage(), + ), GoRoute(path: '/legs', builder: (_, __) => LegsPage()), GoRoute(path: '/traction', builder: (_, __) => TractionPage()), + GoRoute(path: '/trips', builder: (_, __) => TripsPage()), + GoRoute(path: '/add', builder: (_, __) => NewEntryPage()), ], ), GoRoute(path: '/login', builder: (_, __) => const LoginScreen()), @@ -146,15 +154,29 @@ class MyHomePage extends StatefulWidget { } class _MyHomePageState extends State { - int pageIndex = 0; final List contentPages = [ "/", "/calculator", "/legs", "/traction", - "/", + "/trips", ]; + int _getIndexFromLocation(String location) { + int newIndex = contentPages.indexWhere((path) => location == path); + if (newIndex < 0) { + return 0; + } + return newIndex; + } + + void _onItemTapped(int index, int currentIndex) { + if (index < 0 || index >= contentPages.length || index == currentIndex) + return; + context.push(contentPages[index]); + _getIndexFromLocation(contentPages[index]); + } + bool loggedIn = false; bool _fetched = false; @@ -184,7 +206,8 @@ class _MyHomePageState extends State { @override Widget build(BuildContext context) { Widget currentPage; - + final location = GoRouterState.of(context).uri.toString(); + final pageIndex = _getIndexFromLocation(location); final data = context.watch(); final auth = context.read(); @@ -232,10 +255,7 @@ class _MyHomePageState extends State { bottomNavigationBar: NavigationBar( selectedIndex: pageIndex, onDestinationSelected: (int index) { - setState(() { - pageIndex = index; - context.push(contentPages[index]); - }); + _onItemTapped(index, pageIndex); }, destinations: [ NavigationDestination(icon: Icon(Icons.home), label: "Home"), @@ -247,7 +267,7 @@ class _MyHomePageState extends State { ), body: currentPage, floatingActionButton: FloatingActionButton( - onPressed: null, + onPressed: () => {context.push("/add")}, tooltip: 'New Entry', child: const Icon(Icons.add), ), // This trailing comma makes auto-formatting nicer for build methods.