Recommended Dark/Light or custom Theme implementation in Flutter Cupertino - flutter

I’m currently implementing an iOS style app in flutter using the CupertinoApp class and Cupertino widgets. Now I would like to implement a different Theme for the application (like feedly did), so that the user can switch between both during runtime.
Unlike a MaterialApp, CupertinoApp has no property to set the theme.
MaterialApp with theme property:
void main() => runApp(MyApp());
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,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
CupertinoApp without theme property:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return CupertinoApp(
title: 'Flutter Demo',
// theme: ThemeData(
// primarySwatch: Colors.blue,
// ),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
It looks like sb. is working on a CupertinoTheme which actually seems to be a Theme for a MaterialApp and not for a CupertinoApp.
What's the recommended way to define themes and change them during runtime in a Cupertino style app (e.g. Dark and Light Theme)?

First of all CupertinoThemeData is an alternative for ThemeData:
import 'package:flutter/cupertino.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return CupertinoApp(
title: 'Flutter Demo',
theme: CupertinoThemeData(),
home: MyHomePage(),
);
}
}
CupertinoThemeData has a 'raw' property which contains these properties:
Brightness, primaryColor, primaryContrastingColor, textTheme and so on.
Would you want to choose either dark or light? that's too easy!!
CupertinoThemeData.raw(Brightness.light)
CupertinoThemeData.raw(Brightness.dark)
but if you want to change it during runtime you should write a function:
CupertinoThemeData generalTheme(Brightness brightness) {
CupertinoThemeData _basicCupertinoTheme = CupertinoThemeData.raw(
brightness,
...
}
As you see, you can send an argument to function but don't forget to use setState method.
For more info check out CupertinoThemeData.raw constructor

you can use the following package to control dark and light themes, also the theme is saved directly into shared preferences so when you reopen the application, the latest theme you chose will be loaded,use the package as provided in the read me section.
https://pub.dartlang.org/packages/dynamic_theme

Related

Why MyApp and MaterialApp are underlined? The code is from initial flutter demo project

Started my new project - got always MaterialApp underlined. Requires bottomNavigationBar. I tried it differently: started a demo project in flutter - MyApp and MaterialApp are underlined as well. So i cannot run the project.
Here is the initial code of demo project:
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),);
}
}```
Please, tell me, what it wants? It is a code , made by flutter, i mean...
MyApp MyApp({Key? key})
package:project/main.dart
**2 positional argument(s) expected, but 1 found.
Try adding the missing arguments.**
Thank you
have you imported material app? your code should look like:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: MyTheme.appTheme,
debugShowCheckedModeBanner: false,
home: const HomeScreen(),
);
}
}

Getx controller not found even after Get.put() | Flutter | Getx

"YouController" not found. You need to call "Get.put(YouController())" or
"Get.lazyPut(()=>YouController())"
Getting this error even after I have Get.put() before coming to the screen.
Any Idea?
You can try this:
create a file named initial_bindings.dart in your lib folder. Then add this snippet to it.
import 'package:get/get.dart';
class InitialBinding implements Bindings {
#override
void dependencies() {
Get.lazyPut<YouController>(() => YouController(),fenix: true);
}
}
Then in your main.dart, add this line into your GetMaterialApp.
class MyApp extends StatelessWidget {
const MyApp({super.key});
#override
Widget build(BuildContext context) {
return GetMaterialApp(
initialBinding: InitialBinding(), // add this line
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
Then whenever you want to access the controller, you can just use Get.find<YourController>() to use it.

Flutter title doesn't change

I'm trying to change the title but despite the fact I changed the title it still shows the previous title on the tab of the browser.
void main() {
runApp(Myapp());
}
class Myapp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'New title ', : but still shows the old title on the browser
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
scaffoldBackgroundColor: Colors.black,
),
home: NavScreen(),
);
}
}
If you are changing the title during the run time, you just need to wrap it with the stateful Widget.
If your App title is static and does not change after a build, try clearing the flutter cache and rebuild your application again.

Provider Error: Could not find the correct Provider<Color> above this MyApp Widget

I do not understand why the following code generates the error?
According to me the provider is an ancestor to MaterialApp and it should work? What am I missing?
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
new Provider<Color>(create: (_) => Colors.amber)
],
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Provider.of<Color>(context),
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
)
);
}
}
Provider initially in build method. You don't access to use provider value because provider inits context, but you tried to use provider.of(context) in the same place. If you use another state(view), you should use it.
Example code : https://gist.github.com/VB10/358dc944e5a86b58db2d898eaa1b5ea6

Change background color used in Recents apps screen with Flutter app

How can I change the color shown in the app switcher of Android. Right now the color comes up with a gray background, but I want it to be red.
This is the app code.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
color: Colors.red,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Material(
child: Text('Check color in app switcher.'),
),
);
}
}
The color parameter of MaterialApp has no effect.
If you are using the MaterialApp you would use the color attribute to change the color of the app bar in the switcher.
You could also use the SystemChrome.setApplicationSwitcherDescription method which you would find in package:flutter/services.dart