In order to have a globally available method for showing snackbars, i created a Riverpod provider for the ScaffoldMessenger, as it can be found in some examples on the internet:
final scaffoldMessengerKeyProvider = Provider((ref) =>
GlobalKey<ScaffoldMessengerState>());
final scaffoldMessengerProvider = Provider((ref) =>
ref.watch(scaffoldMessengerKeyProvider).currentState!);
return MaterialApp.router(
theme: AppTheme.lightTheme,
darkTheme: AppTheme.darkTheme,
scaffoldMessengerKey: ref.watch(scaffoldMessengerKeyProvider),
...
);
This allows me to show snackbars from anywhere by calling:
widgetRef.read(scaffoldMessengerProvider).showSnackBar(
SnackBar(
content: Text('Hi, i am a SnackBar'),
),
);
Showing the snackbar works fine, but the theme that is being applied to those snackbars seems to be the blue default theme.
How could I apply my own Themes (AppTheme.lightTheme / AppTheme.darkTheme) to this ScaffoldMessenger or just the snackbars? Is there a clean way of doing this, that i'm not seeing?
I think the cleanest way to do it is inside your lightTheme and darkTheme and apply the desired Theme.
Example:
static final darkTheme = ThemeData(
snackBarTheme: const SnackBarThemeData(
actionTextColor: Colors.red,
backgroundColor: Colors.black,
contentTextStyle: TextStyle(color: Colors.white),
elevation: 20
),
Related
I have an icon set using a transparent image, on the home screen the background is white but when in the Gesture Navigation view the icon above the app screen is blue. How do I change this background color? (Using flutter)
I am also having this issue. As a work around, I keep the Material App ThemeData's primary color as white. Then used the Theme widget to override my page theme to use my custom theme.
https://api.flutter.dev/flutter/material/ThemeData-class.html
class MyApp extends StatelessWidget {
final _navigatorKey = GlobalKey<NavigatorState>();
NavigatorState? get _navigator => _navigatorKey.currentState;
MyApp({super.key});
// App Routing
Route<dynamic> _generateRoute(RouteSettings settings) {
Widget newPage = Container();
switch (settings.name) {
case AppRoutes.welcome:
newPage = const WelcomePage();
break;
case AppRoutes.login:
newPage = LoginPage();
break;
}
return FadeRoute(
page: Theme(
data: lightTheme,
child: newPage,
),
);
}
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider<AppBloc>(
create: (_) => AppBloc()..add(InitializeAppEvent())),
BlocProvider<AuthBloc>(create: (_) => AuthBloc())
],
child: BlocListener<AuthBloc, AuthState>(
listener: (_, state) {
if (state is Authenticated) {
// Go to Main Page
_navigator?.pushReplacementNamed(AppRoutes.home);
} else {
// Go to Login Page
_navigator?.pushReplacementNamed(AppRoutes.login);
}
},
child: MaterialApp(
navigatorKey: _navigatorKey,
title: 'Flutter Demo',
theme: ThemeData(primaryColor: Colors.white),
initialRoute: AppRoutes.welcome,
onGenerateRoute: _generateRoute,
)));
}
}
Thats the primary color you can change it like this
MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primaryColor: Colors.lightGreen,//here.change this one
),
)
It isn't from Android. Flutter's MaterialApp already provides a set of attributes for us, including a Prymary Color, ColorScheme, etc.
The reason is obvious, if every developer had to write every theme aspect in every widget coding would be awful. So Material widgets look for a theme, which Material will provide as default, if we don't overide it.
Know this, the solution is:
Overide some componentes of the whole Theme in the MaterialApp wich is my recomendation. The code bellow is an example of this.
Wrap a specifc widget/s with an Theme widget that overides the define Theme for explict specified atributes.
Pass a custom theme directly in the widgets you are using. Very common to see in Text widgets when people do Text( "some text", style: TextStyle()) (note the TextStyle), but this logic is apllied to a bunch of other widgets too, including buttons. Disavantage of this is that you have to manual change every widget, so no auto darkmode and painfull design changes for reasonable size apps. I do not recomend as a go to solution for every widget.
Example of what I meant by overiding the default Theme of your App:
MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Association App for AMDKP Integrated Plataform',
theme: ThemeData(
colorScheme: ColorScheme(
brightness: Brightness.light,
primary: consts.golden1,
onPrimary: consts.black41,
secondary: Colors.green.shade500,
onSecondary: Colors.green.shade300,
background: consts.greyWhite,
onBackground: consts.black41,
surface: Colors.white,
onSurface: Colors.black45,
error: Colors.red.shade900,
onError: Colors.red.shade900,
),
primarySwatch: Colors.blue,
primaryColor: consts.golden1,
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
shadowColor: consts.black41,
primary: Theme.of(context).colorScheme.onSurface.withAlpha(150),
onPrimary: Theme.of(context).colorScheme.surface,
)),
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
primary: Colors.white.withAlpha(230),
backgroundColor: Colors.black87.withAlpha(170),
textStyle: Theme.of(context).textTheme.bodyMedium,
padding: const EdgeInsets.symmetric(horizontal: 10.0),
)),
inputDecorationTheme: const InputDecorationTheme(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: consts.golden1, width: 2)),
),
),
home: const HomePage(),
So definitely take a look at flutter themes, it will empower your flutter developer skills and you will benefit a lot by using it anyway! :)
Cheers
I am new to flutter. I just created a theme file to define light and dark theme.When I defined light theme, the textTheme: property which is deprecated is not changing the text title color of app bar into black.
If I have to create or define textTheme or replace it ?? How should I do it ?
class MyTheme {
static ThemeData lightTheme(BuildContext context) => ThemeData(
primarySwatch: Colors.deepPurple,
fontFamily: GoogleFonts.lato().fontFamily,
appBarTheme: AppBarTheme(
color: Colors.white,
elevation: 0.0,
iconTheme: IconThemeData(color: Colors.black),
//------
textTheme: Theme.of(context).textTheme, // Problem is here
//------
));
static ThemeData darkTheme(BuildContext context) =>
ThemeData(brightness: Brightness.dark);
}
Change it to this
textTheme: Theme.of(context).appBarTheme.textTheme,
PS: It's deprecated. try to migrate.
Use toolbarTextStyle and titleTextStyle instead of using textTheme inside appBarTheme.
toolbarTextStyle:
Theme.of(context).appBarTheme.toolbarTextStyle?.copyWith(
color: Colors.amber,
///your config
),
titleTextStyle:
Theme.of(context).appBarTheme.titleTextStyle?.copyWith(
color: Colors.amber, ///your config
)
More about using theme.
I want to give every CircularProgressIndicator a color in my app but what attribute is responsible for it in MaterialApp widget:
MaterialApp(
theme: ThemeData(
// what is the attribute for this widget.
)
)
I used both accentColor and colorScheme.secondary but still not change.
You can use accentColor to change the color of CircularProgressIndicator. It will look like this:
theme: ThemeData(
accentColor: Colors.red,
EDIT:
In the newest version of Flutter, you should use colorScheme instead of accentColor.
colorScheme: ColorScheme.fromSwatch().copyWith(
secondary: Colors.red,
),
In ThemeData there is progressIndicatorTheme attribute which you can assign ProgressIndicatorThemeData constructor to it, and there you can change the default color of ProgressIndicator.
I'm using the showAboutDialog function from flutter to show used licences in my project. How ever I'm stuck with changing the text color of the VIEW LICENSES and CLOSE textbuttons. See this image for clarification:
This is my code:
...
onTap: () {
showAboutDialog(
context: context,
applicationName: 'bla',
applicationLegalese: 'November 2023',
);
},
What I tried so far is looking for a color field inside the showAboutDialog how ever I could not find anything. I'm assuming that I could change the color in my MaterialApp ThemeData. Unfortunately I was not able to find the specific theme to override the default styling of those textbuttons.
I tried the following in my MaterialApp ThemeData to change the color of VIEW LICENSES and CLOSE to green but that did not change anything:
textButtonTheme: TextButtonThemeData(style: ButtonStyle(foregroundColor: MaterialStateProperty.all<Color>(Colors.green))
Any ideas about this?
I was not satisfied with the answers here because all were showing only MaterialColor use-cases and I wanted a custom color. But I finally found something explaining it well on the following link.
https://blog.logrocket.com/new-material-buttons-in-flutter/
Basically, what is confusing is that the new design uses the primary color instead of the textStyle property. You can still apply the other answers to change the overall theme using a MaterialColor, and you can override the existing color theme using any color by using primary under TextButton.styleFrom.
Example for anywhere in the app:
TextButton(
onPressed: () {},
style: TextButton.styleFrom(
foregroundColor: Colors.pink,
),
child: Text(
'TextButton (New)',
style: TextStyle(fontSize: 30),
),
)
Example for the theme:
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
primary: kDarkColor, // This is a custom color variable
textStyle: GoogleFonts.fredokaOne(),
),
),
You can use this:
return MaterialApp(
theme: ThemeData.dark().copyWith(
textButtonTheme: TextButtonThemeData(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.resolveWith(
(state) => Colors.orange)))),
home: MyWidget(),
);
MaterialStateProperty.resolveWith takes a function, you can specify the color based on states, such as
MaterialState.pressed,
MaterialState.hovered,
MaterialState.focused,
More info on this.
How about this one?
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
colorScheme: ColorScheme.fromSwatch(
primarySwatch: Colors.green,
).copyWith(),
),
debugShowCheckedModeBanner: false,
home: YourScreen(),
);
}
i run this code.
after some research i find out this way to change colour.
for this you need to set application main theme colour change, like this
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.brown,//i am set brown colour,you can set your colour here
),
debugShowCheckedModeBanner: false,
home: YourScreen(),
);
}
after this its work,
showAboutDialog(
context: context,
applicationName: 'bla',
applicationLegalese: 'November 2023',
);
If you want to change the colors only for the dialog and not for the whole app, you have to create a new context. Surround the Button that showing the dialog with a Theme and a Builder
Theme(
data: Theme.of(context).copyWith(
colorScheme: colorScheme.copyWith(primary: Colors.green),
),
child: Builder(
builder: (context) {
return ListTile(
title: Text('show dialog'),
onTap: () => showAboutDialog(
context: context,
...)
);
},
),
)
is there a way to set whole app backgorundcolor in flutter. For example i want to use white background on all screens. so the first thing i do is manually setting background color to all screens. But i think its overkill. and i am looking for the a shortcut to achieve it.
i have tried below code but couldn't get achieve what i wanted.
#override
Widget build(BuildContext context) {
return BlocProvider<SplashBloc>(
bloc: splashBloc,
child: MaterialApp(
theme: new ThemeData(scaffoldBackgroundColor: Colors.white),
home: Splash(),
),
);
}
}
In main.dart, use,
MaterialApp(
theme: ThemeData(
scaffoldBackgroundColor: Colors.white),
),
This will change the background color of the entire app provided you are returning Scaffold in the build Widget.
You use use the theme in your MaterialApp Widget to set up the theme colors for your entire app like so:
MaterialApp(
theme: ThemeData(
primaryIconTheme: IconThemeData(color: Colors.white),
primaryColor: Color.fromRGBO(254, 248, 248, 1),
appBarTheme: AppBarTheme(
color: <color_of_choice>,
),
),
Your backgroundColor will assume the primaryColor above.
Read all about it here.