Flutter Change Colors by Theme - flutter

I add this package to my project.
This works but I want to change the color of a container, for example, depending on which theme is selected. How can I do that?

Create a helper method getThemeColor as follows:
Color getThemeColor({#required Color darkThemeColor,#required Color lightThemeColor}) {
return Theme.of(context).brightness == Brightness.dark ? darkThemeColor : lightThemeColor;
}
And then use this wherever you want to set your color:
Container(
color: getThemeColor(darkThemeColor: Colors.red, lightThemeColor: Colors.green),
),

Related

SlideAction Icon color need to change - Flutter

Using below package in Flutter :
https://pub.dev/packages/flutter_slidable/example
There in code you will find below widget:
SlidableAction(
onPressed: doNothing,
backgroundColor: Color(0xFF0392CF),
foregroundColor: Colors.white,
icon: Icons.save,
label: 'Save',
),
Now Please note that icon property has Icons.save which is inbuilt and coming from flutter inbuilt sdk package.
Now, I want to change the color of that Icon. I have tried using Icons() widget but it only accept Icons.any_inbuild_icon name as a value.
How can I change the color of that Slidable Icon? Thanks in advance.
foregroundColor is the color of the icon. so change the value of foreground and it works for me.
foregroundColor: Colors.white,
CHANGE TO WHATEVER COLOR YOU WANT
example:
Colors.red or pass hex value Color(0xDE00FF00)
VIEW CODE
VIEW RESULT

Is there a way to change the outline border color of the OTP fields using PinCodeTextField plugin

I'm new to Flutter. I'm using PinCodeTextField https://pub.dev/packages/pin_code_fields plugin to create OTP Text field I want the text field to be in different color but I couldn't find the decoration property to change the colour.
Spent hours looking for the same problem and finaly found a way around.
Searck for a file ‘otp_field_style.dart’ through find.
Open the file and you will see following:
import 'package:flutter/material.dart';
import 'package:<myApp’s Constants>/constants/constants.dart';
class OtpFieldStyle {
/// The background color for outlined box.
final Color backgroundColor;
/// The border color text field.
final Color borderColor;
/// The border color of text field when in focus.
final Color focusBorderColor;
/// The border color of text field when disabled.
final Color disabledBorderColor;
/// The border color of text field when in focus.
final Color enabledBorderColor;
/// The border color of text field when disabled.
final Color errorBorderColor;
OtpFieldStyle(
{this.backgroundColor: kFillShade7,
this.borderColor: kOrange4,
this.focusBorderColor: kOrange1,
this.disabledBorderColor: kFillShade4,
this.enabledBorderColor: kFillShade2,
this.errorBorderColor: Colors.red});
}
Now, you can change the colors by giving the hard values,
OR
Import your constants.dart file (where one normally defines the constants, including colors etc) in this and link the colors through it e.g,
import 'package:/constants/constants.dart';
is my contants file, I have linked my kColors in above code,
just change it to appropriate link for your constants dart file.
5. Save and close ‘otp_field_style.dart’.
You can change the colors through your defined constants, I hope it helps!
you can use this properties to change the colors
pinTheme: PinTheme(
shape: PinCodeFieldShape.box,
borderRadius: BorderRadius.circular(5),
fieldHeight: 50,
fieldWidth: 40,
activeFillColor: Colors.black,
inactiveColor: Colors.deepOrange,
inactiveFillColor: Colors.green,
selectedFillColor: Colors.deepPurple,
selectedColor: Colors.greenAccent,
activeColor: Colors.blue
),

Add custom property to ThemeData in Flutter

I need to change color of a widget based on theme. I have separate ThemeData for light and dark theme. Now is it possible to add a custom property to ThemeData so that I can change the color of the widget based on theme and using that custom property?
Sadly, it appears that you simply can't - and the Flutter team doesn't seem to be interested in adding this, given their suggestion.
I think this is a major flaw, because we can't benefit from Theme.of(context) to automatically update all of our Widget that consume this ThemeData.
While some may say that you can use extensions to add new properties, you effectively won't know how to differ between multiple ThemeData (unless you can use some properties like Brightness, but I think this is just too hacky and not reliable to do so).
The alternative is to create another InheritedWidget (just like they said in the issue mentioned above) to handle your custom theme properties.
Edit: It seems that a new PR has introduced the possibility to extend the ThemeData, but it hasn't landed in main or even stable yet.
Flutter has recenty introduced ThemeExtensions (thanks #guilherme-matuella for the PR link!)
You can get a pretty good idea on how to use the functionality by following examples from the main Flutter repo:
return MaterialApp(
title: MyApp._title,
theme: ThemeData.light().copyWith(
extensions: <ThemeExtension<dynamic>>[
const MyColors(
brandColor: Color(0xFF1E88E5),
danger: Color(0xFFE53935),
),
],
),
darkTheme: ThemeData.dark().copyWith(
extensions: <ThemeExtension<dynamic>>[
const MyColors(
brandColor: Color(0xFF90CAF9),
danger: Color(0xFFEF9A9A),
),
],
),
themeMode: isLightTheme ? ThemeMode.light : ThemeMode.dark,
home: Home(
isLightTheme: isLightTheme,
toggleTheme: toggleTheme,
),
);
Which you can later retrieve in your widget like so:
final MyColors myColors = Theme.of(context).extension<MyColors>();
Instead of adding custom property, we can extend ThemeData by extension function. For example, if we need a custom property for color, we can add extension function on ColorScheme. Color dependencies are now moved to Themedata.
// checking brightness to support dynamic themeing
extension CustomColorSchemeX on ColorScheme {
Color get smallBoxColor1 =>
brightness == Brightness.light ? Colors.blue : Colors.grey[400];
}
And then access that property through Theme.of(context)...
Container(
decoration: BoxDecoration(
border: Border.all(
color: Theme.of(context)
.colorScheme
.smallBoxColor1),
),

Color vs Colors in Flutter

I'm writing a function in flutter like this
Expanded playButton({Color colorName, int buttonNumber, int soundNumber}) {
return Expanded(
child: FlatButton(
color: colorName,
onPressed: () {
final player = AudioCache();
player.play('note$soundNumber.wav');
},
child: Text(
'> PLAY $buttonNumber',
style: TextStyle(
fontSize: 35.0,
color: Colors.white,
),
),
),
);
}
Why the argument should be Color colorName instead of Colors colorName.
As we use color: Colors.teal for defining color.
I think the class Colors is a programmer friendly wrapper of the Color class.
As you can see in the source code of the Colors class:
class Colors {
// This class is not meant to be instatiated or extended; this constructor
// prevents instantiation and extension.
// ignore: unused_element
Colors._();
/// Completely invisible.
static const Color transparent = Color(0x00000000);
...
}
Here the 'Colors' class defines a variable transparent which we could easily use like
...
Container(
color: Colors.transparent,
)
...
So you always use a Color instance for a 'color' and therefore you have to define the variable as a Color instance, even if you use the Colors class wrapper.
Colors are predefined instances of the class Color.
It's like this...
Colors is to Color as Mercedes is to Car. Not every Car is a Mercedes. And not every Color object is a Colors object.
Btw, you can use Color to get the exact color you want, like...
const color = const Color(0xffb74093);
So it's much more versatile than Colors

Check for color during widget test?

The goal is to verify the color of a RaisedButton.icon
During my widget tests, I have the ability to look for text with find.text as well as icons with find.byIcon. There is no built in method for finding color.
How does one do the equivalent of find.color?
And example of my code is
RaisedButton.icon(
color: Color(_isAttendingEvent ? 0xFFD9FFB3 : 0xFFE3FFC7),
icon: Icon(_isAttendingEvent ? Icons.star : Icons.star_border),
label: Text(
_isAttendingEvent ? 'Attending' : 'Attend',
),
),
I'm trying to determine whether there is color: Color(0xFFD9FFB3) or color: Color(0xFFE3FFC7)
And I'm not sure if this is possible
I am not sure with RaisedButton.icon, but if it is just an icon you can try using:
expect((tester.firstWidget(find.byType(Icon)) as Icon).color, Color(0xFFE3FFC7));
If it not the first icon widget that appears on your screen, you can specific an index of it by:
expect((tester.widget(find.byType(Icon).at(/*index*/)) as Icon).color, Color(0xFFE3FFC7));
Note: To find a widget colour, you need to cast that widget to be something that contain a color property
For example:
To find Material color you can use:
expect((tester.firstWidget(find.byType(Material)) as Material).color, Colors.black);
To find Container with BoxDecoration color:
expect(((tester.firstWidget(find.byType(Container)) as Container).decoration
as BoxDecoration).color, Colors.black);
To find Text color:
expect(((tester.firstWidget(find.text('text')) as Text).style).color, Colors.black);
To find Appbar Material. color
final appbar = tester.widget<AppBar>(find.byKey(Key("appbar")));
expect(appbar.backgroundColor,Colors.white);
To find Text color Material
final text = tester.widget<Text>(find.text("text"));
expect(text.style.color, Colors.grey);
expect(text.style.fontSize, 15);
Update you flutter version. It's working now, i am able to access color in widget test. My current flutter version is "Flutter 1.15.18 • channel dev".
Earlier it was not working with version "Flutter v1.12.13+hotfix.5"
Hope this will help. More details here
If you're using a TextButton, ElevatedButton, or Outlined button, the background color will be a MaterialStateProperty. You can verify the color in almost the same way as mentioned above:
expect(
tester.widget(textButton.first),
isA<TextButton>().having(
(w) => w.style?.backgroundColor?.resolve({}),
'button color',
Colors.grey,
));