Olá
Estou tentando implementar um BottomNavigationBar, mas quando insiro "onTap: (index) => setState(() => currentIndex = index)," para o estado do ícone selecionado trocar conforme o usuário clicar ele da o erro de "The method 'setState' isn't defined for the type 'HomePage'."... sinceramente eu já tentei várias alterações, entretanto a lista de erros só aumenta. Alguém conseguiria me ajudar, por favor?
Segue abaixo o meu código:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
var theme = darkTheme;
var darkTheme = ThemeData(
primarySwatch: Colors.red,
brightness: Brightness.dark,
visualDensity: VisualDensity.adaptivePlatformDensity,
);
var lightTheme = ThemeData(
primarySwatch: Colors.teal,
brightness: Brightness.light,
visualDensity: VisualDensity.adaptivePlatformDensity,
);
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
int currentIndex =0;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
changeTheme();
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangePlatformBrightness() {
changeTheme();
}
changeTheme() {
var brightness = WidgetsBinding.instance.window.platformBrightness;
brightness == Brightness.dark ? theme = darkTheme : theme = lightTheme;
setState(() {});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Themes',
debugShowCheckedModeBanner: false,
theme: theme,
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
int currentIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Home"),
),
body: Center(
child: Text("Hello"),
),
bottomNavigationBar: BottomNavigationBar (
currentIndex: currentIndex,
onTap: (index) => setState(() => currentIndex = index),
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
backgroundColor: Colors.blue,
),
BottomNavigationBarItem(
icon: Icon(Icons.favorite),
label: 'Favoritos',
backgroundColor: Colors.red,
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
label: 'Pesquisar',
backgroundColor: Colors.purple,
),
BottomNavigationBarItem(
icon: Icon(Icons.menu),
label: 'Opções',
backgroundColor: Colors.green,
),
],
),
);
}
}
Obrigado!