Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a Button on bottom of SliverAppBar and make it overlap on ExtentList?

I've try to put a Column(a Container inside) in SliverAppBar bottom property as a button, but it can't overlap the ExtentList ,just overflow the bottom margin. I want to make it overlapped just like Spotify App do.

this is Spotify Sample: https://imgur.com/VrZRY4c

this is what I've tried: https://imgur.com/4bNZw8j

My code:

class _MenuListState extends State<MenuList> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: <Widget>[
          SliverPadding(
            padding: EdgeInsets.only(top: 10, bottom: 10),
            sliver: SliverAppBar(
              pinned: true,
              expandedHeight: 300,
              title: Text(
                'Testing',
                style: TextStyle(color: Colors.red),
              ),
              flexibleSpace: FlexibleSpaceBar(
                title: Text(''),
                background: Image.asset(
                  'images/w.jpg',
                  fit: BoxFit.cover,
                ),
              ),
              bottom: PreferredSize(
                child: Column(children: <Widget>[
                  Text(
                    'test',
                    style: TextStyle(),
                  ),
                  Container(
                    decoration: BoxDecoration(
                      color: Color.fromRGBO(109, 76, 65, 1),
                      borderRadius: BorderRadius.all(
                        Radius.circular(20),
                      ),
                    ),
                    height: 54,
                    width: 100,
                  ),
                ]),
              ),
            ),
          ),
          SliverFixedExtentList(//extentlist)
        ],
      ),
    );
  }
}
like image 380
ryanhuang1124 Avatar asked Mar 03 '23 01:03

ryanhuang1124


2 Answers

Try this

class _MenuListState extends State<MenuList> {
  static const double _appBarBottomBtnPosition =
      24.0; //change this value to position your button vertically

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            title: Text(
              'Testing',
              style: TextStyle(color: Colors.red),
            ),
          ),
          SliverAppBar(
            pinned: true,
            expandedHeight: 300.0,
            flexibleSpace: FlexibleSpaceBar(
              centerTitle: true,
              titlePadding: EdgeInsets.only(bottom: 25),
              title: Text('Title'),
              background: Image.asset(
                'images/w.jpg',
                fit: BoxFit.cover,
              ),
            ),
            bottom: PreferredSize(
              preferredSize: const Size.fromHeight(0.0),
              child: Transform.translate(
                offset: const Offset(0, _appBarBottomBtnPosition),
                child: RaisedButton(
                  shape: StadiumBorder(),
                  child: Text("Click Here"),
                  onPressed: () {},
                ),
              ),
            ),
          ),
          SliverPadding(
            padding: EdgeInsets.only(top: _appBarBottomBtnPosition),
          ),
          SliverFixedExtentList(
              //extentlist
          ),
        ],
      ),
    );
  }
}
like image 178
Crazy Lazy Cat Avatar answered Apr 29 '23 19:04

Crazy Lazy Cat


The answer provided by @Crazy Lazy Cat works .. but if you are worried about using 2 SliverAppBar 's. you can substitute with the code below to replace the 2 SliverAppBar

 SliverAppBar(
            expandedHeight: 500.0,
            flexibleSpace: FlexibleSpaceBar(
              background: Image.asset(
                'images/w.jpg',
                fit: BoxFit.cover,
              ),
            ),
            bottom: PreferredSize(
              preferredSize: const Size.fromHeight(0.0),
              child: Transform.translate(
                offset: const Offset(0, _appBarBottomBtnPosition),
                child: RaisedButton(
                  shape: StadiumBorder(),
                  child: Text("Click Here"),
                  onPressed: () {},
                ),
              ),
            ),
          ),
like image 40
David Machara Avatar answered Apr 29 '23 19:04

David Machara