如何在Flutter中的NestedScrollView中设置SliverAppBar的最小高度

时间:2019-02-01 08:39:11

标签: user-interface dart flutter

我想在普通的Scaffold应用程序的NestedScrollView中使用SliverAppBar。当用户向下滚动时,我还希望在应用栏上设置一些最小高度。

我不知道如何使用PreferredSize小部件或在线找到的任何其他解决方案,例如this

这是我当前的简化解决方案:

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, fontFamily: 'Oswald'),
      home: SliverHome(),
    );
  }
}

class SliverHome extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: testHome(),
    );
  }

  Widget testHome() {
    return Scaffold(
      body: NestedScrollView(
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return <Widget>[
            SliverAppBar( // this is where I would like to set some minimum constraint
              expandedHeight: 300,
              floating: false,
              pinned: true,
              flexibleSpace: Container(
                padding: EdgeInsets.all(10),
                height: 340,
                width: double.infinity,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.end,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Container(
                      height: 40,
                    ),
                    Container(
                      height: 60,
                    ),
                    Expanded(child: Container()),
                    Text('TEST'),
                  ],
                ),
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: NetworkImage('https://picsum.photos/400/400'),
                        fit: BoxFit.cover)),
              ),
            )
          ];
        },
        body: Container(),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => {},
        tooltip: '+',
        child: Icon(Icons.accessibility_new),
      ),
    );
  }
}

我想停止缩小60 dp附近的应用栏

screenshot

4 个答案:

答案 0 :(得分:3)

我不知道确切的时间,但是到目前为止,您还可以使用塌陷的高度属性,如下所示:

https://api.flutter.dev/flutter/material/SliverAppBar/collapsedHeight.html

当前,我的版本为1.20.1,并且此属性可用。

答案 1 :(得分:2)

这实际上是请求的功能-https://github.com/flutter/flutter/issues/21298

一种解决方法是使用Bottom

SliverAppBar(
              // this is where I would like to set some minimum constraint
              expandedHeight: 300,
              floating: false,
              pinned: true,
              bottom: PreferredSize(                       // Add this code
                preferredSize: Size.fromHeight(60.0),      // Add this code
                child: Text(''),                           // Add this code
              ),                                           // Add this code
              flexibleSpace: Container(
                padding: EdgeInsets.all(10),
                height: 340,
                width: double.infinity,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.end,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Container(
                      height: 40,
                    ),
                    Container(
                      height: 60,
                    ),
                    Expanded(child: Container()),
                    Text('TEST'),
                  ],
                ),
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: NetworkImage('https://picsum.photos/400/400'),
                        fit: BoxFit.cover)),
              ),
            )

答案 2 :(得分:1)

减小容器的大小后,您的代码也可以正常工作。

 SliverAppBar( // this is where I would like to set some minimum constraint
          expandedHeight: 300,
          floating: false,
          pinned: true,
          flexibleSpace: Container(
            padding: EdgeInsets.all(10),
            height: 340,
            width: double.infinity,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.end,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Container(
                  height: 10,  //decreas the size
                ),
                Container(
                  height: 10, //decrease the size
                ),
                Expanded(child: Container()),
                Text('TEST'),
              ],
            ),
            decoration: BoxDecoration(
                image: DecorationImage(
                    image: NetworkImage('https://picsum.photos/400/400'),
                    fit: BoxFit.cover)),
          ),
        )

答案 3 :(得分:0)

只需使用 collapsedHeight 并指定您的最小高度。

 SliverAppBar(
  // Display a placeholder widget to visualize the shrinking size.
  flexibleSpace: Placeholder(),
  expandedHeight: 300,
  collapsedHeight: 60,
);