本文介绍了颤动-应用栏滚动,在灵活空间中重叠内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i am trying to recreate App bar scrolling with overlapping content in Flexible space using flutter.

此行为在此处演示:

http://karthikraj.net/2016/12/24/在Android/scrolling-behavior-for-appbars/

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

I created collapsing AppBar using SliverAppBar already, using the code I pasted here, I am trying to create THIS

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

i cant use Stack for it because i cant find any onScroll callback, so far i created appbar with flexibleSpace, the app bar collapse on scroll:

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,
            ),
      ),
    ),
  );

我要创建的示例

推荐答案

ScrollViews带有一个ScrollController,它是一个可侦听的控件,用于在滚动偏移量更新时进行通知.

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

您可以听ScrollController并使用Stack基于滚动偏移量来实现您感兴趣的效果.

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

这是一个简单的例子:

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);
  }
}

这篇关于颤动-应用栏滚动,在灵活空间中重叠内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 17:39