GoogleFont-Weight issue on copyWith - flutter

I'm creating a text-style model and using getter to have the text-Style that use google_fonts. The issue occurs when I provide fontWeight: property. Also, the fontWeight is not providing similar look as GoogleFont.
I've tested on another project, rebuilding the project, using html renderer. I've checked this question but it is not working.
Comparison between styles
But Looks from GoogleFont
flutter doctor -v no issue
Flutter (Channel stable, 2.5.2, on Microsoft Windows [Version
10.0.19043.1288], locale en-US)
• Flutter version 2.5.2 at C:\Tools\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 3595343e20 (3 weeks ago), 2021-09-30 12:58:18
-0700
• Engine revision 6ac856380f
• Dart version 2.14.3
Model class
class AppTextStyles {
static TextStyle get normalMidBlod => const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 33,
);
static TextStyle get latoMidBlod => GoogleFonts.lato(
fontWeight: FontWeight.bold, //this one
color: Colors.black,
fontSize: 33,
);
static TextStyle get lato => GoogleFonts.lato(
color: Colors.black,
fontSize: 33,
);
}
Test Widget
class Test extends StatelessWidget {
const Test({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Column(
children: const [
Text("style: TextStyle.."),
Text(
"w100",
style: TextStyle(fontWeight: FontWeight.w100, fontSize: 33),
),
Text(
"w200",
style: TextStyle(fontWeight: FontWeight.w200, fontSize: 33),
),
Text(
"w300",
style: TextStyle(fontWeight: FontWeight.w300, fontSize: 33),
),
Text(
"w400",
style: TextStyle(fontWeight: FontWeight.w400, fontSize: 33),
),
Text(
" w500",
style: TextStyle(fontWeight: FontWeight.w500, fontSize: 33),
),
Text(
" w600",
style: TextStyle(fontWeight: FontWeight.w600, fontSize: 33),
),
Text(
" w700",
style: TextStyle(fontWeight: FontWeight.w700, fontSize: 33),
),
Text(
" w800",
style: TextStyle(fontWeight: FontWeight.w800, fontSize: 33),
),
Text(
" w900",
style: TextStyle(fontWeight: FontWeight.w900, fontSize: 33),
),
],
),
const SizedBox(
width: 30,
),
Column(
children: [
Text("normalMidBlod.copyWith"),
Text(
"w100",
style: AppTextStyles.normalMidBlod
.copyWith(fontWeight: FontWeight.w100),
),
Text(
"w200",
style: AppTextStyles.normalMidBlod
.copyWith(fontWeight: FontWeight.w200),
),
Text(
"w300",
style: AppTextStyles.normalMidBlod
.copyWith(fontWeight: FontWeight.w300),
),
Text(
"w400",
style: AppTextStyles.normalMidBlod
.copyWith(fontWeight: FontWeight.w400),
),
Text(
" w500",
style: AppTextStyles.normalMidBlod
.copyWith(fontWeight: FontWeight.w500),
),
Text(
" w600",
style: AppTextStyles.normalMidBlod
.copyWith(fontWeight: FontWeight.w600),
),
Text(
" w700",
style: AppTextStyles.normalMidBlod
.copyWith(fontWeight: FontWeight.w700),
),
Text(
" w800",
style: AppTextStyles.normalMidBlod
.copyWith(fontWeight: FontWeight.w800),
),
Text(
" w900",
style: AppTextStyles.normalMidBlod
.copyWith(fontWeight: FontWeight.w900),
),
],
),
const SizedBox(
width: 30,
),
RichText(
text: TextSpan(
children: [
TextSpan(text: "latoBold.copyWith \n"),
TextSpan(
text: "w100 \n",
style: AppTextStyles.latoMidBlod
.copyWith(fontWeight: FontWeight.w100),
),
TextSpan(
text: "w200 \n",
style: AppTextStyles.latoMidBlod
.copyWith(fontWeight: FontWeight.w200),
),
TextSpan(
text: "w300 \n",
style: AppTextStyles.latoMidBlod
.copyWith(fontWeight: FontWeight.w300),
),
TextSpan(
text: "w400 \n",
style: AppTextStyles.latoMidBlod
.copyWith(fontWeight: FontWeight.w400),
),
TextSpan(
text: " w500 \n",
style: AppTextStyles.latoMidBlod
.copyWith(fontWeight: FontWeight.w500),
),
TextSpan(
text: " w600 \n",
style: AppTextStyles.latoMidBlod
.copyWith(fontWeight: FontWeight.w600),
),
TextSpan(
text: " w700 \n",
style: AppTextStyles.latoMidBlod
.copyWith(fontWeight: FontWeight.w700),
),
TextSpan(
text: " w800 \n",
style: AppTextStyles.latoMidBlod
.copyWith(fontWeight: FontWeight.w800),
),
TextSpan(
text: " w900 \n",
style: AppTextStyles.latoMidBlod
.copyWith(fontWeight: FontWeight.w900),
),
],
),
),
const SizedBox(
width: 30,
),
RichText(
text: TextSpan(
children: [
TextSpan(text: "lato.copyWith \n"),
TextSpan(
text: "w100 \n",
style:
AppTextStyles.lato.copyWith(fontWeight: FontWeight.w100),
),
TextSpan(
text: "w200 \n",
style:
AppTextStyles.lato.copyWith(fontWeight: FontWeight.w200),
),
TextSpan(
text: "w300 \n",
style:
AppTextStyles.lato.copyWith(fontWeight: FontWeight.w300),
),
TextSpan(
text: "w400 \n",
style:
AppTextStyles.lato.copyWith(fontWeight: FontWeight.w400),
),
TextSpan(
text: " w500 \n",
style:
AppTextStyles.lato.copyWith(fontWeight: FontWeight.w500),
),
TextSpan(
text: " w600 \n",
style:
AppTextStyles.lato.copyWith(fontWeight: FontWeight.w600),
),
TextSpan(
text: " w700 \n",
style:
AppTextStyles.lato.copyWith(fontWeight: FontWeight.w700),
),
TextSpan(
text: " w800 \n",
style:
AppTextStyles.lato.copyWith(fontWeight: FontWeight.w800),
),
TextSpan(
text: " w900 \n",
style:
AppTextStyles.lato.copyWith(fontWeight: FontWeight.w900),
),
],
),
)
],
),
);
}
}

Only certain fonts support one ttf file with different weights. If you notice the alphabet g it looks different in each weight. You may have to download the full font family from Google fonts and include that in your pubspec.yaml under assets. It should work.
Check https://pub.dev/packages/google_fonts bundling font in application assets section.

There is a bug on the google_fonts_flutter package repo that talks about this: https://github.com/material-foundation/google-fonts-flutter/issues/141
To summarize, when you create a Google Font TextStyle, the fontFamily is set to the original weight you specified (ex. 'poppins-w400') instead of the general fontFamily name ('poppins').
You can fix this by specifying the fontFamily in your copyWith method.
TextSpan(
text: " w900 \n",
style: AppTextStyles.latoMidBlod
.copyWith(
fontWeight: FontWeight.w900,
fontFamily: GoogleFonts.poppins().fontFamily,
),
),

Related

I was trying to add TextSpan as the children of TextSpan, but I'm getting error

The error which I'm getting is :
" The argument type 'TextSpan' can't be assigned to the parameter type 'List? "
Here is the code:
Widget buildSignUpBtn() {
return GestureDetector(
onTap: () => print('SignUp Pressed'),
child: RichText(
text: TextSpan(
children: TextSpan(
text: 'Don\'t have an Account',
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
fontWeight: FontWeight.w500,
),
),
TextSpan(
text: 'Don\'t have an Account',
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
fontWeight: FontWeight.w500,
),
),
),
),
);
}
Please help me to fix this!
it is taking list of TextSpan in the children property just like this:
RichText(
text: TextSpan(
text: 'Hello ',
style: DefaultTextStyle.of(context).style,
children: const <TextSpan>[
TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' world!'),
],
),
)
you can read more about RichText in the official documentation here

How can i achieve this half bold text in flutter

Does anyone know how I can get this half-bolded effect using Flutter?
You can use RichText widget like below.
RichText(
textAlign: TextAlign.center,
text: TextSpan(children: [
TextSpan(text: "Are you sure you want to delete ", style: black16w700),
TextSpan(text: "Broker Account?", style: black16w500)
]))
var text = RichText(
text: TextSpan(
style: const TextStyle(
fontSize: 14.0,
color: Colors.black,
),
children: <TextSpan>[
TextSpan(text: 'NonBOld'),
TextSpan(text: 'Bold', style: TextStyle(fontWeight: FontWeight.bold)),
],
),
);
Try TextSpan widget having children.
use the richtext widget property as follows
Text.rich(
TextSpan(
style: TextStyle(
fontFamily: 'Roboto',
fontSize: 52,
color: const Color(0xff7a7a7a),
),
children: [
TextSpan(
text: 'arch',
style: TextStyle(
fontWeight: FontWeight.w700,
),
),
TextSpan(
text: 'Incubator',
),
],
),
)
You should use RichText
RichText(
text: TextSpan(
text: 'Hello ',
style: // style text,
children: const <TextSpan>[
TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' world!'),
],
),
)
Read this doc and check this answer

how to make a row responsive in flutter?

Hello i'm try to write this text " choose a one to modify the number " and the word "to modify " is with a different style text . my problem here is that the text is not responsive that mean there is not a return to next line if the phone has a small screen
how can i fix it and make this text in the center ?
Row(
children: const [
Text(
"Choose a one",
style: TextStyle(
fontSize: 17,
fontFamily: 'SFProRegular',
color: Color(0xFF000000),
),
),
Text(
"to modify",
style: TextStyle(
fontSize: 17,
fontFamily: 'SFProRegular',
color: Color(0xFFFF9E4F),
),
),
Text(
"the number",
style: TextStyle(
fontSize: 17,
fontFamily: 'SFProRegular',
color: Color(0xFF000000),
),
),
],
)
You can try using rich text, it will be like the following
RichText(
text: TextSpan(
text: "Choose a one",
style: TextStyle(
fontSize: 17,
fontFamily: 'SFProRegular',
color: Color(0xFF000000),
),
children: const <TextSpan>[
TextSpan(text: 'to modify',
style: TextStyle(
fontSize: 17,
fontFamily: 'SFProRegular',
color: Color(0xFFFF9E4F),
),
TextSpan(text: ' the number',
style: TextStyle(
fontSize: 17,
fontFamily: 'SFProRegular',
color: Color(0xFF000000),
),
),
],
),
)

make space between TextSpan

I am trying to make space between TextSpan
What is the best way to add space between TextSpan?
child: RichText(
text: TextSpan(
children: [
TextSpan(
text: 'Don\'t have an Account?',
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
fontWeight: FontWeight.w400,
),
),
TextSpan(
text: 'Sign Up',
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
],
),
),
SizedBox widget can be used in between two widgets to add space between two widgets.
use the SizedBox by wrapping it with a WidgetSpan widgit
child: RichText(
text: TextSpan(
children: [
TextSpan(
text: 'Don\'t have an Account?',
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
fontWeight: FontWeight.w400,
),
),
WidgetSpan(
child: SizedBox(width: 10),
),
TextSpan(
text: 'Sign Up',
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
],
),
),
If you like to get maximum space between text, you might go for Row widget. For x amount of space inside RichText you can use
TextSpan(...),
WidgetSpan(child: SizedBox(width: x)),
TextSpan(...),
RichText(
text: const TextSpan(
children: [
TextSpan(
text: 'Don\'t have an Account?',
style: TextStyle(
color: Color.fromARGB(255, 0, 0, 0),
fontSize: 15.0,
fontWeight: FontWeight.w400,
),
),
WidgetSpan(child: SizedBox(width: 10)), ///this
TextSpan(
text: 'Sign Up',
style: TextStyle(
color: Color.fromARGB(255, 0, 0, 0),
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
],
),
),
You can use the height property on the TextStyle to create some spacing:
RichText(
text: TextSpan(
children: [
TextSpan(
text: 'Don\'t have an Account?',
style: TextStyle(
height: 1.5, //USE THIS PROPERTY
color: Colors.white,
fontSize: 15.0,
fontWeight: FontWeight.w400,
),
),
SizedBox(height: 10),
TextSpan(
text: 'Sign Up',
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
],
),
),
More info in the docs: https://api.flutter.dev/flutter/painting/TextStyle/height.html

How to get some space between in Richtext?

Im trying getting some space in Richttext. First heres my code
title: RichText(
text: TextSpan(
text: _snapshot.data['username'], // _snapshot.data['username']
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.black),
children: <TextSpan>[
TextSpan(
//"${comment.data()['comment']}"
text: "${comment.data()['comment']}",
style: TextStyle(
fontWeight: FontWeight.normal,
),
)
],
),
)
And what I want is some space between the this
text: TextSpan(
text: _snapshot.data['username'], // _snapshot.data['username']
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
)
And this
children: <TextSpan>[
TextSpan(
//"${comment.data()['comment']}"
text: "${comment.data()['comment']}",
style: TextStyle(
fontWeight: FontWeight.normal,
),
],
),
Hope anyone can help. If you need a picture how it looks at the moment please leave a comment
You can use a SizedBox like below :
RichText(
text: TextSpan(
text: "Test:",
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.black),
children: <InlineSpan>[
WidgetSpan(
alignment: PlaceholderAlignment.baseline,
baseline: TextBaseline.alphabetic,
child: SizedBox(width: 10)),
TextSpan(
text: "Data",
style: TextStyle(
fontWeight: FontWeight.normal,
),
)
],
),
)