kphanipavan / modal_progress_hud_nsn

Modal animated loading overlay package for long running tasks.
https://pub.dev/packages/modal_progress_hud_nsn
MIT License
5 stars 10 forks source link

Re-instantiating some children when isAsyncCall is true #8

Open carolineplayermg opened 1 year ago

carolineplayermg commented 1 year ago

If the ModalProgressHUD has "isAsyncCall" set to true, and wraps a widget which executes code that returns a Future, it seems to prompt a re-initialisation of some widgets in it's child subtree, see here the PageController will reset to page zero despite an attempt to animate to the next page. (But if isAsyncCall = false then it will not have an issue).

Code to recreate:

import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:modal_progress_hud_nsn/modal_progress_hud_nsn.dart'; 

class ScratchPage extends StatefulWidget {
  ScratchPage({Key? key}) : super(key: key);

  @override
  _ScratchPageState createState() => _ScratchPageState();
}

class _ScratchPageState extends State<ScratchPage> {

  bool loading = false;
  PageController pageController = PageController(); 

  @override
  Widget build(BuildContext context) { 
    return Scaffold( 
      body: Stack( 
        children: <Widget>[
          ModalProgressHUD(
            inAsyncCall: loading,
            opacity: 0,
             child: Column(children: [
              Expanded(child:
                PageView.builder( 
                physics: const AlwaysScrollableScrollPhysics(),
                controller: pageController,
                itemBuilder: ((context, index) {
                  return Center(child:  Text("$index", style: TextStyle(fontSize: 20),) ) ;
                  }
                ),
                itemCount: 5,
                clipBehavior: Clip.none
              )    
                )  ,
              TextButton(child: Text("Wait for a future"), 
              onPressed: () async {
                setState(() {
                  loading = true;
                });
                log("pre: pageController page: ${pageController.page}");
                await Future.delayed(const Duration(seconds: 2));
                pageController.nextPage(duration: const Duration(milliseconds: 200), curve:Curves.easeInOut );
                log("post: pageController page: ${pageController.page}");
                setState(() {
                  loading = false;
                });
              })
             ])   
          )
        ]
      )
    );
  }
}

`

kphanipavan commented 1 year ago

Please use the current git version, #12 seems to fixed related issue.

kphanipavan commented 9 months ago

Please test the latest version.

-Pvn