I need to test the following piece of code to basically check if trailingIconButton == null then a certain text theme should be applied.
Text(
type == TileType.org
? orgInfo!.name!
: type == TileType.user
? '${userInfo!.firstName!} ${userInfo!.lastName!}'
: option!.title,
style: type == TileType.org
? Theme.of(context).textTheme.headline5
: type == TileType.user
? Theme.of(context).textTheme.headline6
: option!.trailingIconButton == null
? Theme.of(context).textTheme.bodyText2
: Theme.of(context)
.textTheme
.headline5!
.copyWith(fontSize: 18),
key: const Key('trailingIconButton'),
),
what I wrote
testWidgets('Creating Custom List (giving custom options)', (tester) async {
await tester.pumpWidget(createCustomListTileUser(
option: Options(
icon: const Icon(Icons.add),
title: 'henlo',
subtitle: 'yesh',
trailingIconButton: null)));
final Text text =
tester.firstWidget(find.byKey(const Key('trailingIconButton')));
//equals bodyText2
expect(text.style!.getTextStyle(),
equals(Theme.of(MockBuildContext()).textTheme.bodyText2));
});
The problem is that you're using different contexts to get theme which results in different themes. In your application you probably have defined a Theme which is returned when you're using Theme.of(context) but that's not the case in your tests. When you're doing Theme.of(MockBuildContext()) You're getting different one.
So to solve this you need to wrap your widgets with a theme provider with the same theme used in your application. Then use this code to compare themes:
expect(
text.style!.getTextStyle(),
equals(Theme.of(
tester.element(find.byKey(const Key('trailingIconButton'))))
.textTheme
.bodyText2));
Related
child: Text(
title,
style: TextStyle(color : Colors.grey[700], decoration: _enabledMap[title] == true ? TextDecoration.lineThrough)
),
Hi there, I am new to coding and I need help, please can you show me where I'm going wrong
You are providing data only for _enabledMap[title] == true. But you also need to provide when data is false for ternary case. You can provide null if you don't like to pass any TextDecoration
decoration: _enabledMap[title] == true
? TextDecoration.lineThrough
: null
For more info, see the Dart docs on conditional expressions
This is my code:
CheckboxListTile (
value: agree,
title: Text (AppLocalizations.of(context).translate('Do you accept the terms and conditions?'),
style: TextStyle(fontSize: 18)),
onChanged: (bool value) {
setState(() {
this.agree = value;
});
},
),
The error is:
a non null string must be provided to a text widget
Try adding a fallback text in the Text widget
Text(AppLocalizations.of(context).translate('Do you accept the terms and conditions?' ?? 'Do you accept the terms and conditions?');
If you want it to be translated, you have to check your folder: assets > lang
You will see all the translations that are in your project, I have put the following in the English one:
"DoYouAcceptTheTermsAndConditions?": "Do you accept the terms and conditions?",
And in Spanish:
"DoYouAcceptTheTermsAndConditions?": "¿Aceptas los términos y condiciones?",
If you have any questions, you can ask me questions: D
EDIT: My code now it looks like this:
CheckboxListTile (
title: Text(AppLocalizations.of(context).translate('DoYouAcceptTheTermsAndConditions?'),
style: TextStyle(fontSize: 18)),
value: agree,
onChanged: (bool value) {
setState(() {
this.agree = value;
});
},
),
Might be a very obvious question, but I need to add a TextStyle in a conditional statement, but it seems to not be applying it. This is what I tried:
Text((() {
if (contract.active == true) {
TextStyle(fontSize: 18);
return 'active';
}
return 'inactive';
}())),
It is not applying the textStyle. thank you.
You can use ternary operator:
Text(
'dummy text',
style: contract.active == true ? TextStyle(color: Colors.red): TextStyle(color:Colors.blue),
)
I added this for only TextStyle, you can do it for Text if you want.
And one thing, contract.active is already bool you don't need to check its equality, you can use contract.active instead of contract.active == true.
You can change text like this:
Text(
contract.active ? 'active' :'inactive'
style: contract.active ? TextStyle(fontSize: 18) : TextStyle()
)
You are using an immediate anonymous function there. The first parameter of Text widget expects a string and your anonymous function returns that. The TextStyle object that you've created inside the function body doesn't do anything.
You need to change your implementation to (using immediate anonymous function):
Text(
(() {
if (contract.active) {
return "Active";
}
return "Inactive";
}()),
style: (() {
if (contract.active) {
return TextStyle(fontSize: 18);
}
return null;
}())
),
But the above code looks very odd, you can simply achieve that using ternary operator:
Text(contract.active ? "Active" : "Inactive",
style: active ? TextStyle(fontSize: 18) : null),
Why can't I set the accents as a color?
This is my code :
Text('Hello',
style: TextStyle(color: Colors.accents),
)
In reality accents is not a color but rather a list(const List accents) that contains a lot of colors (accent colors).
Its implementation is :
static const List<MaterialAccentColor> accents = <MaterialAccentColor>[
redAccent,
pinkAccent,
purpleAccent,
deepPurpleAccent,
indigoAccent,
blueAccent,
lightBlueAccent,
cyanAccent,
tealAccent,
greenAccent,
lightGreenAccent,
limeAccent,
yellowAccent,
amberAccent,
orangeAccent,
deepOrangeAccent,
]
You can find it on the flutter website via this link
Now that we know it's a list, to retrieve one of its elements, just do List_name[element_index]. So in our case we'll do accents [color_index].
A small example of use :
Text('Hello',
style: TextStyle(color: Colors.accents[0]),
),
Colors.accents[0] for redAccent
Thanks.
Hi #martiX4 i found that you are passing invalid type to TextStyle widget now as answer given by #abhishek you can use index accents[index] based on your condition
for example if you use redAccent then you can use
List<MaterialAccentColor> accents = <MaterialAccentColor>[
redAccent,blueAccent];
// accents[0] => for redAccent
// accents[0] => for blueAccent
Text('Hello', style: TextStyle(color: Colors.accents[0]),)
accents is List so you can't be assigned to the color
/// The material design accent color swatches.
static const List<MaterialAccentColor> accents = <MaterialAccentColor>[
redAccent,
pinkAccent,
purpleAccent,
deepPurpleAccent,
indigoAccent,
blueAccent,
lightBlueAccent,
cyanAccent,
tealAccent,
greenAccent,
lightGreenAccent,
limeAccent,
yellowAccent,
amberAccent,
orangeAccent,
deepOrangeAccent,
];
you can use accents color using index value
Text('Hello',
style: TextStyle(color: Colors.accents[0]),
)
Is there a way to change the colour of the input text based on some condition ? The inputFormatters only seem to apply to structuring the input.
You can change the input text color using TextStyle
Here is a code sample:
TextField(
style: TextStyle(color: Colors.green),
...
)
You can introduce a condition using a boolean called 'myCondition' and a Ternary Operator in the following way:
TextField(
style: TextStyle(color: myCondition ? Colors.red : Colors.blue),
...
)
https://api.flutter.dev/flutter/painting/TextStyle-class.html