颤动-应用栏滚动,在灵活空间中重叠内容

时间:2018-07-22 20:53:06

标签: flutter flutter-layout flutter-sliver flutter-animation

我正在尝试使用Flutter重新创建在弹性空间中具有重叠内容的应用栏滚动。

此行为在此处演示:

http://karthikraj.net/2016/12/24/scrolling-behavior-for-appbars-in-android/

我已经使用SliverAppBar创建了折叠式AppBar,并使用在此处粘贴的代码尝试创建THIS

我无法使用Stack,因为我找不到任何onScroll回调,到目前为止,我已使用flexibleSpace创建了应用栏,应用栏在滚动时折叠:

Scaffold(
    body: NestedScrollView(
      headerSliverBuilder:
          (BuildContext context, bool innerBoxIsScrolled) => <Widget>[
                SliverAppBar(
                  forceElevated: innerBoxIsScrolled,
                  pinned: true,
                  expandedHeight: 180.0,
                ),
              ],
      body: ListView.builder(
        itemCount: 30,
        itemBuilder: (context, index) => Text(
              "Item $index",
              style: Theme.of(context).textTheme.display1,
            ),
      ),
    ),
  );

What i created so far

编辑:Example of what i want to create

3 个答案:

答案 0 :(得分:1)

ScrollViews take a ScrollController which is a Listenable that notifies on scroll offset updates.

You can listen to the ScrollController and use a Stack to achieve the effect you're interested in based on the scroll offset.

Here's a quick example:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Scroll demo',
      home: new Scaffold(
        appBar: new AppBar(elevation: 0.0),
        body: new CustomScroll(),
      ),
    );
  }
}

class CustomScroll extends StatefulWidget {
  @override
  State createState() => new CustomScrollState();
}

class CustomScrollState extends State<CustomScroll> {
  ScrollController scrollController;
  double offset = 0.0;
  static const double kEffectHeight = 100.0;

  @override
  Widget build(BuildContext context) {
    return new Stack(
      alignment: AlignmentDirectional.topCenter,
      children: <Widget> [
        new Container(
          color: Colors.blue,
          height: (kEffectHeight - offset * 0.5).clamp(0.0, kEffectHeight),
        ),
        new Positioned(
          child: new Container(
            width: 200.0,
            child: new ListView.builder(
              itemCount: 100,
              itemBuilder: buildListItem,
              controller: scrollController,
            ),
          ),
        ),
      ],
    );
  }

  Widget buildListItem(BuildContext context, int index) {
    return new Container(
      color: Colors.white,
      child: new Text('Item $index')
    );
  }

  void updateOffset() {
    setState(() {
      offset = scrollController.offset;
    }); 
  }

  @override
  void initState() {
    super.initState();
    scrollController = new ScrollController();
    scrollController.addListener(updateOffset);
  }

  @override
  void dispose() {
    super.dispose();
    scrollController.removeListener(updateOffset);
  }
}

答案 1 :(得分:0)

我认为您正在寻找SliverAppbar小部件。

看一下这篇文章,向您展示如何实现自己的目标。

https://flutterdoc.com/animating-app-bars-in-flutter-cf034cd6c68b

答案 2 :(得分:0)