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

Data not being updated in stateful widget. #4

Closed
MojtabaTavakkoli opened this issue Apr 7, 2020 · 18 comments
Closed

Data not being updated in stateful widget. #4

MojtabaTavakkoli opened this issue Apr 7, 2020 · 18 comments

Comments

@MojtabaTavakkoli
Copy link

first of all Thanks for the great Library
when trying to change its data after button pressed nothing happens in the UI. onPress() works correctly and changes the data I want (I can see it is changed using print()) but in the UI the button stays the same as it was since the beginning. or maybe im not using the library properly...

heres the code:


                              ...options.map((option) =>
                                  SpringButton(
                                    SpringButtonType.WithOpacity,
                                    Padding(
                                      padding: EdgeInsets.symmetric(vertical: 4.0, horizontal: 30.0),
                                      child: Container(
                                        height: 40.0,
                                        width: clickedOption,
                                        decoration: BoxDecoration(
                                          color: btnColor,
                                          borderRadius: const BorderRadius.all(const Radius.circular(30.0)),
                                          boxShadow: [
                                            BoxShadow(
                                              color: Color(0x80000000),
                                              blurRadius: 10.0,
                                              offset: Offset(0.0, 5.0),
                                            ),
                                          ],
                                        ),
                                        child: Center(
                                          child: Text(option,
                                            style: TextStyle(
                                                fontSize: 18.0,
                                                fontWeight: FontWeight.bold,
                                                color: Colors.white),
                                          ),
                                        ),
                                      ),
                                    ),
                                    onTapDown: (_) {
                                      setState(() {
                                        _answers[_currentIndex] = option;
                                        print(_answers[_currentIndex]);
                                        btnColor = Colors.yellowAccent;
                                        print(option);
                                      });
                                    },
//                                            onLongPress: () ,
                                    onLongPressEnd: (_) {
                                      setState(() {
                                        _answers[_currentIndex] = option;
                                        print(_answers[_currentIndex]);
                                        print(option);
                                      });
                                      },
                                  ),
                              ),

the options is the data coming from the database and after each click should change but what happens is that data updates in options but nothing happens in UI.

@aliyigitbireroglu
Copy link
Owner

Hello,

In most of my libraries, I try to make use of Flutter's cache system. I hope I am doing it right but I am not 100% sure 😃 Most probably, that's the problem.

I can recommend two things which I believe can fix this problem:

  1. Use the "useCache" parameter with the value "false" when creating the SpringButton. I think this alone can solve the issue.
  2. If that doesn't work, try wrapping your inner widget with a ValueListenableBuilder or similar and make use of that to update the UI.

@MojtabaTavakkoli
Copy link
Author

Mate u just Blow it 😁😁❤❤💥💥💥💥💥

the option N.1 solved the problem and the next one taught me something new in flutter world 😁

Note:
if you`re having the same issue and cant add the "useCache" parameter, you have to note that it should declare after your SpringButton child. or you will face the "positional arguments must occur after named arguments" Error! some example below:

SpringButton(
          SpringButtonType.WithOpacity,
          Padding(
               padding: EdgeInsets.symmetric(vertical: 4.0, horizontal: 30.0),
               child: Container(),
               ),
           useCache: false,
           onTapDown: (_) {},
           onLongPress: (_) {} ,
           onLongPressEnd: (_) {},
),

@aliyigitbireroglu
Copy link
Owner

I am glad to have helped. Let me know if you have any other questions.

@MojtabaTavakkoli
Copy link
Author

well sadly I'm facing another issue for onPress() methods. as I send some data to onPressed() method, the next time I update the data and call the onPress() it returns the first-time-received value. the following sample may be more clear:

`
...options.map((option) =>
SpringButton(
SpringButtonType.WithOpacity,
Padding(
padding: EdgeInsets.symmetric(vertical: 4.0, horizontal: 30.0),
child: Container(
height: 40.0,
width: clickedOption,
decoration: BoxDecoration(
color: btnColor,
borderRadius: const BorderRadius.all(const Radius.circular(30.0)),
boxShadow: [
BoxShadow(
color: Color(0x80000000),
blurRadius: 4.0,
offset: Offset(2.0, 2.0),
),
],
),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 4.0),
child: Center(
child: Text(option,
style: TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.bold,
color: Colors.white),
),
),
),
),
),
useCache: false,

                                onTapDown: (_) {
                                  setState(() {
                                    _answers[_currentIndex] = option;
                                    print(_answers[_currentIndex]);
                                    btnColor = Colors.yellowAccent;
                                  });
                                },

                                onLongPressEnd: (_) {
                                  setState(() {
                                    _answers[_currentIndex] = option;
                                    print(_answers[_currentIndex]);
                                    print(option);
                                  });
                                  },
                              ),`

no matter how many times I send new data. the onPress() will always return the first option.

@aliyigitbireroglu
Copy link
Owner

Yes this is actually a problem I also had and I did make some revisions to the code to fix it but I haven't updated the library on Github or Pubdev yet. Until I do, can you try making your SpringButton a Stateful Widget? I think that should solve the problem.

@MojtabaTavakkoli
Copy link
Author

actually it is in stful widget rn. what do you mean exactly?

@aliyigitbireroglu
Copy link
Owner

What I mean is, use a Stateful Widget that returns only the SpringButton. At the moment, your SpringButton might be a part of a Stateful Widget with a larger widget tree and it is possible that Flutter stops rebuilding the widget before arriving to the SpringButton itself. At least, this is my understanding of Flutter.

So two things you can try:

`
class MySpringButton extends StatefulWidget { ...

class _MySpringButtonState extends State {
@OverRide
Widget build(BuildContext context) {
return SpringButton(...
`

  1. And if this doesn't work, you can update the "key" of the SpringButton when you want it to run a different function which will force Flutter to recreate it.

I know neither of these are ideal solutions but until I release an update, you can use these so your development isn't halted. Then you can simply replace them.

@MojtabaTavakkoli
Copy link
Author

aha now i get it. well both solutions made it laggy on android 4.4 to 6 ...
thanks anyways for your time ❤

@aliyigitbireroglu
Copy link
Owner

Thats weird, I can understand the second solution causing a lag but the first one should have been fine. Anyway, I will leave this issue open and notify you when I release an update to fix the problem 👍🏻

@MojtabaTavakkoli
Copy link
Author

actually I was testing on emulators. maybe on real device it would work fine. any ways i would be glad to help you on this if you're busy ..

@aliyigitbireroglu
Copy link
Owner

Well I already have a version that is almost ready but of course feel free to commit, I would appreciate it.

@MojtabaTavakkoli
Copy link
Author

Hello. Any update?

@aliyigitbireroglu
Copy link
Owner

Hello and I'm sorry, I have been very busy these days.
I had the chance of checking it today but, unfortunately, now I am unable to recreate the issue. Could you help me by sending a small sample app code that I can use?

@MojtabaTavakkoli
Copy link
Author

Hello and thanks for your time.
Sorry I was away yesterday and didn't see your comment. I created the sample and here it is.

spring_button_showcase.zip

@aliyigitbireroglu
Copy link
Owner

Hi again.

I released a prerelease version coded 1.0.14-beta which is available both on GitHub and at https://pub.dev/packages/spring_button/versions#dev. I believe it fixes the problem. However, additionally, I tested and saw that simply adding the line

key: Key(option)

to your code where SpringButtons are generated also works. So you can solve your issue however you prefer.

@MojtabaTavakkoli
Copy link
Author

Thanks a lot for your time.
It's fixed now and working perfectly. 🎉🎉🎉💥💥💥💥
Well actually I didn't know about the key property ... and now I don't think I ever want that 😅

Thank you again. 💖💖💖

@aliyigitbireroglu
Copy link
Owner

No problem! Let me know if you need any more help and remember the "key" property of Flutter, its very useful😉

@MojtabaTavakkoli
Copy link
Author

Yes guess I have to keep learning it in schedule. Actually I was using a key for animations and didnt know how to use 2 keys at same time.

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

2 participants