Google font color is ignored - flutter

I have an OutlineButton where I try to use a google font for the text using the google-fonts dependency. The code looks like that:
OutlinedButton(
child: const Text('Next'),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith((states) {
return colorPrimaryButton;
}),
textStyle: MaterialStateProperty.resolveWith((states) {
return GoogleFonts.inter(textStyle: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: Colors.white));
})
),
onPressed: () {
// do something
}),
Although I assigned white as the text color, the text appears blue-ish.
What am I missing here? Why wouldn't the "Next" text appear in white?

Don't need to change button textStyle just change child textSytle
OutlinedButton(
child: Text(
'Next',
style: GoogleFonts.inter(
textStyle: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white)),
),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith((states) {
return Colors.indigo;
}),
),
onPressed: () {
// do something
})
Preview

Related

Change color of text ( foreground ) in Elevated Button

Using Flutter, I set ElevatedButton theme in the style.dart with this.
ThemeData appTheme(BuildContext context) {
const fontFamilyFallBack = ['Lexend', 'NotoSans'];
return ThemeData(
primaryColor: Colors.white,
backgroundColor: Colors.white,
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
primary: Colors.green,
onPrimary: Colors.white,
),
),
textTheme: const TextTheme(
headline1: TextStyle(
fontFamilyFallback: fontFamilyFallBack,
),
subtitle1: TextStyle(
fontFamilyFallback: fontFamilyFallBack,
fontSize: 20,
),
bodyText1: TextStyle(
fontFamilyFallback: fontFamilyFallBack,
fontSize: 14,
),
),
);
}
But still the color of the foreground which is my text still does not change to White as the value of onPrimary whereas the color of the background is appliable.
ElevatedButton(
child: Text(
'Tap go to second page',
style: Theme.of(context).textTheme.bodyText1,
),
onPressed: () => context.router.pushNamed('/error'),
)),
I have also tried ButtonStyle one, but I still get the same result.
elevatedButtonTheme: ElevatedButtonThemeData(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(Colors.green),
foregroundColor: MaterialStateProperty.all<Color>(Colors.white),
),
),
You pass ButtonStyle to the style property like this:
ThemeData(
elevatedButtonTheme: ElevatedButtonThemeData(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all<Color>(Colors.green),
),
),
),
try this
ElevatedButton(
child: Text(
'Tap go to second page',
style:
Theme.of(context).textTheme.bodyText1.copyWith(color:Colors.green),
),
onPressed: () => context.router.pushNamed('/error'),
)),

Flutter - textColor from TextButtonTheme not applied in application

I am using ThemeData to style text inside my buttons. Setting font family and even text size works perfectly, but the text colour is not applied for some reason. It works if I manually apply the given TextStyle in my code, but that's not how it should work, no?
Of course, I am setting my ThemeData in my Application class. I emphasize that some data from TextButtonThemeData and ElevatedButtonThemeData work, but the text colour not...
Like that it applies textSize and other's but NOT textColour:
TextButton(
onPressed: () {},
child: const Text('TAP HERE'),
)
And only like that the colour is correctly applied:
TextButton(
onPressed: () {},
child: const Text('TAP HERE', style: TextStyles.textButton),
)
My ThemeData:
final applicationTheme = ThemeData(
fontFamily: 'Raleway',
primarySwatch: MyColors.primary,
appBarTheme: AppBarTheme(
backgroundColor: MyColors.primary.shade900,
toolbarTextStyle: TextStyles.appBar,
),
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
primary: MyColors.primary.shade900,
textStyle: TextStyles.elevatedButton,
),
),
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
textStyle: TextStyles.textButton,
shadowColor: MyColors.primary.shade500,
),
),
);
My TextStyles:
static final textButton = TextStyle(
color: MyColors.primary.shade500,
fontSize: 14,
fontWeight: FontWeight.w500,
letterSpacing: 1.25,
);
static const elevatedButton = TextStyle(
color: Colors.white,
fontSize: 14,
fontWeight: FontWeight.w500,
letterSpacing: 0.25,
);

Flutter define buttonstyle globally

so I would like to define my buttonstyles globally so that I can access them all at once.
I have enabled this for TextStyle like this:
abstract class ThemeText {
static const TextStyle exampleStyle= TextStyle(
fontFamily: 'Montserrat',
color: Colors.black,
fontSize: 40,
height: 0.5,
fontWeight: FontWeight.w600);
}
and when I access it in my text like this:
child: TextButton(
onPressed: (){},
child: Text("Test",
style: ThemeText.progressHeader,),
)
It works like a charm.
Now I want to do the same with my ButtonStyle:
TextButton(
style: TextButton.styleFrom(
primary: Colors.white,
backgroundColor: Colors.green[900],
textStyle: TextStyle(fontSize: 18, fontStyle: FontStyle.italic)),
child: Text("Test!"),
)
However if I try the same, it doesn't matter how I write it, I always get the
message "const variables must be initialized with a constant value". How do I format this for buttons so that it works just like the TextStyle?
Thank you very much in advance!

Flutter dynamic creation RichText object from List<String>

There is a random list whose length is not constant. For example
List<String> exampleList = List<String>();
exampleList.add("one");
exampleList.add("second random line");
exampleList.add("third random line");
Depending on the content of the exampleList[index], which is generated randomly, it is necessary to change the appearance, design, styles etc. If you use RichText TextSpan, then already hardcoded data is transferred there, and I need to generate this RichText dynamically. Any ideas on this?
approximate result of formating
child: RichText(
text: TextSpan(
text: 'GitHub is a development platform inspired by the way you work. From ',
style: TextStyle(
color: Colors.grey,
fontSize: 20,
),
children: <TextSpan>[
TextSpan(text: 'op_n',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
recognizer: TapGestureRecognizer()
..onTap = () {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text("Sending Message"),
));
}
),
TextSpan(
text: ' to ',
style: TextStyle(color: Colors.grey,
fontSize: 20,
)
),
TextSpan(
text: 'busin_ss,',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
recognizer: TapGestureRecognizer()
..onTap = () {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text("Sending Message"),
));
}
),
TextSpan(
text: ' you can host and review code, manage projects, and build software alongside 36 million developers.',
style: TextStyle(color: Colors.grey,
fontSize: 20,
)
)
]
),
)
Based on your sample, I see that you define each part of your RichText with a text, a style, and optionally a callback.
You could use a List<MyText> instead of a List<String>:
class MyText {
final String text;
final TextStyle style;
final void Function(BuildContext) callback;
MyText({
this.text,
this.style,
this.callback,
});
}
List<MyText> textSpanList = [
MyText(
text:
'GitHub is a development platform inspired by the way you work. From ',
style: TextStyle(
color: Colors.grey,
fontSize: 20,
),
),
MyText(
text: 'op_n',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
callback: (context) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Sending Message"),
),
);
},
),
MyText(
text: ' to ',
style: TextStyle(
color: Colors.grey,
fontSize: 20,
),
),
MyText(
text: 'busin_ss,',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
callback: (context) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Sending Message"),
),
);
},
),
MyText(
text:
' you can host and review code, manage projects, and build software alongside 36 million developers.',
style: TextStyle(
color: Colors.grey,
fontSize: 20,
),
),
];
And then use the List<MyText> as follows:
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
title: 'Flutter Demo',
home: Scaffold(body: MyWidget()),
),
);
}
class MyWidget extends StatelessWidget {
const MyWidget({
Key key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
color: Colors.black,
padding: const EdgeInsets.all(8.0),
child: RichText(
text: TextSpan(
children: textSpanList
.map(
(data) => data.callback == null
? TextSpan(
text: data.text,
style: data.style,
)
: TextSpan(
text: data.text,
style: data.style,
recognizer: TapGestureRecognizer()
..onTap = () => data.callback(context),
),
)
.toList(),
),
),
);
}
}

OnPressed effect on font color

I'm making simple custom Text button
SizedBox(
height: 40,
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: (){},
splashColor: Colors.black12,
child: Center(
child: Text(
'Done',
style: TextStyle(
fontSize: 18, color: Colors.white, fontWeight: FontWeight.w500),
),
),
),
),
)
Now this works great however the effect on onTap event is around the text
How to make that effect to be on the text font only or if that is not possible change color of the text on tapped down and change it back on release
For changing the color of the text when pressed down, you can use the onHighlightChanged event handler of InkWell.
In your class declare a color property:
Color textColor = Colors.white;
And change your InkWell implementation:
InkWell(
onTap: () {},
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
onHighlightChanged: (value) {
setState(() {
textColor = value ? Colors.white70 : Colors.white;
});
},
child: Center(
child: Text(
Done',
style: TextStyle(
fontSize: 18,
color: textColor,
fontWeight: FontWeight.w500
),
),
),
)
Note:
You have to set the splashColor as transparent
You have to set the highlightColor as transparent