I'm trying to determine the default line style in a flutter data table
Looking at the source code is beyond my grasp -
https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/material/data_table.dart
I thought it might be Border(bottom: new BorderSide(width:1.0, color:Colors.grey.withOpacity(0.3))) but that's just eyeballing it imprecisely.
final BorderSide borderSide = Divider.createBorderSide(
context,
width: dividerThickness
?? theme.dataTableTheme.dividerThickness
?? _dividerThickness,
);
final Border? border = showBottomBorder
? Border(bottom: borderSide)
: index == 0 ? null : Border(top: borderSide);
According to this
Related
I don't know how to change the grey color of overdue dates. For me there is not enough contrast between past date, future date and today's date.
Looking at the source code of of CupertinoDatePicker, it seems that the colors are not easily changeable.
To quote from flutter's source code:
TextStyle _themeTextStyle(BuildContext context, { bool isValid = true }) {
final TextStyle style = CupertinoTheme.of(context).textTheme.dateTimePickerTextStyle;
return isValid
? style.copyWith(color: CupertinoDynamicColor.maybeResolve(style.color, context))
: style.copyWith(color: CupertinoDynamicColor.resolve(CupertinoColors.inactiveGray, context));
}
Therefore, the only direct impact you have on the colors is by wrapping your CupertinoDatePicker inside a CupertinoTheme and providing a value for textTheme.dateTimePickerTextStyle . Further customisation is not supported as of yet
Since there is no option to change the overlay color from the CupertinoDatePicker widget constructor, you will need to change the overlay color from its source code.
First, we will take an example of CupertinoDatePicker like this:
// ...
child: CupertinoDatePicker(
onDateTimeChanged: (date) {},
),
// ...
And it shows this on the screen:
Going inside its source code, you need to search for those lines:
const Widget _startSelectionOverlay = CupertinoPickerDefaultSelectionOverlay(capEndEdge: false);
const Widget _centerSelectionOverlay = CupertinoPickerDefaultSelectionOverlay(capStartEdge: false, capEndEdge: false);
const Widget _endSelectionOverlay = CupertinoPickerDefaultSelectionOverlay(capStartEdge: false);
This represents the implementation of that grey overlay that you see on the screen.
Then, you need to go through the CupertinoPickerDefaultSelectionOverlay source code also, you should find this:
/// default margin and use rounded corners on the left and right side of the
/// rectangular overlay.
/// Default to true and must not be null.
const CupertinoPickerDefaultSelectionOverlay({
super.key,
this.background = CupertinoColors.tertiarySystemFill,
this.capStartEdge = true,
this.capEndEdge = true,
}) : assert(background != null),
assert(capStartEdge != null),
assert(capEndEdge != null);
We want to change the color of that overlay background, so in this line:
this.background = CupertinoColors.tertiarySystemFill,
We will change its value with the Color we want, so as an example we will change it like this:
this.background = const Color.fromARGB(49, 0, 253, 30),
and after a hot restart, the result will be:
Notes:
If you want to use material colors like Colors.red,
Colors.green..., you will need to import the material library on
the top of that file.
You should mark the color with a const as I did in the sample of
code, otherwise, you will get an error.
I have defined a variable that holds a color value and when I setstate the screen, I always change this color. I want to use this color both as normal and shade100 in my application. However, my code is giving an error below.
late Color objeColor;
objeColor = Colors.orange;
Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
color: objeColor, // there is no error *******
),),
Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
color: objeColor.shade100, // there is error *******
),),
error
The getter 'shade100' isn't defined for the type 'Color'.
Try importing the library that defines 'shade100', correcting the name to the name of an existing getter, or defining a getter or field named 'shade100'
shade100 is a getter for MaterialColor. You can do
late MaterialColor objeColor;
...
color: objeColor.shade100
You can't change the shade of the color again in the widget tree. Though you've got plenty of other methods to manipulate the color with. For example, objeColor.withAlpha(75) looks exactly like Colors.orange.shade100 so it would perfectly fit your needs. In other usecases you can try to experiment with other methods, like withOpacity().
I would like to change the divider color in a DataRow in a DataTable, only for the rows and not for the header row.
I can wrap the DataTable in a Theme widget to change the color
Center(
child: Theme(
data: Theme.of(context).copyWith(dividerColor: Colors.grey),
child: DataTable(columns: [
DataColumn(
//....
but this affects also the headingRow
is there a way to prevent this? thank you
This is not possible using the default DataTable because DataTable uses the same Divider for every row including the header row.
But if it is really necessary, I have changed a little bit of code in the data_table.dart file that defines DataTable. this will get you what you want although changing the library files is not good practice:
Add this instead of line 893 - 909:
final BorderSide borderSide = Divider.createBorderSide(
context,
color: Colors.red, // Add this color for your data rows.
width: dividerThickness
?? theme.dataTableTheme.dividerThickness
?? _dividerThickness,
);
final BorderSide borderSide2 = Divider.createBorderSide( // Adding this divider for the header row.
context,
color: Colors.blue, // The color of your header row divider.
width: dividerThickness
?? theme.dataTableTheme.dividerThickness
?? _dividerThickness,
);
final Border? border = showBottomBorder
? Border(bottom: borderSide)
: index == 0 ? null : index == 1 ? Border(top: borderSide2) : Border(top: borderSide); // Changed this that the first row uses the other divider.
The result:
the result
I need to show the color from in my container. Mean i have color codes in string format and i need to use as the Container Color.
decoration: BoxDecoration(
color: widget.product.colors[i].toColor(),
borderRadius: BorderRadius.all(Radius.circular(40)),
This is the simple code i am doing like this but its showing error
Invalid argument(s): Can not interpret string 0xFFF6625E
If i remove the .toColor() then its showing
Error: The argument type 'String' can't be assigned to the parameter type 'Color'.
Can any one please tell how can i show this ?
You can use below code:
decoration: BoxDecoration(
//make sure that the widget.product.colors[i] is a hex code (i.e: "0xff0000")
color : Color(int.parse(widget.product.colors[i])),
borderRadius: BorderRadius.all(Radius.circular(40)),
border: Border.all(color: Color(int.parse(kPrimaryColor)))
You can write a method like this..
static Color hexToColor(String code) {
if(code.length == 6) {
return Color(int.parse(code.substring(0, 6), radix: 16) + 0xFF000000);
}
else {
return Color(int.parse(code.substring(0, 8), radix: 16) + 0x00000000);
}
}
You could use Supercharged package:
"#ff00ff".toColor(); // painless hex to color
"red".toColor(); // supports all web color names
You can try this:
color: const Color(widget.product.colors[i]),
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,
));