Where is the themeMode property in CupertinoApp - flutter

In MaterialApp I can use themeMode to explicitly set the theme of the app.
themeMode: ThemeMode.dark,
I couldn't find a similar one in CupertinoApp.

You can use brightness property in the CupertinoThemeData for it.
#override
Widget build(BuildContext context) {
return const CupertinoApp(
theme: CupertinoThemeData(
brightness: Brightness.light,
),
title: _title,
home: MyStatefulWidget(),
);
}

Related

Why dose MediaQuery.of(context).platformBrightness not working?

I want to check if my Darkmode is enable or not so I did this:
#override
Widget build(BuildContext context) {
var brightness = MediaQuery.of(context).platformBrightness;
bool isDarkMode = brightness == Brightness.dark;
print(isDarkMode);
in the first widget which builds after the MaterialApp, but I always get false as print result, independently of the active Theme Mode. I also tried
var brightness = SchedulerBinding.instance.platformDispatcher.platformBrightness;
bool isDarkMode = brightness == Brightness.dark;
In the init state, but same problem
I checked if it's the fault of my project, so I created a brand-new Project and used the adaptive_theme package - still same problem
Code of the Material:
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return AdaptiveTheme(
light: ThemeData(
brightness: Brightness.light,
primarySwatch: Colors.yellow,
backgroundColor: Colors.grey
),
dark: ThemeData(
brightness: Brightness.dark,
primarySwatch: Colors.red,
backgroundColor: Colors.blue,
),
builder: (theme, darkTheme)=>MaterialApp(
title: 'Flutter Demo',
theme: theme,
darkTheme: darkTheme,
home: const MyHomePage(title: 'Flutter Demo Home Page'),
), initial: AdaptiveThemeMode.light,
);
}
}
I call MediaQuery.of(context).platformBrightness inside the MyHomePageState.
Any advices what I do wrong?
First make sure that you have added both ThemeModes in Material App itself:
Here's the example:
return MaterialApp(
title: "Dark",
debugShowCheckedModeBanner: false,
theme: ThemeData.copywith(brightness: Brightness.light),
darkTheme: ThemeData.copywith(brightness: Brightness.dark),
themeMode: ThemeMode.light,
);
Simpel answere to my question after a lot of research: In the current version ( 16.01.2022) MediaQuery.of(context).platformBrightness; is not the correct way to check if darkmode inside your app is enabled or not. This only works for checking which Theme the user uses on his system.
For checking on app level just use:
bool isDarkMode = Theme.of(context).brightness == Brightness.dark;
print(isDarkMode);
and it prints the correct value of the bool.
Let me know if you have a different information.

Getx flutter change theme when i change theme dark and then return light mode its not loading my custom theme

Getx flutter when i change theme from light to dark mode and then return to light mode its not returning my custom theme its load old app bar background even theme changes
ThemeData lightTheme = ThemeData(
useMaterial3: true,
backgroundColor: Colors.white,
appBarTheme: AppBarTheme(
elevation: 1000,
backgroundColor: Colors.white,
)
);
ThemeData darkTheme = ThemeData.dark().copyWith(primaryColor: Colors.red);
Getx flutter when i change theme from light to dark mode and then return to light mode its not returning my custom theme its load old app bar background even theme changes
class _MyMaterialAppState extends State<MyMaterialApp> {
#override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
title: appName,
theme: lightTheme,
darkTheme: darkTheme,
translations: Languages(),
locale: Get.deviceLocale,
fallbackLocale: const Locale('en', 'US'),
home: const MyHomePage(),
);
}
}
The documentation of GetX Itself says that you should not depend on any higher level widget than GetMaterialApp in order to update it. This can trigger duplicate keys.
I have seen your code and I tried to test this on physical devide and it works perfectly fine.
Here's the code:
import 'package:flutter/material.dart';
import 'package:get/route_manager.dart';
ThemeData lightTheme = ThemeData(
useMaterial3: true,
backgroundColor: Colors.white,
appBarTheme: AppBarTheme(
elevation: 1000,
backgroundColor: Colors.white,
));
ThemeData darkTheme = ThemeData.dark().copyWith(primaryColor: Colors.red);
void main(List<String> args) {
runApp(MyMaterialApp());
}
class MyMaterialApp extends StatelessWidget {
const MyMaterialApp({super.key});
#override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
title: "appName",
theme: lightTheme,
darkTheme: darkTheme,
// translations: Languages(),
locale: Get.deviceLocale,
fallbackLocale: const Locale('en', 'US'),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("data"),
),
body: Column(
children: [
Center(
child: TextButton(
onPressed: () {
Get.changeTheme(ThemeMode.dark);
// Get.changeThemeMode(ThemeMode.dark);
},
child: Text("Chage"),
),
),
Center(
child: TextButton(
onPressed: () {
Get.changeThemeMode(ThemeMode.light);
},
child: Text("Chage light"),
),
),
],
),
);
}
}
Probably, this is because of the GetMaterialApp widget in the build method. Because the build method is called every time the widget is updated, this means that your custom theme will be overwritten. You can do something like this:
class _MyMaterialAppState extends State<MyMaterialApp> {
GetxController<ThemeData> _themeController = GetxController(lightTheme);
#override
Widget build(BuildContext context) {
return GetBuilder<GetxController<ThemeData>>(
init: _themeController,
builder: (_, theme) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
title: appName,
theme: theme.value,
darkTheme: darkTheme,
translations: Languages(),
locale: Get.deviceLocale,
fallbackLocale: const Locale('en', 'US'),
home: const MyHomePage(),
);
},
);
}
}
This code uses GetBuilder to listen for changes to the _themeController and update theme property.
To change the theme:
_themeController.updateValue(newTheme)

No MaterialLocalizations found in flutter use GETX

when I tab on input to open datepicker dialog, it was error
Please help me , I use GETX Flutter
This is main.dart
#override
Widget build(BuildContext context) {
GetStorage box = GetStorage();
//print(box.read('lang'));
return GetMaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
locale: LocalizationService.getLocaleFromLanguage(
langCode: box.read('lang') ?? 'en'),
fallbackLocale: LocalizationService.fallbackLocale,
translations: LocalizationService(),
initialRoute: AppPages.INITIAL,
getPages: AppPages.routes,
);
}

How to change the `primary color` for ThemeData dark in flutter?

I am using flutter 2.8.0 to create a simple app.
But when I am trying to change the primary color for the App it does not work with me.
This is the code I am working on:
class BMICalculator extends StatelessWidget {
const BMICalculator({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
primaryColor: const Color(0xFF0A0E21),
scaffoldBackgroundColor: const Color(0xFF0A0E21),
),
home: const InputPage(),
);
}
}
#Mrinal already mentioned you to use primarySwatch. Flutter team intended to build clean up the Theme system and make everything more consistent and based off of the ColorScheme colors. They recommend using color scheme instead of primary color
theme: ThemeData(
primaryColor: ColorsX.primary,
colorScheme: ColorScheme.light().copyWith(primary: ColorsX.primary),
);
For dark theme
theme: ThemeData.dark().copyWith(
colorScheme: ColorScheme.light().copyWith(primary: Colors.tealAccent),
),
output:
For more read this article article
Use primarySwatch
theme: ThemeData(
primarySwatch: Colors.red,
),
I found a solution for me..
MaterialColor _primartSwatch = Colors.purple;
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MyApp',
themeMode: _themeMode,
theme: ThemeData(
primarySwatch: _primartSwatch,
),
darkTheme: ThemeData(
brightness: Brightness.dark,
primarySwatch: _primartSwatch,
),
home: SplashScreen(),
);
}

How to remove the elevation (shadow) in a SearchDelegate?

I want to remove the shadow from the search bar when using SearchDelegate but I can't find how to. Any tips?
This is basically all the code:
showSearch(
context: context,
delegate: CustomSearchDelegate(),
);
CustomSearchDelegate() just contains an empty search delegate widget/class.
#override
ThemeData appBarTheme(BuildContext context) {
return super.appBarTheme(context).copyWith(
appBarTheme: super.appBarTheme(context).appBarTheme.copyWith(
elevation: 0.0,
),
);
}
Add this inside the Searchdelegate class:
#override
ThemeData appBarTheme(BuildContext context){
assert(context != null);
final ThemeData theme = Theme.of(context);
assert(theme != null);
return theme.copyWith(
primaryColor: Colors.grey[50],
);
}
and this to the main app theme:
MaterialApp(
theme: ThemeData(
backgroundColor: Colors.white,
appBarTheme: AppBarTheme(elevation: 0.0), //This is important
title: 'Flutter Demo',
home: MyHomePage(),
);
Make changes accordingly in your ThemeData() and add the appBarTheme: AppBarTheme(elevation: 0.0), to remove the elevation and the SearchDelegate theme as it is your search delegate class which will ensure that the elevation defined in the ThemeData of your material app is implemented.
Note: I guess, that the elevation set in the ThemeData will affect the elevation of the appbar in other pages all over the app too.
Result:
Output image