fix navbar matching when using global back button
This commit is contained in:
@@ -113,18 +113,29 @@ class _LoginPanelContentState extends State<LoginPanelContent> {
|
||||
final _usernameController = TextEditingController();
|
||||
final _passwordController = TextEditingController();
|
||||
|
||||
bool _loggingIn = false;
|
||||
|
||||
void login() async {
|
||||
final username = _usernameController.text;
|
||||
final password = _passwordController.text;
|
||||
|
||||
final auth = context.read<AuthService>();
|
||||
|
||||
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<LoginPanelContent> {
|
||||
|
||||
@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<LoginPanelContent> {
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
spacing: 10,
|
||||
children: [
|
||||
FilledButton(onPressed: login, child: Text("Login")),
|
||||
FilledButton(onPressed: login, child: loginButtonContent),
|
||||
ElevatedButton(
|
||||
onPressed: widget.registerCb,
|
||||
child: Text("Register"),
|
||||
|
||||
10
lib/components/pages/newEntry.dart
Normal file
10
lib/components/pages/newEntry.dart
Normal file
@@ -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<DataService>();
|
||||
return Center(child: Text("New Entry Page"));
|
||||
}
|
||||
}
|
||||
33
lib/components/pages/trips.dart
Normal file
33
lib/components/pages/trips.dart
Normal file
@@ -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<DataService>();
|
||||
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}'),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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<MyHomePage> {
|
||||
int pageIndex = 0;
|
||||
final List<String> 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<MyHomePage> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget currentPage;
|
||||
|
||||
final location = GoRouterState.of(context).uri.toString();
|
||||
final pageIndex = _getIndexFromLocation(location);
|
||||
final data = context.watch<DataService>();
|
||||
final auth = context.read<AuthService>();
|
||||
|
||||
@@ -232,10 +255,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
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<MyHomePage> {
|
||||
),
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user