I am developing a simple app with Flutter like a university project. In my app I develop this file .dart to include a bottom bar in all the pages to navigate between them:
import 'package:flutter/material.dart';
class BottomBar extends StatefulWidget {
final VoidCallback onHomePressed;
final VoidCallback onCareerPressed;
final VoidCallback onProgressPressed;
final VoidCallback onAccountPressed;
final int selectedIndex;
const BottomBar({
super.key,
required this.onHomePressed,
required this.onCareerPressed,
required this.onProgressPressed,
required this.onAccountPressed,
this.selectedIndex = 0,
});
@override
_BottomBarState createState() => _BottomBarState();
}
class _BottomBarState extends State<BottomBar> {
late int _selectedIndex;
@override
void initState() {
super.initState();
_selectedIndex = widget.selectedIndex;
}
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
switch (index) {
case 0:
widget.onHomePressed();
break;
case 1:
widget.onCareerPressed();
break;
case 2:
widget.onProgressPressed();
break;
case 3:
widget.onAccountPressed();
break;
}
}
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
backgroundColor: Theme.of(context).bottomNavigationBarTheme.backgroundColor,
selectedItemColor: Theme.of(context).bottomNavigationBarTheme.selectedItemColor,
unselectedItemColor: Theme.of(context).bottomNavigationBarTheme.unselectedItemColor,
currentIndex: _selectedIndex,
onTap: _onItemTapped,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.timer_sharp),
label: 'Allenamento',
),
BottomNavigationBarItem(
icon: Icon(Icons.calendar_month),
label: 'Carriera',
),
BottomNavigationBarItem(
icon: Icon(Icons.insights),
label: 'Progressi',
),
BottomNavigationBarItem(
icon: Icon(Icons.account_circle),
label: 'Account',
),
],
);
}
}
These are the colors that I want to use:
class AppColors {
static const Color backgroundColor = Color.fromARGB(255, 7, 29, 0);
static const Color backgroundSezioni = Color.fromARGB(132, 255, 255, 255);
static const Color backgroundCalendario = Color.fromARGB(145, 255, 255, 255);
static const Color bottomBarColor = Color.fromARGB(255, 0, 21, 2);
static const Color barIcon = Color.fromARGB(255, 255, 255, 255);
static const Color barIconSelect = Color.fromARGB(255, 40, 255, 16);
}
The problem is that when I include it in all my pages the background color seems not work or better probably there’s something that override my color that give white color to the background.
An example of my four pages is this:
import 'package:flutter/material.dart';
import 'package:level_up/repository/category_repo.dart';
import 'package:level_up/pages/allenamenti.dart';
import 'package:level_up/pages/account.dart';
import 'package:level_up/pages/carriera.dart';
import 'package:level_up/pages/progressi.dart';
import 'package:level_up/style/stile.dart';
import 'package:level_up/utils/bottom_bar.dart';
class Home extends StatefulWidget {
const Home({super.key, this.title = 'LevelUp'});
final String title;
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
int _selectedIndex = 0;
void _onBottomBarSelected(int index) {
setState(() {
_selectedIndex = index;
});
switch (index) {
case 0:
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const Home()),
);
break;
case 1:
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const Carriera()),
);
break;
case 2:
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const Progressi()),
);
break;
case 3:
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const Account()),
);
break;
}
}
void _navigateToAllenamenti(String categoria) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AllenamentiPage(categoria: categoria),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: FutureBuilder<List<dynamic>>(
future: fetchCategory(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(child: Text('Errore: ${snapshot.error}'));
} else if (!snapshot.hasData || snapshot.data!.isEmpty) {
return const Center(child: Text('Nessun dato disponibile'));
} else {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Padding(
padding: DimApp.margineLato,
child: Text(
'Seleziona il tipo di allenamento:',
style: TextStyle(
fontSize: 18,
color: Colors.white,
),
),
),
const SizedBox(height: 5.0,),
Expanded(
child: ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
var category = snapshot.data![index];
String imagePath = category['img_url'];
String categoria = category['_id'];
return GestureDetector(
onTap: () {
_navigateToAllenamenti(categoria);
},
child: Container(
margin: DimApp.margineMedio,
height: 200.0,
child: Stack(
children: [
ClipRRect(
borderRadius: DimApp.raggioSezioni,
child: Image.asset(
imagePath,
fit: BoxFit.cover,
height: 200.0,
width: double.infinity,
alignment: Alignment.center,
errorBuilder: (context, error, stackTrace) {
return const Center(
child: Icon(Icons.error));
},
),
),
Positioned(
bottom: 0,
left: 0,
right: 0,
child: Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.6),
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(10.0),
bottomRight: Radius.circular(10.0),
),
),
child: Text(
categoria,
style: const TextStyle(
color: Colors.white,
fontSize: 19.0,
fontWeight: FontWeight.bold,
),
),
),
),
],
),
),
);
},
),
),
],
);
}
},
),
bottomNavigationBar: BottomBar(
selectedIndex: _selectedIndex,
onHomePressed: () => _onBottomBarSelected(0),
onCareerPressed: () => _onBottomBarSelected(1),
onProgressPressed: () => _onBottomBarSelected(2),
onAccountPressed: () => _onBottomBarSelected(3),
),
);
}
}
This page recover data from an API and generate the different categories, but I think that the content of my page don’t affect the behaviour of the bottom bar because it doesn’t work in any pages.
For completeness these are the main.dart file and the app.dart file that launch the application:
import 'dart:io';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'package:level_up/app.dart';
import 'package:window_manager/window_manager.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
WindowOptions windowOptions = const WindowOptions(
//Pixel 8 pro
size: Size(448, 998),
);
if (!kIsWeb &&
!Platform.isAndroid &&
!Platform.isIOS &&
(Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
await windowManager.ensureInitialized();
windowManager.waitUntilReadyToShow(windowOptions, () async {
await windowManager.show();
await windowManager.focus();
});
}
runApp(
const ProviderScope(
child: App(),
),
);
}
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:level_up/pages/login.dart';
import 'package:level_up/style/stile.dart';
class App extends ConsumerWidget {
const App({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'LevelUp',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: AppColors.backgroundColor,
),
appBarTheme: const AppBarTheme(
centerTitle: true,
backgroundColor: AppColors.backgroundColor,
titleTextStyle: TextStyle(
color: Colors.white,
fontSize: 30,
fontWeight: FontWeight.bold,
),
),
scaffoldBackgroundColor: AppColors.backgroundColor,
bottomNavigationBarTheme: const BottomNavigationBarThemeData(
backgroundColor: AppColors.bottomBarColor,
selectedItemColor: AppColors.barIconSelect,
unselectedItemColor: AppColors.barIcon,
),
),
home: const Login(),
);
}
}
I tried to import the bottom bar in another project and it seems work, but in this project no
Alessandro Signori is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.