Why when I switch page using Get(()=> Page2 of getX package, the app go back to default theme colors?
I have a custom theme with yellow color, but then it goes back to the flutter blue default color.
Am I missing something?
my code
appBar: AppBar(
title: Text('Profile'),
actions: [
IconButton(
icon: Icon(Icons.edit_note_outlined), onPressed: () {
setState(() {
/*isVisible= !isVisible;
isReadOnly = !isReadOnly;*/
});
Get.to(
AddNewProduct(),
duration: Duration(milliseconds: 300),
transition: Transition.fade
);
},
),
],
),
my main
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(GetMaterialApp(
home: const MyApp(),)
);
}
my custom theme
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter app',
theme: ThemeData(
primarySwatch: Colors.amber,
buttonTheme: ButtonTheme.of(context).copyWith(
textTheme: ButtonTextTheme.primary,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25.0)
),
)
),
home: const MyHomePage(title: 'Home'),
);
}
4
Answers
In your case you are using two app widgets:
But you have to use only GetMaterialApp.
In your
main
function removeGetMaterialApp
In your MyApp widget replace
MaterialApp
withGetMaterialApp
ANSWER THAT DEPENDS ON CONTEXT
Let’s look on structure of your widget tree
As you can see when you are navigating to the AddNewProduct screen you request context from the Builder widget where your theme is not set up and you launch a new screen with the default theme
To solve this you have two options:
I prefer the second option:
And your HomeWidget:
You are using two
MaterialApp
classes. The ‘normal’ one and theGetMaterialApp
. You should get rid of the normal one, and move all parameters to theGetMaterialApp
.GetMaterialApp
replacesMaterialApp
. So likeYou probably don’t need your
MyApp
class anymore. Or alternatively replace theMaterialApp
inMyApp
withGetMaterialApp
and remove it from main likechange
to
Do not use
*GetMaterialApp(*
in build useScaffold
You are using 2 material classes.
-> MaterialApp
-> GetMaterialApp
Remove GetMaterialApp from main class
And replace MaterialApp to GetMaterialApp in MyApp widget