简体   繁体   中英

Flutter OpenContainer Animation behavior

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');
  }

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM