flutter-intro-slider icon indicating copy to clipboard operation
flutter-intro-slider copied to clipboard

Make compatible with hot reload

Open saddy001 opened this issue 5 years ago • 8 comments

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()));

saddy001 avatar Jun 07 '19 16:06 saddy001

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

duytq94 avatar Jun 10 '19 02:06 duytq94

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

Detail here

duytq94 avatar Jun 12 '19 09:06 duytq94

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.

saddy001 avatar Jun 12 '19 16:06 saddy001

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

freitzzz avatar Aug 26 '19 10:08 freitzzz

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 ?

deepss1 avatar Aug 28 '19 06:08 deepss1

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.

freitzzz avatar Aug 28 '19 11:08 freitzzz

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 ?

deepss1 avatar Aug 28 '19 17:08 deepss1

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.

michael-markl avatar Mar 04 '23 13:03 michael-markl