how do I animate the opening screen text in flutter?

Issue

I tried the other StackOverflow answers and in all, that onPressed() is mentioned and the setState() is called in there but I don’t know where to call setState() in my app as I don’t have any buttons. so does setState() really required here?
This is my main class

class OpeningScreen extends StatefulWidget {
  @override
  _OpeningScreenState createState() => _OpeningScreenState();
}

class _OpeningScreenState extends State<OpeningScreen> {
  @override

  void initState(){
    super.initState();
    Timer(Duration(seconds: 5), () => Navigator.of(context)
        .pushAndRemoveUntil(MaterialPageRoute(
        builder: (context) => home_screen()),
            (Route<dynamic> route) => false));
  }
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: openingScreenBgDecoration,
        child: Center(
          child: Column(
            children: [
              textAnimation(welcomeText),
            ],
          ),
        ),
      ),
    );
  }
}

this is where I declared my animated default text style

String welcomeText = "Welcome to My App";
bool animated = true;

TextStyle openingScreenTextStyle =
    TextStyle(
        color: Colors.black,
        fontSize: 48.0,
        fontWeight: FontWeight.bold,
    );

TextStyle openingScreenTextStyle1 =
TextStyle(
    color: Colors.black,
    fontSize: 24.0,
    fontWeight: FontWeight.bold,
);

textAnimation (welcomeText){
    AnimatedDefaultTextStyle(
        child: Text(welcomeText),
        style: animated ? openingScreenTextStyle : openingScreenTextStyle1,
        duration: Duration(milliseconds: 200));
    return textAnimation;
}

This is the error I got

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following _TypeError was thrown building OpeningScreen(dirty, state: _OpeningScreenState#aa235):
type '(dynamic) => dynamic' is not a subtype of type 'Widget'

The relevant error-causing widget was:
  OpeningScreen file:///C:/Users/IdeaProjects/i_app/lib/main.dart:5:13

When the exception was thrown, this was the stack:
#0      _OpeningScreenState.build (package:i_app/ui/neercs_gninepo.dart:29:15)
#1      StatefulElement.build (package:flutter/src/widgets/framework.dart:4663:28)
#2      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4546:15)
#3      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
#4      Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
#5      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4525:5)
#6      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4710:11)
#7      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4520:5)
...     Normal element mounting (132 frames)
#139    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
#140    MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5991:32)
...     Normal element mounting (267 frames)
#407    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
#408    Element.updateChild (package:flutter/src/widgets/framework.dart:3258:18)
#409    RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1174:16)
#410    RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1145:5)
#411    RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1087:17)
#412    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2620:19)
#413    RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1086:13)
#414    WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:927:7)
#415    WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:908:7)
(elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)

════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following _TypeError was thrown building OpeningScreen(dirty, state: _OpeningScreenState#aa235):
type '(dynamic) => dynamic' is not a subtype of type 'Widget'

The relevant error-causing widget was: 
  OpeningScreen file:///C:/Users/IdeaProjects/i_app/lib/main.dart:5:13
When the exception was thrown, this was the stack: 
#0      _OpeningScreenState.build (package:i_app/ui/neercs_gninepo.dart:29:15)
#1      StatefulElement.build (package:flutter/src/widgets/framework.dart:4663:28)
#2      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4546:15)
#3      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
#4      Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)

Solution

It worked. Made few changes.

WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      setState(() {
      animated = !animated;
    });
    });

Inside the initState() only I declared setState() and for this error

type '(dynamic) => dynamic' is not a subtype of type 'Widget'

I directly declared AnimatedDefaultTextStyle inside the children

Answered By – ghost deathrider

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.