2023-12-17 23:00:31 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2024-01-13 17:07:11 +01:00
|
|
|
import 'package:flutter_svg/svg.dart';
|
2023-12-18 17:51:22 +01:00
|
|
|
import 'package:go_router/go_router.dart';
|
2024-01-09 12:34:44 +01:00
|
|
|
import 'package:moody/utils/definitions/color_pair.dart';
|
|
|
|
import 'package:moody/utils/logic/preferences_service.dart';
|
2023-12-17 23:00:31 +01:00
|
|
|
|
|
|
|
class CustomBottomNavigationBar extends StatefulWidget {
|
|
|
|
final int initialSelectedIndex;
|
|
|
|
|
2024-01-09 12:34:44 +01:00
|
|
|
const CustomBottomNavigationBar({Key? key, this.initialSelectedIndex = 0}) : super(key: key);
|
2023-12-17 23:00:31 +01:00
|
|
|
|
|
|
|
@override
|
2024-01-09 12:34:44 +01:00
|
|
|
State<CustomBottomNavigationBar> createState() => _CustomBottomNavigationBar();
|
2023-12-17 23:00:31 +01:00
|
|
|
}
|
|
|
|
|
2024-01-09 12:34:44 +01:00
|
|
|
class _CustomBottomNavigationBar extends State<CustomBottomNavigationBar> {
|
2023-12-17 23:00:31 +01:00
|
|
|
late int _selectedIndex;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_selectedIndex = widget.initialSelectedIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onItemTapped(int index) {
|
|
|
|
setState(() {
|
|
|
|
_selectedIndex = index;
|
|
|
|
});
|
2024-01-09 12:34:44 +01:00
|
|
|
var currentRoute = GoRouter.of(context).routeInformationProvider.value.uri.toString();
|
2023-12-18 17:51:22 +01:00
|
|
|
var goToRoute = "/";
|
|
|
|
switch (index) {
|
|
|
|
case 0:
|
|
|
|
goToRoute = "/statistic";
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
goToRoute = "/home";
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
goToRoute = "/settings";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (currentRoute != goToRoute) {
|
|
|
|
context.go(goToRoute);
|
|
|
|
}
|
2023-12-17 23:00:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-01-01 18:31:10 +01:00
|
|
|
return FutureBuilder<ColorPair>(
|
2024-01-09 12:34:44 +01:00
|
|
|
future: PreferencesService().loadColorPair(), // Async loading of the color
|
2024-01-01 18:31:10 +01:00
|
|
|
builder: (BuildContext context, AsyncSnapshot<ColorPair> snapshot) {
|
2024-01-09 12:34:44 +01:00
|
|
|
if (snapshot.connectionState == ConnectionState.done && snapshot.hasData) {
|
2024-01-01 18:31:10 +01:00
|
|
|
// When data is loaded
|
2024-01-09 12:34:44 +01:00
|
|
|
Color backgroundColor = snapshot.data!.backgroundColor; // Use loaded background color
|
2024-01-13 17:07:11 +01:00
|
|
|
Color textColor = snapshot.data!.textColor; // Use loaded background color
|
|
|
|
|
|
|
|
return buildNavigationBar(backgroundColor, textColor);
|
2024-01-01 18:31:10 +01:00
|
|
|
} else {
|
|
|
|
// While loading or if no data, show default or loading indicator
|
2024-01-13 17:07:11 +01:00
|
|
|
return buildNavigationBar(Colors.white, Colors.black); // Default color or loading indicator
|
2024-01-01 18:31:10 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-01-13 17:07:11 +01:00
|
|
|
Widget buildNavigationBar(Color backgroundColor, textColor) {
|
2023-12-18 17:51:22 +01:00
|
|
|
return Container(
|
|
|
|
width: 175,
|
2024-01-09 12:34:44 +01:00
|
|
|
margin: const EdgeInsets.only(bottom: 30),
|
|
|
|
padding: const EdgeInsets.all(5),
|
2023-12-18 17:51:22 +01:00
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: Colors.white,
|
|
|
|
border: Border.all(color: Colors.transparent),
|
2024-01-09 12:34:44 +01:00
|
|
|
borderRadius: BorderRadius.circular(35), // Adjust radius to fit your design
|
2023-12-18 17:51:22 +01:00
|
|
|
boxShadow: [
|
|
|
|
BoxShadow(
|
|
|
|
color: Colors.grey.withOpacity(0.5),
|
|
|
|
spreadRadius: 1,
|
|
|
|
blurRadius: 7,
|
2024-01-09 12:34:44 +01:00
|
|
|
offset: const Offset(3, 3), // changes position of shadow
|
2023-12-17 23:00:31 +01:00
|
|
|
),
|
2023-12-18 17:51:22 +01:00
|
|
|
BoxShadow(
|
|
|
|
color: Colors.grey.withOpacity(0.3),
|
|
|
|
spreadRadius: 1,
|
|
|
|
blurRadius: 5,
|
2024-01-09 12:34:44 +01:00
|
|
|
offset: const Offset(-1, -1), // changes position of shadow
|
2023-12-18 17:51:22 +01:00
|
|
|
),
|
|
|
|
// You can add more BoxShadow layers to create more complex shadows
|
|
|
|
],
|
|
|
|
),
|
|
|
|
child: ClipRRect(
|
|
|
|
borderRadius: BorderRadius.circular(35),
|
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
|
|
children: <Widget>[
|
2024-01-13 17:07:11 +01:00
|
|
|
_buildNavItem(SvgPicture.asset('assets/icons/icon-analyze.svg', color: textColor), 0, backgroundColor),
|
|
|
|
_buildNavItem(SvgPicture.asset('assets/icons/logo.svg', color: textColor, fit: BoxFit.cover), 1, backgroundColor),
|
|
|
|
_buildNavItem(SvgPicture.asset('assets/icons/icon-settings.svg', color: textColor, fit: BoxFit.contain), 2, backgroundColor),
|
2023-12-18 17:51:22 +01:00
|
|
|
],
|
2023-12-17 23:00:31 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-01-13 17:07:11 +01:00
|
|
|
Widget _buildNavItem(SvgPicture icon, int index, Color selectedColor) {
|
2023-12-18 17:51:22 +01:00
|
|
|
return Container(
|
2023-12-20 17:24:18 +01:00
|
|
|
height: 40,
|
2024-01-01 18:31:10 +01:00
|
|
|
width: 40,
|
2023-12-18 17:51:22 +01:00
|
|
|
decoration: _selectedIndex == index
|
|
|
|
? BoxDecoration(
|
2024-01-01 18:31:10 +01:00
|
|
|
color: selectedColor,
|
2023-12-18 17:51:22 +01:00
|
|
|
borderRadius: BorderRadius.circular(40),
|
|
|
|
)
|
|
|
|
: null,
|
|
|
|
child: IconButton(
|
2023-12-20 17:24:18 +01:00
|
|
|
icon: icon,
|
2023-12-18 17:51:22 +01:00
|
|
|
color: _selectedIndex == index ? Colors.white : null,
|
|
|
|
onPressed: () => _onItemTapped(index),
|
|
|
|
splashColor: Colors.transparent,
|
|
|
|
highlightColor: Colors.transparent,
|
2024-01-13 17:07:11 +01:00
|
|
|
iconSize: 40,
|
2023-12-17 23:00:31 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|