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 _usernameController = TextEditingController();
|
||||||
final _passwordController = TextEditingController();
|
final _passwordController = TextEditingController();
|
||||||
|
|
||||||
|
bool _loggingIn = false;
|
||||||
|
|
||||||
void login() async {
|
void login() async {
|
||||||
final username = _usernameController.text;
|
final username = _usernameController.text;
|
||||||
final password = _passwordController.text;
|
final password = _passwordController.text;
|
||||||
|
|
||||||
final auth = context.read<AuthService>();
|
final auth = context.read<AuthService>();
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
_loggingIn = true;
|
||||||
|
});
|
||||||
try {
|
try {
|
||||||
auth.login(username, password);
|
auth.login(username, password);
|
||||||
print('Login successful');
|
print('Login successful');
|
||||||
|
setState(() {
|
||||||
|
_loggingIn = false;
|
||||||
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Handle error
|
// Handle error
|
||||||
print('Login failed: $e');
|
print('Login failed: $e');
|
||||||
|
setState(() {
|
||||||
|
_loggingIn = false;
|
||||||
|
});
|
||||||
ScaffoldMessenger.of(
|
ScaffoldMessenger.of(
|
||||||
context,
|
context,
|
||||||
).showSnackBar(SnackBar(content: Text('Login failed')));
|
).showSnackBar(SnackBar(content: Text('Login failed')));
|
||||||
@@ -133,6 +144,15 @@ class _LoginPanelContentState extends State<LoginPanelContent> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
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(
|
return Column(
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
@@ -164,7 +184,7 @@ class _LoginPanelContentState extends State<LoginPanelContent> {
|
|||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
spacing: 10,
|
spacing: 10,
|
||||||
children: [
|
children: [
|
||||||
FilledButton(onPressed: login, child: Text("Login")),
|
FilledButton(onPressed: login, child: loginButtonContent),
|
||||||
ElevatedButton(
|
ElevatedButton(
|
||||||
onPressed: widget.registerCb,
|
onPressed: widget.registerCb,
|
||||||
child: Text("Register"),
|
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:flutter/material.dart';
|
||||||
import 'package:dynamic_color/dynamic_color.dart';
|
import 'package:dynamic_color/dynamic_color.dart';
|
||||||
import 'package:mileograph_flutter/components/pages/calculator.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/traction.dart';
|
||||||
|
import 'package:mileograph_flutter/components/pages/trips.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
import 'package:mileograph_flutter/components/pages/legs.dart';
|
import 'package:mileograph_flutter/components/pages/legs.dart';
|
||||||
@@ -85,8 +87,14 @@ class MyApp extends StatelessWidget {
|
|||||||
routes: [
|
routes: [
|
||||||
GoRoute(path: '/', builder: (_, __) => const Dashboard()),
|
GoRoute(path: '/', builder: (_, __) => const Dashboard()),
|
||||||
GoRoute(path: '/calculator', builder: (_, __) => CalculatorPage()),
|
GoRoute(path: '/calculator', builder: (_, __) => CalculatorPage()),
|
||||||
|
GoRoute(
|
||||||
|
path: '/calculator/details',
|
||||||
|
builder: (_, __) => CalculatorPage(),
|
||||||
|
),
|
||||||
GoRoute(path: '/legs', builder: (_, __) => LegsPage()),
|
GoRoute(path: '/legs', builder: (_, __) => LegsPage()),
|
||||||
GoRoute(path: '/traction', builder: (_, __) => TractionPage()),
|
GoRoute(path: '/traction', builder: (_, __) => TractionPage()),
|
||||||
|
GoRoute(path: '/trips', builder: (_, __) => TripsPage()),
|
||||||
|
GoRoute(path: '/add', builder: (_, __) => NewEntryPage()),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
GoRoute(path: '/login', builder: (_, __) => const LoginScreen()),
|
GoRoute(path: '/login', builder: (_, __) => const LoginScreen()),
|
||||||
@@ -146,15 +154,29 @@ class MyHomePage extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _MyHomePageState extends State<MyHomePage> {
|
class _MyHomePageState extends State<MyHomePage> {
|
||||||
int pageIndex = 0;
|
|
||||||
final List<String> contentPages = [
|
final List<String> contentPages = [
|
||||||
"/",
|
"/",
|
||||||
"/calculator",
|
"/calculator",
|
||||||
"/legs",
|
"/legs",
|
||||||
"/traction",
|
"/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 loggedIn = false;
|
||||||
bool _fetched = false;
|
bool _fetched = false;
|
||||||
|
|
||||||
@@ -184,7 +206,8 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
Widget currentPage;
|
Widget currentPage;
|
||||||
|
final location = GoRouterState.of(context).uri.toString();
|
||||||
|
final pageIndex = _getIndexFromLocation(location);
|
||||||
final data = context.watch<DataService>();
|
final data = context.watch<DataService>();
|
||||||
final auth = context.read<AuthService>();
|
final auth = context.read<AuthService>();
|
||||||
|
|
||||||
@@ -232,10 +255,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
bottomNavigationBar: NavigationBar(
|
bottomNavigationBar: NavigationBar(
|
||||||
selectedIndex: pageIndex,
|
selectedIndex: pageIndex,
|
||||||
onDestinationSelected: (int index) {
|
onDestinationSelected: (int index) {
|
||||||
setState(() {
|
_onItemTapped(index, pageIndex);
|
||||||
pageIndex = index;
|
|
||||||
context.push(contentPages[index]);
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
destinations: [
|
destinations: [
|
||||||
NavigationDestination(icon: Icon(Icons.home), label: "Home"),
|
NavigationDestination(icon: Icon(Icons.home), label: "Home"),
|
||||||
@@ -247,7 +267,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
),
|
),
|
||||||
body: currentPage,
|
body: currentPage,
|
||||||
floatingActionButton: FloatingActionButton(
|
floatingActionButton: FloatingActionButton(
|
||||||
onPressed: null,
|
onPressed: () => {context.push("/add")},
|
||||||
tooltip: 'New Entry',
|
tooltip: 'New Entry',
|
||||||
child: const Icon(Icons.add),
|
child: const Icon(Icons.add),
|
||||||
), // This trailing comma makes auto-formatting nicer for build methods.
|
), // This trailing comma makes auto-formatting nicer for build methods.
|
||||||
|
|||||||
Reference in New Issue
Block a user