Related
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
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
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
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,
),
),
I want to set a black heart between "m" and "ms". and also I want to use "TextAlign.justify". if I use "TextAlign.center" then it's working fine. but if I use "TextAlign.justify" so the black heart is not set in the correct place. If I remove the "WidgetSpan" widget and add "TextSpan" for a black heart so also it's working. but I want to add bottom padding.
Please guys help me.
Thanks
Text.rich(
TextSpan(
children: <InlineSpan>[
TextSpan(
text: MyString.txtAboutUs ,
style: TextStyle(
fontSize: FontSize.size14,
fontFamily: MyFont.poppins_regular)),
TextSpan(
text: " m",
children: <InlineSpan>[
WidgetSpan(
baseline: TextBaseline.ideographic,
alignment: PlaceholderAlignment.top,
child: Container(
padding: EdgeInsets.only(bottom: 3),
child: Text("🖤", style: TextStyle(
fontSize: FontSize.size8,
fontFamily: MyFont.poppins_regular) ),
)
),
TextSpan(
text: "ms ",
style: TextStyle(
fontSize: FontSize.size14,
fontFamily: MyFont.poppins_medium,
color: Colors.black,
fontWeight: FontWeight.bold)),
],
style: TextStyle(
fontSize: FontSize.size14,
fontFamily: MyFont.poppins_medium,
color: Colors.black,
fontWeight: FontWeight.bold)),
TextSpan(
text: MyString.txtAboutImmediately,
style: TextStyle(
fontSize: FontSize.size14,
fontFamily: MyFont.poppins_regular)),
],
),
textAlign: TextAlign.justify,
),
Try to below simple Text Widget
Text('you add 🖤 here'),
// OR
Text('you add \u{2764} here} '),
Updated Answer using RichText
Center(
child: Text.rich(
TextSpan(
children: <TextSpan>[
TextSpan(
text: 'You add ',
style: TextStyle(
fontSize: 10.0,
color: Colors.black,
),
),
TextSpan(
text: '🖤',
),
TextSpan(
text: 'Here',
),
],
),
textAlign: TextAlign.center,
style: TextStyle(fontSize: 10.0,),
),
),
Screen like
replace padding with transform.
WidgetSpan(
baseline: TextBaseline.ideographic,
alignment: PlaceholderAlignment.top,
child: Container(
transform: Matrix4.translationValues(0, -3, 0),
margin: EdgeInsets.only(bottom: 8),
child: Text(
"🖤",
style: TextStyle(
// fontSize: FontSize.size8,
// fontFamily: MyFont.poppins_regular,
),
),
),
),