flutter apply or copyWith method - flutter

I'm trying to apply blue color on Theme.of(context).textTheme.headline6, it can be done in two ways
Text(
"Hello",
style: Theme.of(context)
.textTheme
.headline6!
.copyWith(color: Colors.blue),
),
or
Text(
"Hello",
style: Theme.of(context)
.textTheme
.headline6!
.apply(color: Colors.blue),
),
Which one you prefer cpyWith or apply method?
Preview on image

The main difference is that apply() offers several parameters to scale parameters of the source TextStyle.
For example, copyWith(fontSize: 14) lets you explicitly set the font size, while apply(fontSizeFactor: 1.6) lets you scale the source's font size by a custom factor (note that an explicit fontSize parameter does not exist in apply()).

apply() creates a copy of text style replacing or altering the specified properties in it.
copyWith() creates a copy of text style but with the given fields replaced with the new values.
source Flutter: Apply style as a Theme in a Text widget

Related

Is it okay to extend the Flutter built-in TextStyle class with my own custom methods?

Is there a problem if I extend some of the framework's built-in classes, specifically TextStyle, with my own extension methods? docs here
I'll layout an example of what I mean so that it resembles the actual implementation I'm using in my project - but not fully, it's just to be as clear and clean as possible:
In this example I'm declaring some common-use text styles (for context, in my project I have many many more), and later I'm extending the built-in flutter class TextStyle to have a faster and smoother implementation of further customization:
// some text styles I'll use across the whole app
const TextStyle titleLarge600 = TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.w600,
);
const TextStyle titleSmall500 = TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.w500,
);
const TextStyle bodyRegular400 = TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.w400,
);
// ...
//* Here it comes
extension CopyWithStyles on TextStyle {
/// Copy with [MyColors.primaryFontColor] color.
TextStyle get primaryColor => copyWith(color: MyColors.primaryFontColor);
/// Copy with [MyColors.accentColor] color.
TextStyle get accentColor => copyWith(color: MyColors.accentColor);
// ... several colors later
/// Copy with [TextDecoration.underline] decoration.
TextStyle get underline => copyWith(decoration: TextDecoration.underline);
/// Copy with [TextStyle.letterSpacing] of 0.4.
TextStyle get withLetterSpacing => copyWith(letterSpacing: 0.4);
// etc ...
}
This allows me to later just import these styles and methods and implement them in my Text widgets:
// ...
Text('Some title', style: titleSmall500.primaryColor),
... and even concatenate them:
// ...
Text(
'Some action here',
style: bodyRegular400.accentColor.underline.withLetterSpacing, // ← THIS WORKS! it has no limits really
),
I'm amazed as to how flexible extension methods are, but in this particular case (extending TextStyle) I'm wondering about performance issues, and compatibility with the framework, or if maybe I'm missing something and actually should not do this.
For the moment I'm not experiencing any issues. Still, I'm wondering if there could be any potential problems. Is there a problem if I implement something like this in my projects? Is there something I'm not considering and should take into account in the future?
TY

'TextStyle?' can't be assigned to the parameter type 'TextStyle'

I am Getting this null safety error while working on my flutter app.
The argument type 'TextStyle?' can't be assigned to the parameter type 'TextStyle'.
Here is the code snippet that is throwing this error:
ButtonStyle(textStyle: MaterialStateProperty.all<TextStyle>(Theme.of(context).textTheme.button))
VS Code suggest this solution to put not operator at the end of the argument.
ButtonStyle(textStyle: MaterialStateProperty.all<TextStyle>(Theme.of(context).textTheme.button!))
Question: Is this a good practice to use ! in my code and what other solutions are possible in this case?
You can also get this error if you imported wrong library for the material TextStyle.
This may happen when you used:
import 'dart:ui';
Instead of:
import 'package:flutter/material.dart';
Material state properties represent values that depend on a widget's material "state". The state is encoded as a set of MaterialState values, like MaterialState.focused, MaterialState.hovered, MaterialState.pressed. For example the InkWell.overlayColor defines the color that fills the ink well when it's pressed (the "splash color"), focused, or hovered. The InkWell uses the overlay color's resolve method to compute the color for the ink well's current state.
You could use only Theme of context to use theme textStyle like this:
Text("copied text theme",
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.button)
Initialize Your Style in the Buildcontext, Example :
var _styleType = Theme.of(context)
.textTheme
.body1
.copyWith(color: Colors.white);
Then Apply It to the Widget :
Container(
margin: EdgeInsets.symmetric(vertical: 10),
child: new Text("\u00a9 TasnuvaOshin",
overflow: TextOverflow.ellipsis,
style: _styleType),
),
MaterialStateTextStyle.resolveWith((states) => TextStyle(fontSize: 12),
You can use .resolveWith Method to create a MaterialStateTextStyle from a MaterialPropertyResolver callback function.

How to use PingFangSC? Is PingFangSC included by default in Flutter?

Now I have a design drawing, the title of which is PingFangSC-Semibold, and the content is PingFangSC-Regular.
How to use PingFangSC-Regular and PingFangSC-Semibold in Flutter?
The default font in Flutter seems to be SF.
Do I need to introduce these two fonts, or how?
enter image description here
For us custom font you can use this pages:
1.flutter
2.StackOverflow
But for this font no need to added the TTF file.
just call it like this :
Text(
'取消',
style: TextStyle(
fontSize: 28,
fontWeight: FontWeight.w400,
fontFamily: 'PingFangSC-Regular,PingFang SC'),
),

Does Flutter support cascading styles?

I want to be able to set a default text style which I can use across my app and then overwrite part of that style in specific circumstances. React Native supports this, does Flutter support it too please? Or if not, how can I achieve something like this?
Example of how I'd hope this would work:
style.dart:
ThemeData appTheme() {
return ThemeData(
...
textTheme: TextTheme(
headline1: TextStyle(fontSize: 26.0, fontWeight: FontWeight.bold),
...
)
)
}
body.dart:
Text('My text here', style: [TextStyle(color: myColorVariable), themeData.textTheme.headline1])
Many thanks!
You can use
Theme.of(context).textTheme.copyWith()
or even
Theme.of(context).textTheme.subtitle1.copyWith()
to change the default for a specific text widget.

Is there any way of using Text with spritewidget in Flutter?

I'm developing a game in Flutter with spritewidget library. I'd like to know if is it possible to use text inside a SpriteWidget.
I know i can use regular Flutter widgets but i need that the text size is relative to SpriteWidget so it can be consistently shown in different screen sizes.
I have searched library documentation but i haven't found anything related to text render.
Any suggestion would be appreciated!!
You can absolutely use text inside a SpriteWidget. There is the Label node for that particular purpose. If you need more advanced text rendering, you can use the code from the Label as your starting point.
Example:
Label label = Label(
'My text label',
textAlign: TextAlign.center,
textStyle: new TextStyle(
fontFamily: 'Orbitron',
letterSpacing: 10.0,
color: Colors.white,
fontSize: 24.0,
fontWeight: FontWeight.w600
)
);
addChild(label);