How can I show the right animated text in flutter? - flutter

I need to show an animated sentence in the correct translation for the user. On first start, however, flutter is as if it were loading the animation first and then looking for the right translation even though I used InitState and DidChangeDependencies. If I switch windows and return, the translation with the animation is shown correctly, only on the first start it causes problems. Here is the animation code:
AnimatedTextKit(
isRepeatingAnimation: false,
animatedTexts: [
TypewriterAnimatedText(_frase,
textAlign: TextAlign.center,
textStyle: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 21),
speed: Duration(milliseconds: 30))
])
This is the code of the variable "_frase":
#override
void didChangeDependencies() async {
super.didChangeDependencies();
_frase =
"${AppLocalizations.of(context)!.ext} ${DateFormat.MMMM("${AppLocalizations.of(context)!.codice}").format(DateTime.now()).capitalize()} $annoAttuale";
...
l10n.yaml:
arb-dir: lib/traduzioni
//in the animation, the first printout makes this translation, then the correct one for the user
template-arb-file: app_it.arb
output-localization-file: app_localizations.dart

Related

setState not updating part of page when called

When accessing a flutter page, I'm creating a graph (syncfusion) and 2 rows with values downloaded from some API.
I create an empty page, then, when the data is loaded, I set the variables with the respective data and call setState(() {walletRows=wallet.getWallet();}); to update the rows variable with the data for the rows I mentioned in the beginning.
Problem: the graph is actually generated but the rows are not created (if I change page or hot reload, the rows appears)
In initState i call loadPageData which loads the data in 2 variables.
#override
initState() {
loadPageData();
_pageController = PageController();
super.initState();
}
This is the loadPageData func
Future<void> loadPageData() async {
this._chartData = await getChartData(); // variable to feed sfcartesianchart
const storage = FlutterSecureStorage(); // getting token for api call
String token = await storage.read(key: 'token') as String;
var wallet = WalletRow();
wallet.addText(token);
setState(() {walletRows=wallet.getWallet();}); //setting state with updated walletrows
}
Within a Column i have another Column where the children are taken from walletRows.
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
textAlign: TextAlign.left,
"Rows",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color:
Color.fromARGB(255, 113, 113, 113)),
),
Column(children:walletRows)
]),
As requested, here is the page (just part of it, else it would be too long):
https://pastebin.com/cVC2pdhX
UPDATE
I realized that if I add a Button with setState on the onPressed event, the rows get correctly added; so it seems there is a problem with the first setState although I still can't find a solution to it.

Which one is performance wise better Text or extracted TextWidget function?

In my flutter code when I am creating an UI lot of places using Text widget. So I converted this Text widget into a function and calling everywhere? Text widget also including some styling. So calling the function or calling the Text widget is better (execution speed)?
Example code:
Text('Time left to Entrance exam',style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w400,
color: Colors.black
),);
or
Text buildText(String text,double fontSize, Color color) {
return Text(text,
style: TextStyle(
fontSize: fontSize,
color: color
),);
}
TextWidget function is more useful than multiple texts. It's absolutely a good practice and if any changes need you can able to change centrally and it's time-saving with clean code. You do not get the execution speed issue. And more important things, in both widget and function you just call a single Text widget. That's why there is no performance issue. You go for the second one for good practice.

Flutter - text and icons not displaying sometimes

Sometimes after turning on my app, user see not rendered screen.
Buttons are available, but no labels and icons are printed.
I didn't notice it with debug version, only release.
In some cases restarting app helps, and sometimes user has to install the app again.
What may be causing this issue?
Broken screen:
And this is how it should look like
Code seems to be rather standard, e.g. logo, 'Slift' string is created this way:
#override
Widget build(BuildContext context) {
return BorderedText(
strokeWidth: strokeWidth,
strokeColor: ThemeColors.getColor3(),
child: Text(
'SLift',
style: GoogleFonts.baloo(
textStyle:
TextStyle(decoration: TextDecoration.none, fontSize: fontSize),
),
));
}
are you using MediaQuery in your code? Because sometimes it works differently on release and debug mode.

Dropdown Menu based on List, A non-null String must be provided to a Text widget

I am trying to generate a list of dropdown options for my user. I have created a custom model to use and here it is:
class UserFontModel {
String fontFamily;
FontWeight fontWeight;
UserFontModel({this.fontFamily, this.fontWeight});
}
I then create an instance here:
List<UserFontModel> _fonts = [
UserFontModel(fontFamily: 'Bold', fontWeight: FontWeight.w700),
UserFontModel(fontFamily: 'Medium', fontWeight: FontWeight.w500),
UserFontModel(fontFamily: 'Regular', fontWeight: FontWeight.w400),
UserFontModel(fontFamily: 'Light', fontWeight: FontWeight.w300),
UserFontModel(fontFamily: 'Thin', fontWeight: FontWeight.w100),
];
I then create a dropdown menu with the items based on the above list:
new DropdownButton<String>(
hint: Text('Style'),
items: _fonts.map((fonts) => DropdownMenuItem<String> (
child: Container(
width: MediaQuery.of(context).size.width * 0.2,
child: Text(fonts.fontFamily, style: TextStyle(fontWeight: fonts.fontWeight),),
),
)).toList(),
onChanged: (String _) {
setState(() {
print(_);
});
},
),
But for some reason (probably a simple one that i'm missing), I can't work out why i'm getting the error:
A non-null String must be provided to a Text widget.
Any thoughts?
Well, my obvious thought is: post a minimal compilable example, with the full error message.
But another thought that might actually help you to find the mistake yourself is: don't make programming hard on yourself. You are human. Let the machine do the hard work, that is the way we should do this. You want to make sure you never accidentally have a null value in your models? Then write the model so a null value is not possible without your compiler telling you exactly where you failed at compile time:
class UserFontModel {
final String fontFamily;
final FontWeight fontWeight;
const UserFontModel({#required this.fontFamily, #required this.fontWeight})
: assert(fontFamily != null),
assert(fontWeight != null);
}
This class is not instatiable without both fontFamily and fontWeight given, it will warn you at runtime, if you gave one and it was null and it makes sure you cannot accidentally change the fields after they have been checked to be correct. The point here is: this is your compiler. the machine does the work for you. As it should be.
This will not fix your actual problem, but it should fix your problem of not finding your actual problem.

Flutter text not rendering correctly in some cases

I am developing a flutter app to display speed itself. But sometime, the speed isn't rendered correctly in the UI. This doesn't happen always but happens say 5-10% of the time.
I tried to add another text field, with lesser size below it(just to debug), and the same text renders there correctly.
2 screenshots - One correct and one with the bug
I am using a column widget to display the text and this is how I am building the children list:
List<Widget> children = [];
children.add(Container(
child: Text(listenerSpeedText,
style: TextStyle(color: Colors.lightGreen, fontSize: 100.00)),
padding: EdgeInsets.only(bottom: 5.00)));
children.add(Text(
'digitalSpeed: ' + listenerSpeedText,
style: TextStyle(
color: Colors.red,
fontSize: 25.00,
),
));
return children;
Any help would be appreciated. I am not sure what is going on. Do I need to give extra width or height to container?