flutter - 在灵活空间中使用重叠内容滚动应用栏 [英] flutter - App bar scrolling with overlapping content in Flexible space

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

问题描述

我正在尝试使用颤振在灵活空间中重新创建具有重叠内容的应用栏滚动.

这里演示了行为:

我想要创建的示例

解决方案

ScrollViews 采用一个 ScrollController,它是一个可通知滚动偏移更新的 Listenable.

你可以监听ScrollController,使用Stack根据滚动偏移量达到你感兴趣的效果.

这是一个简单的例子:

import 'package:flutter/material.dart';无效的主要()=>运行应用程序(新的我的应用程序());MyApp 类扩展 StatelessWidget {@覆盖小部件构建(BuildContext 上下文){返回新的 MaterialApp(标题:'滚动演示',家:新脚手架(appBar:新的AppBar(海拔:0.0),正文:新的自定义滚动(),),);}}类 CustomScroll 扩展 StatefulWidget {@覆盖状态 createState() =>新的 CustomScrollState();}类 CustomScrollState 扩展了 State{滚动控制器滚动控制器;双偏移 = 0.0;静态常量双倍 kEffectHeight = 100.0;@覆盖小部件构建(BuildContext 上下文){返回新堆栈(对齐:AlignmentDirectional.topCenter,孩子:<小部件>[新容器(颜色:颜色.蓝色,高度:(kEffectHeight - 偏移 * 0.5).clamp(0.0, kEffectHeight),),新定位(孩子:新容器(宽度:200.0,孩子:新的ListView.builder(项目数:100,itemBuilder: buildListItem,控制器:滚动控制器,),),),],);}小部件 buildListItem(BuildContext context, int index) {返回新容器(颜色:颜色.白色,孩子:新文本('项目$索引'));}无效更新偏移(){设置状态((){偏移量 = 滚动控制器.offset;});}@覆盖无效初始化状态(){super.initState();滚动控制器 = 新滚动控制器();scrollController.addListener(updateOffset);}@覆盖无效处置(){super.dispose();scrollController.removeListener(updateOffset);}}

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

the behavior is demonstrated here:

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

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

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

edit: Example of what i want to create

解决方案

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆