Search code examples
flutterdartmobilescroll

Flutter scroll until end of parent then child start to scroll


how can I make the child listView scroll when parent reach the end of the scroll with my code I can achieve that but you can stop mid scroll when you are holding and that disable parent and start child scroll but leaves it half way

example

Column(
          children: [
            Expanded(
              child: NotificationListener<OverscrollNotification>(
                onNotification: (OverscrollNotification value) {
                  print(value.metrics);
                  if (value.depth == 0) {
                    if (value.overscroll < 0 &&
                        listViewController.offset + value.overscroll <= 0) {
                      if (listViewController.offset != 0) {
                        listViewController.jumpTo(0);
                      }
                      if (listViewScrollPhsics ==
                          const ClampingScrollPhysics()) {
                        setState(() {
                          listViewScrollPhsics =
                              const NeverScrollableScrollPhysics();
                        });
                      }
                      print('1');
                      return true;
                    }
                    if (listViewController.offset + value.overscroll >=
                        listViewController.position.maxScrollExtent) {
                      if (listViewController.offset !=
                          listViewController.position.maxScrollExtent) {
                        listViewController.jumpTo(
                            listViewController.position.maxScrollExtent);
                      }
                      print('2');
                      return true;
                    }
                    listViewController
                        .jumpTo(listViewController.offset + value.overscroll);
                    if (listViewScrollPhsics !=
                        const ClampingScrollPhysics()) {
                      setState(() {
                        listViewScrollPhsics = const ClampingScrollPhysics();
                      });
                    }

                    print('3');
                    return true;
                  }
                  //listview controller
                  else {
                    if (value.overscroll < 0 &&
                        SingleController.offset + value.overscroll <= 0) {
                      if (SingleController.offset != 0) {
                        SingleController.jumpTo(0);
                      }
                      if (listViewScrollPhsics ==
                          const ClampingScrollPhysics()) {
                        setState(() {
                          listViewScrollPhsics =
                              const NeverScrollableScrollPhysics();
                        });
                      }
                      print('4');
                      return true;
                    }
                    if (SingleController.offset + value.overscroll >=
                        SingleController.position.maxScrollExtent) {
                      if (SingleController.offset !=
                          SingleController.position.maxScrollExtent) {
                        SingleController.jumpTo(
                            SingleController.position.maxScrollExtent);
                      }
                      print('5');
                      return true;
                    }
                    SingleController.jumpTo(
                        SingleController.offset + value.overscroll);
                    if (listViewScrollPhsics !=
                        const ClampingScrollPhysics()) {
                      setState(() {
                        listViewScrollPhsics = const ClampingScrollPhysics();
                      });
                    }
                    print('6');
                    return true;
                  }
                },
                child: SingleChildScrollView(
                  physics: SinglePhysics,
                  controller: SingleController,
                  child: Column(
                    children: [
                      Container(
                        decoration: const BoxDecoration(color: Colors.amber),
                        width: currentWidth,
                        height: 200,
                      ),
                      Container(
                        height: currentHeight - 50 - 80,
                        width: currentWidth,
                        decoration: const BoxDecoration(color: Colors.red),
                        child: Column(
                          children: [
                            Container(
                              width: currentWidth,
                              height: 50,
                              decoration:
                                  const BoxDecoration(color: Colors.purple),
                            ),
                            Expanded(
                              child: ListView.builder(
                                  physics: listViewScrollPhsics,
                                  controller: listViewController,
                                  shrinkWrap: true,
                                  itemCount: 20,
                                  itemBuilder: (BuildContext context, index) {
                                    return Container(
                                      margin: const EdgeInsets.only(top: 10),
                                      width: currentWidth,
                                      height: 70,
                                      decoration: const BoxDecoration(
                                          color: Colors.green),
                                    );
                                  }),
                            ),
                          ],
                        ),
                      )
                    ],
                  ),
                ),
              ),
            ),
            Footer(currentWidth: currentWidth),
          ],
        ),

I want the parent scroll stop whenever it is out of the page and start the child scroll


Solution

  • You can achieve this with Slivers check this documentation.

    Example :

      body: CustomScrollView(
        slivers: [
          const SliverAppBar(
            expandedHeight: 200,
            flexibleSpace: FlexibleSpaceBar(title: Text('Your Content')),
            centerTitle: true,
            backgroundColor: Colors.blue,
          ),
          SliverList.builder(
            itemCount: 50,
            itemBuilder: (context, index) => Container(
              height: 55,
              color: Colors.purple,
              margin: const EdgeInsets.all(15),
            ),
          ),
        ],
      )