No MaterialLocalizations found in flutter use GETX - flutter

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,
);
}

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.

Where is the themeMode property in CupertinoApp

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(),
);
}

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(),
);
}

Change application's starting dart file in flutter

I am new for flutter and I want to change the starting UI(interface). I tried to find out . but could not do it
Here the home: parameter specifies the starting page. If you want to change the page that will open at startup, you can give the name of the page you want instead of HomePage ().
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Time Tracker',
theme: ThemeData(
primarySwatch: Colors.indigo,
),
home: HomePage(),
debugShowCheckedModeBanner: false,
),
);
}
}
So:
home: HomePage(),
Change to:
home: AnythingPage()

How to implement a side-slide close page on the TargetPlatform.android?

Flutter setting TargetPlatform.iOS
new MaterialApp(
title: 'Mian',
theme: new ThemeData(
primarySwatch: Colors.orange,
platform: TargetPlatform.iOS,
)
)
Can skip the close page.
Flutter setting TargetPlatform.android
new MaterialApp(
title: 'Mian',
theme: new ThemeData(
primarySwatch: Colors.orange,
platform: TargetPlatform.android,
)
)
Can‘t skip the close page.
You can do it using CupertinoPageRoute when you push the Widget.
First you have to import cupertino:
import 'package:flutter/cupertino.dart';
Then use the Navigator:
Navigator.of(context)
.push(CupertinoPageRoute(builder: (context) => YourNewWidgetPage()));
By reading the article I have new answers to update, this way is more friendly.
enter code class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
pageTransitionsTheme: PageTransitionsTheme(builders: {
TargetPlatform.android: CupertinoPageTransitionsBuilder(),
TargetPlatform.iOS: CupertinoPageTransitionsBuilder(),
})),
routes: {
"/": (BuildContext context) =>
MyHomePage(title: 'Flutter Demo Home Page'),
"/two": (BuildContext context) => TwoPage(),
},
);
}
}
Thanks to Arvinth for the article:https://medium.com/flutter-community/page-transitions-using-themedata-in-flutter-c24afadb0b5d