how to disable a widget inside a pageview if there is no data

Viewed 35

i have a pageview with widget1 and widget2 both use a futurebuilder i want to disable widget2 and swipe if there is no data in his futurebuilder

body: PageView(
 controller: controller,
  scrollDirection: Axis.vertical,
                child: Column(
                  
                  children: <Widget>[
                    SizedBox(
                      
                      child: PageView(
                       scrollDirection: Axis.horizontal

                      children: [
                        Widget1(),
                        Widget2()
                      ]),
                    ),
                    Widget3(),

                   
                    Widget4(),

                   
                    widget5(),
                    widget6(),

                   
                  ],
                ),
              ), 
1 Answers

You can simply create a list containing all widgets to spread in the PageView() as shown below.

List<Widget> allWidgets = [];

body: PageView(
 controller: controller,
  scrollDirection: Axis.vertical,
                child: Column(
                  
                  children: <Widget>[
                    SizedBox(
                      
                      child: PageView(
                       scrollDirection: Axis.horizontal

                      children: [
                       ...allWidgets
                      ]),
                    ),
                    Widget3(),

                   
                    Widget4(),

                   
                    widget5(),
                    widget6(),

                   
                  ],
                ),
              ), 

And add your custom widgets which are all have their own FutureBuilder() as you just said to allWidgets List. You can do this with if(snapshot.HasData){} as you can see in example.

class Widget1 extends StatefulWidget {
                    Widget1({Key? key}) : super(key: key);
                  
                    @override
                    State<Widget1> createState() => _Widget1State();
                  }
                  
                  class _Widget1State extends State<Widget1> {
                    @override
                    Widget build(BuildContext context) {
                      return Scaffold(body: FutureBuilder(
                        future: someData,
                        builder: (BuildContext context, AsyncSnapshot snapshot) {
                          if (snapshot.hasData) {
    allWidgets.add(Widget1());
                            return Container();
                          }else{
                            return CircularProgressIndicator();
                          }
                          
                        },
                      ),
                      );
                    }
                  }
Related