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

problem with OnTap #15

Closed
mohammadne opened this issue Nov 29, 2019 · 11 comments
Closed

problem with OnTap #15

mohammadne opened this issue Nov 29, 2019 · 11 comments

Comments

@mohammadne
Copy link

I tried your documentation
but when I register OnTap I loose animation effect.
this is my code:

LikeButton(
                            size: 30.0,
                            circleColor: CircleColor(
                              start: Color(0xffE9C349),
                              end: Colors.red,
                            ),
                            onTap: (isLiked) async {
                              await gen.addMusicsToFavorite();
                              final Completer<bool> completer =
                                  new Completer<bool>();
                              Timer(
                                const Duration(milliseconds: 200),
                                () {
                                  completer.complete(gen.music['is_favorite']);
                                  return completer.future;
                                },
                              );
                            },
                            bubblesColor: BubblesColor(
                              dotPrimaryColor: Colors.red,
                              dotSecondaryColor: Color(0xffE9C349),
                            ),
                            isLiked: gen.music['is_favorite'],
                            likeBuilder: (bool isLiked) {
                              return isLiked
                                  ? Icon(
                                      Icons.favorite,
                                      color: Color(0xffE9C349),
                                      size: 30.0,
                                    )
                                  : Icon(
                                      Icons.favorite_border,
                                      color: Colors.white,
                                    );
                            },
                          ),

in my provider class I execute addMusicsToFavorite and then the result will be gen.music['is_favorite'],

@zmtzawqlp
Copy link
Member

why use Timer? it's just demo to do as we request service.

                        onTap: (isLiked) async {
                          await gen.addMusicsToFavorite();
                          final Completer<bool> completer =
                              new Completer<bool>();
                          Timer(
                            const Duration(milliseconds: 200),
                            () {
                              completer.complete(gen.music['is_favorite']);
                              return completer.future;
                            },
                          );
                        },

you need to know how to do it with async /await. i did't see any return from your code

@mohammadne
Copy link
Author

mohammadne commented Nov 30, 2019

I am new to this future topic , but what am I doing is below now is :

onTap: (_) => gen.addFavoriteMusics()

and addFavoriteMusics is

  Future<bool> addFavoriteMusics() async {
    try {
      var resp = await Music().name('/favorite').favoritePost(
      body: {'favorite': _music['is_favorite'] == true ? '0' : '1'},
      id: _music['id'],
      );
      _music['is_favorite'] = !_music['is_favorite'];
      //if it is done , do following with server
      _favoriteMusics =
            await Music().include(['album.artists']).favorite().$get();
        notifyListeners();
        return _music['is_favorite'];
      
    } catch (e) {
      return null;
    }
  }

it workes fine but it doesn't have any animation .
can you please help me that what is my problem ?

@mohammadne
Copy link
Author

also I want optimistic updating effect , how can I implement that ?

@mohammadne
Copy link
Author

any idea?

@guit4eva
Copy link

Did you figure this out?

@zmtzawqlp
Copy link
Member

plz provide a runnable demo to show your issue。

@guit4eva
Copy link

guit4eva commented Dec 15, 2019

Rough working code for reference for @mohammadne :

class _LikeButtonMainState extends State<LikeButtonMain> {
  bool _isLiked = false;
  @override
  Widget build(BuildContext context) {
    return LikeButton(
      onTap: (bool isLiked) {
        return _like(_isLiked).then((value) {
          _isLiked = value;
          return value;
        });
      },
      circleColor: CircleColor(start: Color(0xff00ddff), end: Color(0xff0099cc)),
      bubblesColor: BubblesColor(
        dotPrimaryColor: Color(0xff33b5e5),
        dotSecondaryColor: Color(0xff0099cc),
      ),
      likeBuilder: (bool isLiked) {
        return Icon(
          Icons.favorite,
          color: _isLiked ? Color(0xFFDF5757) : Colors.grey,
        );
      },
    );
  }

  Future<bool> _like(isLiked) async {
    isLiked = !isLiked;
    return isLiked;
  }
}

@zmtzawqlp
Copy link
Member

class _LikeButtonMainState extends State {
bool _isLiked = false;
@OverRide
Widget build(BuildContext context) {
return LikeButton(
onTap: (bool isLiked) {
return _like(isLiked);
},
circleColor: CircleColor(start: Color(0xff00ddff), end: Color(0xff0099cc)),
bubblesColor: BubblesColor(
dotPrimaryColor: Color(0xff33b5e5),
dotSecondaryColor: Color(0xff0099cc),
),
likeBuilder: (bool isLiked) {
return Icon(
Icons.favorite,
color: _isLiked ? Color(0xFFDF5757) : Colors.grey,
);
},
);
}

Future _like(isLiked) async {
isLiked = !isLiked;
return isLiked;
}
}

@zmtzawqlp
Copy link
Member

onTap is asynchronous call back

@zmtzawqlp
Copy link
Member

please double check codes in demo

@mohammadne
Copy link
Author

Thanks alot.

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

No branches or pull requests

3 participants