I want to update the displaying font in flutter using a function.I've tried with following method but it won't update.I can't find the problem.
Function
_fontselect(String ){
if (_character==1) {
return "Font";
} else{
return "Font2";
}
}
context
Center(child: Text(text,
textAlign: TextAlign.center,
style: TextStyle(
fontFamily:_fontselect(String),
fontWeight: FontWeight.bold,
fontSize: 28.0,
color: Colors.red)
),
),
Use Ternary Operator within the fontFamily itself.
Center(child: Text(text,
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: _character==1 ? "Font" : "Font2",
fontWeight: FontWeight.bold,
fontSize: 28.0,
color: Colors.red)
),
),
Your function needs a String variable to use inside of the function.
_fontselect(String character){
if (character==1) {
return "Font";
} else{
return "Font2";
}
}
Also it looks like character is an int but you are sending a String over to the function you should call the function with an int if this makes more sense.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
class CATextStyle extends TextStyle {
static const style = TextStyle();
//const CATextStyle._(TextStyle style) : super(style);
CATextStyle._style(TextStyle style) : super(style);
}
abstract class CATextStyles {
static const _parent = TextStyle();
static final headLine1 =
CATextStyle._style(_parent.copyWith(color: Colors.amber));
}
I want to created class like this but is showing error
i want to know how to use only one textstyle class and reuse that using copywith method
Here is how I do this, Create a separate Directory and name it Constants and inside this create a dart file with the name appstyles.dart. In this class declare all your styles just like the below code.
class AppStyles{
static TextStyle headline1 = TextStyle(
fontFamily: 'Roboto',
fontWeight: FontWeight.w300,
fontSize: 40,
);
static const TextStyle headline2 = TextStyle(
fontWeight: FontWeight.w400,
fontSize: 30,
);
static const TextStyle bodyText1 = TextStyle(
fontWeight: FontWeight.w300,
color: Colors.black,
fontSize: 16,
);
static const TextStyle bodyText2 = TextStyle(
color: Colors.black,
fontWeight: FontWeight.w300,
fontSize: 14,
);
}
The same is you use for Container decoration etc e.g
static final kRoundedTContainer = BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.all(
Radius.circular(10),
));
And here is how to use these styles
Text(
'Hello World',
style: AppStyles.headline1,
)
Container(
decoration: AppStyles.kRoundedTContainer,
height: 200,
width: 200,
)
You can create TextStyle global class like this.
class CommonFontStyle {
static font12WithRegular({color}) {
return TextStyle(
fontSize: Dimensions.fontSize12,
fontFamily: 'Montserrat Regular',
fontWeight: FontWeight.w500,
color: color);
}
static font12WithMedium({weight}) {
return TextStyle(
fontSize: Dimensions.fontSize12,
fontFamily: 'Montserrat Medium',
fontWeight: weight,
);
}
}
and for use of this you have to add simply this font12WithRegular() to your textStyles.
child: Text(
"Okay",
style: CommonFontStyle.font12WithRegular(),
textAlign: TextAlign.center,
),
I need to highlight some words in a text. I used highlight_text 1.6.0.
Map<String, HighlightedWord> words = {
"Flutter": HighlightedWord(
textStyle: textStyle,
),
"words": HighlightedWord(
textStyle: textStyle,
),
};
TextHighlight(
text: text,
words: words,
matchCase: false,
textStyle: TextStyle(
fontSize: 14.0,
color: Colors.black,
),
textAlign: TextAlign.justify,
)
It is working fine when given static values.
Is it possible to load the highlighted words from API like a list instead of the map?
List=["Flutter", "Words"]
Lets assume you get this sample list from api:
List sample = ["Flutter", "Words"];
then you can use fromIterable:
var result = Map<String, HighlightedWord>.fromIterable(sample,
key: (v) => v,
value: (v) => HighlightedWord(
textStyle: textStyle,
),
);
then use result in TextHighlight:
TextHighlight(
text: text,
words: result,
matchCase: false,
textStyle: TextStyle(
fontSize: 14.0,
color: Colors.black,
),
textAlign: TextAlign.justify,
)
How can i change my Texts color immedietly
TextStyle get headingStyle {
return GoogleFonts.lato(
textStyle: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
color:Get.isDarkMode?Colors.white:Colors.black);
}
color changes when i hot reloud but doesn't change when i clicked the button(button changes isDarkMode )
Try put the Obx.
It's create an observable in your code
TextStyle get headingStyle {
return Obx(() => GoogleFonts.lato(
textStyle: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
color:Get.isDarkMode?Colors.white:Colors.black));
}
I am styling text but am not able to get what I want like -:
I want this text below
look like this -:
how do I do it can anyone help?
If you want each word on a new line
String text = "Lorem ipsum dolor sit amet";
Text(
text.replaceAll(" ", "\n"),
),
text.replaceAll(" ", "\n") will replace all space with \n(new line)
You can use \n (optn + shift + 7) to make a new line inside you're String.
Container(
child: Center(
child: Text('Hello \nWorld',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 22.0,
fontWeight: FontWeight.bold,
letterSpacing: 0.7
)
),
),
),
Use textAlign to make your text align in the center (default is align left).
Its seem like by your comment you have a dynamic data. Since your question is not very clear, I will give you sample example to handle dynamic Data.
List a = ['Hello World', 'Hello', 'World'];
ListView(
children: a.map((e) {
if (e == "Hello") {
return Text(
e,
textAlign: TextAlign.end,
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
);
} else if (e == "World") {
return Text(
e,
textAlign: TextAlign.end,
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
);
} else {
return Text(
e,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
);
}
}).toList()),
Code Explanation
1). I have declare a Random List of String.
2). Then I used List View to handle dynamic list.
3). I put iteration and use a Text widget and put some style according to my Requirment.
you can put condition by yourself what you needed.
You can use \n to make a new line
Code:
Center(
child: Text('Hello Guys, Thanks \nfor visiting our website \nFlutter Examples.com',
style: TextStyle(fontSize: 22),
textAlign: TextAlign.center)
)
I tried to find how capitalize text in Flutter, but I couldn't find it.
My code:
Center(
heightFactor: 2,
child: Text(
'Strengthening the bond of owners and pets, more than ever...',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0,
fontStyle: FontStyle.italic,
fontWeight: FontWeight.bold,
color: Colors.cyanAccent[700],
wordSpacing: 8,
),
)),
I dont know if there is a way to do it through the Text widget, but you can use string.toUppercase() to capitalize the word:
Center(
heightFactor: 2,
child: Text(
'Strengthening the bond of owners and pets, more than ever...'.toUpperCase(),
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0,
fontStyle: FontStyle.italic,
fontWeight: FontWeight.bold,
color: Colors.cyanAccent[700],
wordSpacing: 8,
),
)),
To capitalize and to uppercase are different:
Capitalize: "Strengthing..."
Uppercase: "STRENGTHING..."
Capitalize
To capitalize a string in all locales:
import 'package:intl/intl.dart';
toBeginningOfSentenceCase('strengthening...');
To capitalize a string for en-US like locales:
String text = 'strengthening...';
text = text[0].toUpperCase() + text.substring(1).toLowerCase();
You can also have the virtual keyboard automatically switch to uppercase on the start of a sentence:
TextField(
textCapitalization: TextCapitalization.sentences
)
https://api.flutter.dev/flutter/services/TextCapitalization.html
Uppercase
To uppercase a string:
'strengthening...'.toUpperCase()
You can also have the virtual keyboard automatically switch to uppercase on each character
TextField(
textCapitalization: TextCapitalization.characters
)
https://api.flutter.dev/flutter/services/TextCapitalization.html
Better way to capitalize text
// copy this code to somewhere in your dart file
// also you can write/add methods to below code to support your strings to modify i.e. `toCapitalize()` or `yourStringModifyingMethod`.
extension StringCasingExtension on String {
String toCapitalize() => length > 0 ? ${this[0].toUpperCase()}${substring(1).toLowerCase()}' : '';
// String yourStringModifyingMethod() => write your logic here to modify the string as per your need;
}
Here is how you can use it
var word = 'this is capital'.toCapitalized(); // 'This is capital'
Well, you could use this package to capitalize the content in the Text widget:
https://pub.dev/packages/text_tools.
//This will put the letter in position 15 in UpperCase, will print 'Strengthening The bond of owners and pets, more than ever...'
Center(
heightFactor: 2,
child: Text(
TextTools.toUppercaseAnyLetter(text: 'Strengthening the bond of owners and pets, more than ever...', position: 15),
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0,
fontStyle: FontStyle.italic,
fontWeight: FontWeight.bold,
color: Colors.cyanAccent[700],
wordSpacing: 8,
),
)),