Issue when I try creating Sliver app bar and pageview in the same widget

535 Views Asked by At

I am facing issues when it comes to logic while I am working with my app I want a sliver appBar with and a page view in the same widget when i do that and assign a custom scroll view for each page of my pageviews I get problems but if I declared a sliver app bar on each page of the pages it works fine and at the same time I should not have a nested scroll view in my pageview widget now I don't think that I should write an app bar for each one of them when I could just write it my page view widget any thoughts this is my code

Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        top: true,
        bottom: false,
        child:
         NestedScrollView(
          floatHeaderSlivers: true,
            headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
              return <Widget>[
                  SliverOverlapAbsorber(
                    handle: NestedScrollView.sliverOverlapAbsorberHandleFor(
                        context),         
        sliver:
               SliverAppBar(
        // toolbarHeight: 50,
        backgroundColor: Color.fromRGBO(255, 255, 255, 1),
        title: const Text(
          'Partnerna',
          style: TextStyle(
              fontSize: 21,
              fontWeight: FontWeight.bold,
              fontStyle: FontStyle.normal,
              color: linerColorUp),
        ),
        actions: [
          Padding(
            padding: const EdgeInsets.symmetric(
                vertical: 0, horizontal: 10),
            child: Container(
              alignment: Alignment.centerRight,
              // color: Colors.amber,
              // width: double.infinity,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: const [
                  CircelCntainerBackgroundWidget(
                      backGroundColor: buttonbackgroundcolor,
                      child: Padding(
                        padding: EdgeInsets.all(3),
                        child: FaIcon(
                          FontAwesomeIcons.squarePlus,
                          size: 18,
                        ),
                      )),
                  SizedBox(
                    width: 20,
                  ),
  
              CircelCntainerBackgroundWidget(
                  backGroundColor: buttonbackgroundcolor,
                  child: Icon(
                    Icons.notification_add_rounded,
                    size: 21,
                  )),
              SizedBox(
                width: 20,
              ),
  
              // SizedBox(width: 10,),
              CircleAvatar(
                radius: 14,
                backgroundImage: NetworkImage(
                    "https://th.bing.com/th/id/OIP.2tWiaVWFJjvC1HhJQuTtCwHaHt?w=173&h=181&c=7&r=0&o=5&pid=1.7"),
              ),
            ],
          ),
        ),
      )
    ],
    // expandedHeight: 200,
    floating: true,
    pinned: false,
    snap: true,
    forceElevated: innerBoxIsScrolled,
    elevation: 0,
      ),
              )];
        },
        body:
           PageView(
            children: [
              HomeScreen(),
              ConnectScreen(),
              ConnectRequestScreen(),
              MessagScrenn(),
              SettingScreen(),
            ],
            physics: const NeverScrollableScrollPhysics(),
            controller: pageController,
            onPageChanged: onPageChange,
          ),
  ),),
1

There are 1 best solutions below

4
On

try to make a custom Scaffold same as your code, to wrap each of the PageView children.

class MyHomePage extends StatefulWidget {
  const MyHomePage({
    Key? key,
  }) : super(key: key);

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: PageView(
        children: [
          Page1(),
          Page2(),
          Page3(),
        ]
      ),
    );
  }
}

class Page1 extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return CustomScaffold(
      body: (...)
    );
  }
}

class Page2 extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return CustomScaffold(
      body: (...)
    );
  }
}

class Page3 extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return CustomScaffold(
      body: (...)
    );
  }
}

class CustomScaffold extends StatefulWidget {
  final Widget body;
  const CustomScaffold({ Key? key, required this.body}) : super(key: key);

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

class _CustomScaffoldState extends State<CustomScaffold> {
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: NestedScrollView(
        headerSliverBuilder: (context, innerBoxIsScrolled){
          (...)
        },
        body: widget.body,
      ),
    );
  }
}