Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]: Animation for showing/hiding a dialog #25

Closed
1 task done
DevTello opened this issue Apr 10, 2024 · 12 comments
Closed
1 task done

[Question]: Animation for showing/hiding a dialog #25

DevTello opened this issue Apr 10, 2024 · 12 comments
Assignees
Labels
question Further information is requested

Comments

@DevTello
Copy link

DevTello commented Apr 10, 2024

What is your question?

How do I set up an animation for showing/hiding a dialog?

Code of Conduct

  • I agree to follow this project's Code of Conduct
@DevTello DevTello added the question Further information is requested label Apr 10, 2024
@feduke-nukem
Copy link
Owner

@DevTello, hello.

Do you mean setting up custom animation?

@DevTello
Copy link
Author

Well, I need to set a Duration of showing and fading of a dialog, I managed to set up only decoration: const EasyDialogAnimation.fade()

@feduke-nukem
Copy link
Owner

feduke-nukem commented Apr 11, 2024

Well, I need to set a Duration of showing and fading of a dialog, I managed to set up only decoration: const EasyDialogAnimation.fade()

Each dialog has a property named animationConfiguration of type EasyDialogAnimationConfiguration. Every applied animation is working according to this to look synchronized.

final content = Container(
  height: 150.0,
  color: Colors.blue[900],
  alignment: Alignment.center,
  child: Text(
    _selectedPosition.name,
    style: const TextStyle(
      color: Colors.white,
      fontSize: 30.0,
    ),
  ),
);

content
    .positioned(
      animationConfiguration:
          const EasyDialogAnimationConfiguration.bounded(
        duration: Duration(seconds: 1),
        reverseDuration: Duration(milliseconds: 500),
      ),
      autoHideDuration: _isAutoHide
          ? Duration(milliseconds: _autoHideDuration.toInt())
          : null,
    )
    .fade()
    .show();

To be able to configure durations independently you could create your own custom EasyDialogAnimation and override onShow/onHide methods to trigger independent AnimationController to control animation in a desired way.

Something like that:

final class IndependentFade<D extends EasyDialog> extends EasyDialogAnimation<D> {
  late final AnimationController _animationController;
  final Duration duration;
  final Duration reverseDuration;
  final TickerProvider vsync;

  IndependentFade({
    required this.duration,
    required this.reverseDuration,
    required this.vsync,
  });

  @override
  void init() {
    _animationController = AnimationController(
      duration: duration,
      reverseDuration: reverseDuration,
      vsync: vsync,
    );
  }

  @override
  void onShow() {
    super.onShow();

    _animationController.forward();
  }

  @override
  void onHide() {
    _animationController.reverse();
    super.onHide();
  }

  @override
  Widget call(D dialog) {
    return FadeTransition(
      opacity: CurvedAnimation(
        parent: _animationController,
        curve: curve,
      ),
      child: dialog.content,
    );
  }
}

For now it is required to explicitly pass vsync but technically each animation already has it under the hood. Maybe I should implement passing EasyDialogContext into lifecycle methods to avoid boilerplate.

@DevTello
Copy link
Author

animationConfiguration: const EasyDialogAnimationConfiguration.bounded( duration: Duration(seconds: 1), reverseDuration: Duration(seconds: 1), ),
forward duration plays 2x faster for me than reverseDuration, given they have the same Duration) is this the case for you?

@feduke-nukem
Copy link
Owner

animationConfiguration: const EasyDialogAnimationConfiguration.bounded( duration: Duration(seconds: 1), reverseDuration: Duration(seconds: 1), ), forward duration plays 2x faster for me than reverseDuration, given they have the same Duration) is this the case for you?

Could you provide sample code?

@DevTello
Copy link
Author

Sorry for the delay, I'm overloaded a bit. Will do as soon as I can)

@DevTello
Copy link
Author

DevTello commented Apr 22, 2024

Here you go. Subjectively these 2 sec are perceived as 1. Using Android 10

import 'package:flutter/material.dart';
import 'package:flutter_easy_dialogs/flutter_easy_dialogs.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
      builder: FlutterEasyDialogs.builder(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  EasyDialog? _dialog;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Flutter Demo Home Page'),
        ),
        body: Center(
          child: MaterialButton(onPressed: showDialog, child: Text('Show')),
        ));
  }

  Future<void> showDialog() async {
    _dialog?.hide();
    _dialog = FullScreenDialog(
      animationConfiguration: const EasyDialogAnimationConfiguration.bounded(
        //plays 2x faster than reverseDuration for some reason
        duration: Duration(milliseconds: 2000),
        reverseDuration: Duration(milliseconds: 1000),
      ),
      decoration: const EasyDialogAnimation.fade(),
      content: Scaffold(
        body: SafeArea(
          child: Placeholder(child: Center(child: MaterialButton(onPressed: hideDialog, child: Text('Hide')))),
        ),
      ),
    )..show();
  }

  void hideDialog() {
    _dialog?.hide();
    _dialog = null;
  }
}

@DevTello
Copy link
Author

Here is the link to the playground: https://flutlab.io/editor/bdca479b-172a-4311-81ef-fd0efc871343

@feduke-nukem
Copy link
Owner

Here is the link to the playground: https://flutlab.io/editor/bdca479b-172a-4311-81ef-fd0efc871343

Thanks.

I will take a close look as soon as possible.

@feduke-nukem
Copy link
Owner

Here is the link to the playground: https://flutlab.io/editor/bdca479b-172a-4311-81ef-fd0efc871343

Try to provide Curves.linear into const EasyDialogAnimation.fade(curve: Curves.linear).

The default curve is adding described "slow" behaviour.

@DevTello
Copy link
Author

thanks, it helped, but not quite obvious)

@feduke-nukem
Copy link
Owner

thanks, it helped, but not quite obvious)

Probably I should set all curves as Linear by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants