I'm relatively new to Flutter building my first mobile app for a client.
I have a stateful widget that is just a title text for a page. I want to fade-in that widget whenever the user navigates to that page.
I've tried a delayed approach with a Timer (which feels a little gross) with a setState inside changing a _isTitleVisible variable which controls the opacity of the AnimatedOpacity widget. This seems to mostly work but after the first navigation, I get a blip of the animation which seems to be running twice.
Here is the current implementation showing the blip of the title.
I'm very open to alternative methods of making this work, this is just what I have found in my own googling.
Any help would be greatly appreciated!
Using Flutter - v1.22.5
Code:
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class FadeInTitle extends StatefulWidget {
@override
_FadeInTitleState createState() => _FadeInTitleState();
}
class _FadeInTitleState extends State<FadeInTitle> {
bool _isTitleVisible = false;
Timer _timer;
@override
void initState() {
// TODO: implement initState
super.initState();
_timer = Timer(
Duration(milliseconds: 300),
() => {
setState(() {
_isTitleVisible = true;
}),
},
);
}
@override
void dispose() {
// TODO: implement dispose
_timer.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
print('FadeInTitle Build');
return AnimatedOpacity(
opacity: _isTitleVisible ? 1.0 : 0.0,
duration: Duration(milliseconds: 500),
child: Text(
'Morning Reflection',
style: TextStyle(
color: Colors.white,
fontSize: 20,
),
),
);
}
}