2

通常,当我们向下滑动 SliverList 时,会在 SliverList 的上层显示一个浮动的 SliverAppBar。像这样:

但是,将 的值更改为 时shrinkWraptrue会显示在或SliverAppBar的元素下方。SliverListSliverGrid

如何解决这个问题?

这是代码:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);


  final String title;

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

class _MyHomePageState extends State<MyHomePage> {


  @override
  Widget build(BuildContext context) {

    return CustomScrollView(
      shrinkWrap: false,
      slivers: <Widget>[

        const SliverAppBar(
          pinned: false,
          floating: true,
          flexibleSpace: FlexibleSpaceBar(
            title: Text('SliverAPPBar'),
          ),
        ),

        SliverGrid.count(
          crossAxisCount: 3,
          children: [
            Padding(
              padding: EdgeInsets.only(left: 24.0, right: 24.0),
              child: Container(color: Colors.red, height: 150.0),
            ),
            Padding(
              padding: EdgeInsets.only(left: 24.0, right: 24.0),
              child: Container(color: Colors.purple, height: 150.0),
            ),
            Padding(
              padding: EdgeInsets.only(left: 24.0, right: 24.0),
              child: Container(color: Colors.green, height: 150.0),
            ),
            Padding(
              padding: EdgeInsets.only(left: 24.0, right: 24.0),
              child: Container(color: Colors.orange, height: 150.0),
            ),
            Padding(
              padding: EdgeInsets.only(left: 24.0, right: 24.0),
              child: Container(color: Colors.yellow, height: 150.0),
            ),
            Padding(
              padding: EdgeInsets.only(left: 24.0, right: 24.0),
              child: Container(color: Colors.pink, height: 150.0),
            ),
            Padding(
              padding: EdgeInsets.only(left: 24.0, right: 24.0),
              child: Container(color: Colors.cyan, height: 150.0),
            ),

            Padding(
              padding: EdgeInsets.only(left: 24.0, right: 24.0),
              child: Container(color: Colors.indigo, height: 150.0),
            ),
            Padding(
              padding: EdgeInsets.only(left: 24.0, right: 24.0),
              child: Container(color: Colors.blue, height: 150.0),
            ),
          ],
        ),


        SliverList(
          delegate: SliverChildListDelegate(
            [
              Padding(
                padding: EdgeInsets.only(left: 24.0, right: 24.0),
                child: Container(color: Colors.pink, height: 100.0),
              ),
              Padding(
                padding: EdgeInsets.only(left: 24.0, right: 24.0),
                child: Container(color: Colors.cyan, height: 100.0),
              ),
              Padding(
                padding: EdgeInsets.only(left: 24.0, right: 24.0),
                child: Container(color: Colors.indigo, height: 100.0),
              ),
              Padding(
                padding: EdgeInsets.only(left: 24.0, right: 24.0),
                child: Container(color: Colors.blue, height: 100.0),
              ),
              Padding(
                padding: EdgeInsets.only(left: 24.0, right: 24.0),
                child: Container(color: Colors.yellow, height: 100.0),
              ),
            ],

          ),
        ),


        SliverFixedExtentList(
          itemExtent: 150.0,
          delegate: SliverChildListDelegate(
            [
              Padding(
                padding: EdgeInsets.only(left: 24.0, right: 24.0),
                child: Container(color: Colors.red, height: 150.0),
              ),
              Padding(
                padding: EdgeInsets.only(left: 24.0, right: 24.0),
                child: Container(color: Colors.purple, height: 150.0),
              ),
              Padding(
                padding: EdgeInsets.only(left: 24.0, right: 24.0),
                child: Container(color: Colors.green, height: 150.0),
              ),
              Padding(
                padding: EdgeInsets.only(left: 24.0, right: 24.0),
                child: Container(color: Colors.orange, height: 150.0),
              ),
              Padding(
                padding: EdgeInsets.only(left: 24.0, right: 24.0),
                child: Container(color: Colors.yellow, height: 150.0),
              ),
              Padding(
                padding: EdgeInsets.only(left: 24.0, right: 24.0),
                child: Container(color: Colors.pink, height: 150.0),
              ),
              Padding(
                padding: EdgeInsets.only(left: 24.0, right: 24.0),
                child: Container(color: Colors.cyan, height: 150.0),
              ),

              Padding(
                padding: EdgeInsets.only(left: 24.0, right: 24.0),
                child: Container(color: Colors.indigo, height: 150.0),
              ),
              Padding(
                padding: EdgeInsets.only(left: 24.0, right: 24.0),
                child: Container(color: Colors.blue, height: 150.0),
              ),
            ],
          ),
        ),

      ],
    );


  }
}
4

0 回答 0