Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter OpenContainer Animation behavior

Tags:

flutter

I'm doing a simple open container from a card.

I am seeing that the constructor of the widget being opened is called multiple times.

Is this normal behavior? I was planning to do some heavy initialization by getting data from the backend, but don't want to do it in the constructor if the animation is designed to create the object again and again.

Widget build(BuildContext context) {
    return OpenContainer(
      closedColor: Colors.transparent,
      transitionDuration: Duration(seconds: 1),
      closedBuilder: (BuildContext context, VoidCallback action) => Card(
        child: Container(
          padding: EdgeInsets.all(5),
          color: Colors.black54,
          child: InkWell(
            splashColor: Colors.blue.withAlpha(30),
            onTap: () => action(),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  session.sessionName,
                  style: textTheme.headline6,
                ),
                Text('Week:' + session.week.toString()),
                Text('Session:' + session.sessionDay.toString()),
              ],
            ),
          ),
        ),
      ),
      openBuilder: (session.completed)
          ? (BuildContext c, VoidCallback action) =>
              ReviewCompletedSession(session: session)
          : (BuildContext c, VoidCallback action) =>
              ManageSession(key: ValueKey(session.id), session: session),
      tappable: false,
    );
}

The Manage session widget constructor right now is a simple one.

ManageSession({
    Key key,
    @required this.session,
  }) : super(key: key) {
    print('Constructor called');
  }
like image 617
Sravan Kumar Nerella Avatar asked Jun 17 '26 21:06

Sravan Kumar Nerella


1 Answers

Use a stateful widget, and call a async function to initialise once finished call setState (This answer was posted mistakenly, I was trying to comment, now I made it look like an answer)

like image 50
Yadu Avatar answered Jun 20 '26 13:06

Yadu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!