Understanding some flutter design decisions - flutter

Apologies if this question doesn't belong here but some flutter design choices seem unintuitive to me and if you all could help me in understanding why it's done like this, I feel I would have a better grasp of this framework.
Also, I come from a webdev background, therefore CSS type styling makes sense to me so that could be another reason why this type of styling seems so different.
I haven't done a lot of flutter but from my limited experience I have the following questions:
(I realize that some of it may not be optimal and hence the reasons for my confusion might be me just doing it incorrectly, in which case please do correct me)
This is the code I wrote for changing the background color of a button
TextButton(
child: Text('Example'),
onPressed: onPressedFunction,
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(Colors.SomeMaterialColor)
)
)
or alternatively
TextButton(
child: Text('Example'),
onPressed: onPressedFunction,
style: TextButton.styleFrom(backgroundColor: Colors.SomeMaterialColor),
)
Q1 Why do I have to specify that the style will be of type ButtonStyle(....), any style that goes into the button class should be of type ButtonStyle right?
Q2 The reason why setting backgroundColor isn't as straightforward as backgroundColor: CustomColor is because "backgroundColor isn't Colortype, so you can't use Colors.YourColor" , but why not? what advantage do you get by not making it ColorType
When changing the theme of the app, this is the code that I wrote
theme: ThemeData(
colorScheme: ColorScheme.fromSwatch(
primarySwatch: Colors.red,
).copyWith(
secondary: Colors.purple,
),
),
Q3. Using fromSwatch and copyWith seems convoluted, why isn't it something as simple as
ThemeData(
colorScheme: ColorsSchemeClass(
primarySwatch: Colors.red,
secondaryColor: Colors.purple,
),
)
I am sure there are excellent reasons for this type of design but I can't seem to understand them, therefore any help would be greatly appreciated.

First of all It's all depending on what is called Material Design by Google. It has certain guidelines that must be followed to achieve maximum Flutter theming advantage.
Q1: Dart is hard-type language in general. And the use cases is different form widget to another, with some similarities. for example Button style has on hover color or splash color. while Text doesn't need this property. But both of them has foreground color.
Q2: As I mentioned before, Material Design is essential for the Widget that flutter provide. you can make your custom widget the can take Colors directly but I would not recommend this approach.
Q3:In Material Design concept, There is two main type of colors, Primary and accent colors, That's makes Flutter deal with Color Swatch. For your example First create red color swatch and copy it's property with changing the secondary color to purple.
For more information with Material Design in general read this
link
How to implement it in flutter read this article

Related

Flutter's applications default color

I'm making a Welcome Screen which has two TextButtons each wrapped in a Container.
one for Creating an account and another for logging in
(both of them have some shadow below them )
I managed to make both containers in flutter but I found out that the background color of the application is not pure white (#FFFFFF) which mean if I set the color of the login container to Colors.white it won't look like the background color of the app like above picture.
So I need a way to set the color of the login container to the same color as the application.
let's avoid hard coding I don't want to determine the background color with an external tool and set it to the button.
I was thinking of taking same color as parent or something like that but I don't know if that exists.
main.dart
WelcomeScreen.dart
The scaffold background color is Grey[50]. You can set background color on scaffold like
Scaffold(
backgroundColor: Colors.white,
Or for app
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: Theme.of(context).copyWith(
scaffoldBackgroundColor: Colors.white,
),
More about Theme.

How to change status bar / navigation bar color/brightness with respective to theme?

Using Getx for theming, but facing some issue with changing status bar icon brightness based on light / dark theme.
I found two ways.
First.
WidgetsBinding.instance!.addPostFrameCallback((_) {
SystemChrome.setSystemUIOverlayStyle(overlayStyle);
});
This need to be called during initState().
Second.
AnnotatedRegion<SystemUiOverlayStyle>()
This widget also does work properly.
Is there any other method which can be implemented for this purpose?
P.S. Using Flutter 2.8.0
You can use ThemeData to change brightness of the whole app based on dark/bright.
All your themes customisation can be done using this. Below is an example for brightness and primary color.
ThemeData(
brightness: Brightness.dark, // or Brightness.light (for dark or light)
// .........
primarySwatch: Colors.orange, // for changing primary color
),

MaterialApp ThemeData iconTheme

I have some ListTile widgets around my app, and all of them have an icon.
From here I see that I need to override my ListTile with
ListTileTheme(
iconColor: Colors.blue,
child: ..
)
since the ListTile.iconColor is gray by default and doesn't fallback to ThemeData.iconTheme.iconColor.
I wonder if there is a way to specify the list tile theme in ThemeData, so I don't have to create a new widget just for that.
For now, there isn't, but this feature may arrive in the next flutter release: https://github.com/flutter/flutter/issues/31247

How do I change text selection handle color for iOS in Flutter?

I'm using the following theme code:
ThemeData.dark().copyWith(
accentColor: Colors.green,
textSelectionColor: Colors.green.withOpacity(0.5),
textSelectionHandleColor: Colors.green,
);
And that works for android, but for iOS it is not changing the color of the text selection handle color to green (it is still the default blue). How can I change that color for iOS?
I was able to change the color using Themes. You need to set cupertinoOverrideTheme like this
CupertinoThemeData(
primaryColor: Colors.green,
)
it seems that this is a known issue.
It would appear that TextField on iOS ignores the MaterialTheme values for this.
Another piece of information which suggests that this is not possible in iOS is looking at the CupertinoThemeData documentation, which clearly does not consider a textSelectionHandleColor.

PrimarySwatch, accentColor and canvasColor

Can someone briefly explain to me where are all the three themes(Primary Swatch, accent Color and canvas Color) used in flutter?
I find extensive use of these themes but am very confused which widgets use which theme?
primarySwatch: is a main color of app for example it change the color of appbar
accentColor: or secondary color is The foreground color for widgets for example floatingActionButton
canvasColor: and this can change the color of scaffold widget
You first need to learn Material Design to understand them. Check this.
primarySwatch is a MaterialColor, not a color. See this.
accentColor is a secondary color. It might be used for active tab, focused input texts, checked boxes etc.
canvasColor is the default color of MaterialType.canvas Material. Here.