Flutter get current time zone / code name is always en_US - flutter

I have a problem with my app...I can not get the correct current Locale of my app.
This:
print(Localizations.localeOf(context));
is always giving me:
en_US
but I am actually in Germany. I tried it on both Android and iPhone Simulator as well as on a real device. It is always giving me "en_US" :(
I create my MaterialApp like this:
MaterialApp(
title: 'ProjectX',
theme: ThemeData(
primarySwatch: ColorService.createMaterialColor(AppColors.secondary),
backgroundColor: AppColors.white,
scaffoldBackgroundColor: AppColors.white,
visualDensity: VisualDensity.adaptivePlatformDensity,
fontFamily: AppTextStyles.mainFont,
),
initialRoute: isOnline
? showOnboarding
? '/onboarding'
: '/'
: '/offline',
onGenerateRoute: AppRouter.generateRoute,
locale: Locale('de', 'DE'),
localizationsDelegates: [
AppLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
DefaultCupertinoLocalizations.delegate,
],
),
Is this a known bug? What am I missing here?

Add supportedLocales named argument to MaterialApp as stated in Internationalization documentation.
MaterialApp(
title: 'ProjectX',
theme: ThemeData(
primarySwatch: ColorService.createMaterialColor(AppColors.secondary),
backgroundColor: AppColors.white,
scaffoldBackgroundColor: AppColors.white,
visualDensity: VisualDensity.adaptivePlatformDensity,
fontFamily: AppTextStyles.mainFont,
),
initialRoute: isOnline
? showOnboarding
? '/onboarding'
: '/'
: '/offline',
onGenerateRoute: AppRouter.generateRoute,
locale: Locale('de', 'DE'),
supportedLocales: [Locale('de', 'DE')],
localizationsDelegates: [
AppLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
DefaultCupertinoLocalizations.delegate,
],
),

Is german the current language of the device?
In Locale, I think that it gets from keyboard language.
Like: if my keyboard language is US English, the locale will get en_US, even though I live in Brazil.

Related

Shared ThemeData between light and dark themes

I'm trying to avoid repeating my code.
My app has light and dark theme modes, and I'm trying to change my app theme in both modes without repeating lines, like the following code.
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
// shared ThemeData between light and dark themes :(
appBarTheme: const AppBarTheme(
toolbarHeight: 100,
),
),
darkTheme: ThemeData(
brightness: Brightness.dark,
// shared ThemeData between light and dark themes :(
appBarTheme: const AppBarTheme(
toolbarHeight: 100,
),
),
themeMode: ThemeMode.dark,
);
As I explained in my code, that there are few lines repeated in both light and dark themedata
// shared ThemeData between light and dark themes :(
appBarTheme: const AppBarTheme(
toolbarHeight: 100,
),
Is there a good and TESTED way to avoid this mistake?
First define your ThemeData as a variable, for example:
final myTheme = ThemeData(
// brightness: Brightness.light,
primarySwatch: Colors.red,
primaryColor: Colors.blue,
cardColor: Color(0xCCF2F2F2),
);
You can then use your variable. If you want to modify a few items, you can use the copyWith constructor. For example:
MaterialApp(
// light theme: use the variable as is
theme: themeData,
// dark theme: need to modify a few things
darkTheme: themeData.copyWith(
brightness: Brightness.dark, // change brightness
),
home: MyHomePage(),
)
make a ThemeData variable and put all the shared properties then use copyWith when you want to change something like this:
ThemeData _themeData = ThemeData(
primarySwatch: Colors.indigo,
appBarTheme: AppBarTheme(
color : Colors.deepOrange,
),
);
then :
theme: _themeData.copyWith(
brightness: Brightness.light,
),
darkTheme: _themeData.copyWith(
brightness: Brightness.dark
),
themeMode: ThemeMode.light,
you can also use ThemeData.from()

how to add font family into in main.dart flutter

I'm new to flutter and struggling to add font family in my code..already updated my pubspec.yamal with OpenSans fonts.. now how to add font family into this main. dart material app widget??
fontFamily: "OpenSans",
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: theme.copyWith(
colorScheme: theme.colorScheme.copyWith(primary: Color(0xff075e54),secondary: Color(0xff128C7E),
),
),
home: Homescreen(key: null),
);
}
}
You can try this
theme: theme.copyWith(
colorScheme: theme.colorScheme.copyWith(primary: Color(0xff075e54),secondary: Color(0xff128C7E)),
textTheme: theme.textTheme.apply(fontFamily: "OpenSans"),
),
Create a simple file for the theme like the one I created theme_util.dart and use it as follows.
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: appTheme,
home: Homescreen(key: null),
);
theme_util.dart
ThemeData appTheme = ThemeData(
backgroundColor: appColor.white,
scaffoldBackgroundColor: appColor.white,
appBarTheme: AppBarTheme(
color: appColor.white,
brightness: Brightness.light,
textTheme: TextTheme().copyWith(headline6: titleStyle),
iconTheme: IconThemeData(color: appColor.primaryColor)),
fontFamily: 'Opensans',
primaryColor: appColor.primaryColor,
primaryColorDark: appColor.primaryDarkColor,
primaryColorLight: appColor.primaryLight,
accentColor: appColor.accentColor,
brightness: Brightness.light,
//primarySwatch: appColor.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
buttonTheme: ButtonThemeData(buttonColor: appColor.primaryColor),
);
You can create a style file in your dart folder and use the font where ever you want like this way-text_styles.dart
class TextStyles {
static TextStyle overline(
{required BuildContext context, required Color color}) {
return GoogleFonts.montserrat(
textStyle: Theme.of(context).textTheme.overline?.copyWith(color: color),
);
}
}
and use it as-
TextStyles.overline(context: context, color: theme.textColor).copyWith(fontWeight: FontWeight.w900),
Note:if you use custom font you have to install the package from your pubspec.yaml.Example-
google_fonts:

How to change initial language of flutter app?

I am trying to create Arabic flutter app and the default language of flutter app is English , I want the app to start with Arabic language , So how can I achieve this ?
return MaterialApp(
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale('en'),
const Locale('ar'),
],
locale: const Locale('ar'),
);

Flutter Widgets don't get overridden accent color

I have a dynamic theme in my app.
I set the theme using the following code:
final ThemeData base = ThemeData(brightness: Brightness.light, fontFamily: 'Simpler');
return base.copyWith(
primaryColor: AppColors.appColor,
accentColor: AppColors.secondaryColor,
accentColorBrightness: Brightness.light,
accentIconTheme: IconThemeData(color: Colors.black)
);
This is set as the theme in my app
child: MaterialApp(
title: Provider.of<AppConfig>(context).appName,
locale: selectedLocale.locale,
theme: currentTheme.data,
initialRoute: '/',
onGenerateRoute: routes,
localizationsDelegates: [
AppLocalizationsDelegate(),
FallbackCupertinoLocalisationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
Locale('en', 'US'),
],
),
The docs show that widgets such as a fab or a switch should be default be set to the themes accent color but for some reason the color the widgets get is the system default light blue accent color.
When I debug and check to see the accent color in the Theme.of(context) I see that the color is correct...
If I specificaly add the color change to the custom theme such as
floatingActionButtonTheme: FloatingActionButtonThemeData(
backgroundColor: AppColors.secondaryColor,
foregroundColor: Colors.black,
),
The color is set as it should but I don't think that I should override all the properties in the ThemeData, it just dose'nt make sense.

How to change cursor color in flutter

Dears,
I have 2 qestions in flutter If you don't mind.
1- How to change the color of the cursor as it default blue and I don't like it
2- how can I make the text at the bottom of the screen whatever the screen size. ??
Thank you in advance.
put cursorColor: Colors.white, inside the TextFormField
This works fine in both iOS and Android:
TextField(cursorColor: Colors.white)
But, if you like to set it in theme then
SOLUTION 1 - Original answer, recently not tested, also, seems it's deprecated:
I found the solution here:
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
MaterialApp(
title: "Rate your Colleagues",
theme: ThemeData(
// for iOS
cupertinoOverrideTheme: CupertinoThemeData(
primaryColor: Colors.red,
),
// for others(Android, Fuchsia)
cursorColor: Colors.red,
home: SplashScreen(),
);
...
SOLUTION 2 - edited answer - not tested - please give credit to #i4guar
I found the solution here:
MaterialApp(
title: "Rate your Colleagues",
theme: ThemeData(
textSelectionTheme: TextSelectionThemeData(
cursorColor: darkPrimarySwatchColor,
selectionColor: darkPrimarySwatchColor,
selectionHandleColor: darkPrimarySwatchColor,
),
),
home: SplashScreen(),
);
cursorColor is now deprecated for ThemeData use this instead (works on both iOS and android):
MaterialApp(
title: "Rate your Colleagues",
theme: ThemeData(
textSelectionTheme: TextSelectionThemeData(
cursorColor: darkPrimarySwatchColor,
selectionColor: darkPrimarySwatchColor,
selectionHandleColor: darkPrimarySwatchColor,
),
),
home: SplashScreen(),
);
Flutter has been updated, and now the cursorColor is used like this:
ThemeData(
...
textSelectionTheme: TextSelectionThemeData(
cursorColor: Colors.blue, //thereby
),
),
For question 1 you can set the cursorColor for theme attribute when calling MaterialApp like the below
new MaterialApp(
title: "Flutter App",
theme: ThemeData(
cursorColor: Colors.red,
home: HomeScreen(),
)
I had to set useTextSelectionTheme to true and set textSelectionTheme for my custom dark theme:
ThemeData _defaultDarkTheme = ThemeData.dark();
ThemeData _darkTheme = initializeDefaultLineHeight(ThemeData(
brightness: Brightness.dark,
// How to set cursor color for TextFormField
useTextSelectionTheme: true,
textSelectionTheme: _defaultDarkTheme.textSelectionTheme.copyWith(
cursorColor: Colors.grey[600]),