How to overlap SliverList on a SliverAppBar

前端 未结 3 1630
孤街浪徒
孤街浪徒 2020-12-15 05:27

I\'m trying to overlap a SliverList a few pixels over the SliverAppBar. Similar to this post. I\'d like the image in the FlexibleSpaceBar

3条回答
  •  囚心锁ツ
    2020-12-15 05:54

    I found an easier way to achieve that:

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'How to overlap SliverList on a SliverAppBar',
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: Home(),
        );
      }
    }
    
    class Home extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: CustomScrollView(
            slivers: [
              SliverAppBar(
                bottom: PreferredSize(
                  child: Container(),
                  preferredSize: Size(0, 20),
                ),
                pinned: false,
                expandedHeight: MediaQuery.of(context).size.height * 0.4,
                flexibleSpace: Stack(
                  children: [
                    Positioned(
                        child: Image(
                          fit: BoxFit.cover,
                          image: NetworkImage(
                            "https://images.pexels.com/photos/62389/pexels-photo-62389.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260",
                          ),
                        ),
                        top: 0,
                        left: 0,
                        right: 0,
                        bottom: 0),
                    Positioned(
                      child: Container(
                        height: 30,
                        decoration: BoxDecoration(
                          color: Colors.lightBlue[100],
                          borderRadius: BorderRadius.vertical(
                            top: Radius.circular(50),
                          ),
                        ),
                      ),
                      bottom: -1,
                      left: 0,
                      right: 0,
                    ),
                  ],
                ),
              ),
              SliverFixedExtentList(
                itemExtent: 50.0,
                delegate: SliverChildBuilderDelegate(
                  (BuildContext context, int index) {
                    return Container(
                      alignment: Alignment.center,
                      color: Colors.lightBlue[100 * (index + 1 % 9)],
                      child: Text('List Item $index'),
                    );
                  },
                ),
              ),
            ],
          ),
        );
      }
    }
    
    

    You can see it in DartPad: https://dartpad.dev/e2976ad6ede98813b75ee44b1217cc96

    The important part is to use a stack in the flexibleSpace that contains the content you wanna show as a positioned widget and another position widget with the border decoration.

    You also need to use the bottom section of the SliverAppBar setting the preferred size as the same as the widget with border decoration.

    The DartPad does not render the widget properly (it shows a margin at the bottom, but this does not happen in the iOS/Android).

提交回复
热议问题