duytq94 / flutter-intro-slider

Simple and configurable app introduction slider for Flutter
https://pub.dartlang.org/packages/intro_slider
MIT License
677 stars 141 forks source link

Make compatible with hot reload #30

Open saddy001 opened 5 years ago

saddy001 commented 5 years ago

I'd like to resurrect https://github.com/duytq94/flutter-intro-slider/issues/18 because indeed hot reload would be very useful. For me, it's not working even when I use the suggested

Navigator.pushReplacement(
        context, MaterialPageRoute(builder: (context) => IntroScreen()));
duytq94 commented 5 years ago

Definitely need some help since I don't know why hot reload don't affect. Any explanation is a great value.

duytq94 commented 5 years ago

I found this, maybe we stuck in this case: image

Detail here

saddy001 commented 5 years ago

I'm using it within a PostFrameCallback where I async call a

Future<Null> checkIsFirstLaunch() async {
    ...
    Navigator.of(context).pushNamed('/intro');
}

to additionally ask a isFirstLaunch variable (SharedPreferences). Maybe hot reload is lost somewhere on the path. However, I don't know of another way to check if it's the first launch, where we usually want to have intro slides, I guess.

freitzzz commented 5 years ago

I found this, maybe we stuck in this case: image

Detail here

As @duytq94 said, neither hot-reload and setState are possible to change IntroSlider widget as widgets are computed on IntroSlider initState. In order to achieve this, this would require to change all widgets computation to build method instead of initState. Also a workaround for setState is to create a new IntroSlider object and assign it to your widget

deepss1 commented 5 years ago

I found this, maybe we stuck in this case: image Detail here

As @duytq94 said, neither hot-reload and setState are possible to change IntroSlider widget as widgets are computed on IntroSlider initState. In order to achieve this, this would require to change all widgets computation to build method instead of initState. Also a workaround for setState is to create a new IntroSlider object and assign it to your widget

Can you please give a detailed resolution or workaround ?

freitzzz commented 5 years ago

I found this, maybe we stuck in this case: image Detail here

As @duytq94 said, neither hot-reload and setState are possible to change IntroSlider widget as widgets are computed on IntroSlider initState. In order to achieve this, this would require to change all widgets computation to build method instead of initState. Also a workaround for setState is to create a new IntroSlider object and assign it to your widget

Can you please give a detailed resolution or workaround ?

In Flutter there are two type of widgets, Stateless and Stateful Widgets. Stateless Widgets, as the name indicate, dont have a state. This means that you cannot change the widget state, and in this way not indicate the framework that its required to render the widget again. A Stateful Widget has various states throughout its lifecycle and allows to change its state. By changing the widget state, you indicate the framework that is required to render widget again, or at least compute new widget values. This plugin widget (IntroSlider) is a StatefulWidget, yet by changing its state using setState the widget is rendered the same way as it was previously. This is due to all IntroSlider widget computations are done in initState method. When setState is called, framework will call the widget build method, as this method has the responsibility to tell the framework which widget should be rendered on the device screen. In order to fix this problem, all computation that is done in initState method should be moved to build method.

As I said in my last reply, a workaround would be creating a new IntroSlider each time you want to change that state of the widget.

deepss1 commented 5 years ago

It will be really helpful to me and to others if you can share your piece of code where you render IntroSlider. I have completely moved initState computation in build method before it renders anything.

But by creating new IntroSlider, will we lose the previous IntroSlider's state ?

michael-markl commented 1 year ago

If you are using initState, you should also implement onDidUpdateWidget (to reflect changes of the widget on the Intro Slider state). Otherwise, you also prohibit a switch of light/dark mode or a change of the system language to be reflected.