0

I'm using OpenContainer animation to open a screen that could display alert dialog upon the opening of the screen - the case of the item the screen is trying to display is no longer valid or deleted.

Because OpenContainer renders the screen during the animation, the alert dialog is displayed several times.

My attempt to address the issue was to modify the OpenContainer buildPage method to return animation status to openBuilder callback. Is there better way to do without modifying OpenContainer code?

child: AnimatedBuilder(
        animation: animation,
        builder: (BuildContext context, Widget child) {
          if (animation.isCompleted) {
            return SizedBox.expand(
              child: Material(
                color: openColor,
                elevation: openElevation,
                shape: openShape,
                child: Builder(
                  key: _openBuilderKey,
                  builder: (BuildContext context) {
                    return openBuilder(context, closeContainer, false); // added false
                  },
                ),
              ),
            );
          }

Code to reproduce the issue - https://gist.github.com/MartinJLee/0992a986ad641ef5b4f477fb1ce69249 the issue

e-j5
  • 1,759
  • 1
  • 11
  • 23
  • Consider using an [AlertDialog](https://api.flutter.dev/flutter/material/AlertDialog-class.html) instead. – dev-aentgs Jun 27 '20 at 09:07
  • @dev-aentgs I'm using AlertDialog – e-j5 Jun 27 '20 at 09:31
  • Sorry, I meant to say if the `OpenContainer` is only displaying the new screen for `AlertDialog` then use `AlertDialog` directly as it hides the original screen by default. Can you add the images for the effect you are trying to achieve? – dev-aentgs Jun 27 '20 at 09:40
  • 1
    Yes, I'm not just displaying `AlertDialog` the dialog will display on the top of the original content which may be deleted by the publisher. The screenshot is what will be displayed after the animation by `OpenContainer`. – e-j5 Jun 27 '20 at 12:37
  • Thanks. Try the below answer. – dev-aentgs Jun 27 '20 at 12:39
  • 1
    I guess I should be using something like https://stackoverflow.com/questions/49466556/flutter-run-method-on-widget-build-complete – e-j5 Jun 30 '20 at 22:20

2 Answers2

0

You can add a listener to your AnimationController something like this

Consider you have an AnimationController like this -

AnimationController _animationController =  AnimationController(
            vsync: this, 
            duration: Duration(seconds: 5),
      );

Then you can add a Status Listener to this _animationController using the addStatusListener method, something like this -

_animationController.addStatusListener((status) {
        if(status == AnimationStatus.completed){
          //Do something here
        }
    });

This listener will be called everytime the animation state changes.

Hope this helps!

Tanuj
  • 2,032
  • 10
  • 18
  • As far as I know, we cannot add `AnimationController` to `OpenContainer` – e-j5 Jun 27 '20 at 12:17
  • 1
    animation parameter of AnimationBuilder takes an instance of AnimationController. – Tanuj Jun 27 '20 at 12:37
  • Sorry I updated my question, I'm asking if there is a better way to address the issue without modifying `OpenContainer` code – e-j5 Jun 28 '20 at 00:47
0

I was able to do using the following code on the container for openBuilder.

  void initState() {
    super.initState();
    WidgetsBinding.instance
        .addPostFrameCallback((_) => yourFunction(context));
  }

see original answers - Flutter: Run method on Widget build complete

e-j5
  • 1,759
  • 1
  • 11
  • 23