dart - 防止滚动的英雄跳到应用栏顶部

标签 dart flutter

我有一个可以滚动的Hero,它的一部分在屏幕外。当我触发转换时,它似乎突然跳到 AppBar 的顶部,然后在转换反转时跳回到它下面。如何强制 AppBar 停留在 Hero 之上?

video

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: new Example(),
    theme: new ThemeData(primaryColor: Colors.orange),
    debugShowCheckedModeBanner: false,
  ));
}

Widget positionHeroOverlay(BuildContext context, Widget overlay, Rect rect, Size size) {
  final RelativeRect offsets = new RelativeRect.fromSize(rect, size);
  return new Positioned(
    top: offsets.top,
    right: offsets.right,
    bottom: offsets.bottom,
    left: offsets.left,
    child: overlay,
  );
}

class LogoPageRoute extends MaterialPageRoute<Null> {
  LogoPageRoute(this.colors) : super(
    builder: (BuildContext context) {
      return new Scaffold(
        appBar: new AppBar(
          title: new Text('Flutter Logo'),
        ),
        body: new ConstrainedBox(
          constraints: new BoxConstraints.expand(),
          child: new Hero(
            tag: colors,
            child: new FlutterLogo(colors: colors),
          ),
        ),
      );
    },
  );

  /// The color of logo to display
  final MaterialColor colors;

  @override
  final Duration transitionDuration = const Duration(seconds: 1);
}

final List<MaterialColor> swatches = [
  Colors.blue,
  Colors.orange,
  Colors.green,
];

class Example extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('All Logos'),
      ),
      body: new ListView(
        children: swatches.map((MaterialColor colors) {
          return new InkWell(
            onTap: () {
              Navigator.push(context, new LogoPageRoute(colors));
            },
            child: new Hero(
              tag: colors,
              child: new FlutterLogo(size: 360.0, colors: colors),
            ),
          );
        }).toList(),
      ),
    );
  }
}

最佳答案

将您的 AppBar 包装在 Hero 中以强制它保持在顶部。

  appBar: new PreferredSize(
    child: new Hero(
      tag: AppBar,
      child: new AppBar(
        title: new Text('All Logos'),
      ),
    ),
    preferredSize: new AppBar().preferredSize,
  ),

对于详情页的AppBar,我建议强制显示后退按钮,使其在页面标题更改的同时出现。

    appBar: new PreferredSize(
      child: new Hero(
        tag: AppBar,
        child: new AppBar(
          leading: const BackButton(),
          title: new Text('Flutter Logo'),
        ),
      ),
      preferredSize: new AppBar().preferredSize,
    ),

这是它的样子:

video

关于dart - 防止滚动的英雄跳到应用栏顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45888236/

相关文章:

dart - 选择时如何更改BottomNavigationBarItem图标, flutter

flutter - 如何在 Flutter 中启用相机闪光灯?

flutter - 如何获取我点击的图像像素的颜色?

android - geolocator 和 onesignal_flutter 插件之间的不兼容导致应用程序意外终止

class - 在 Dart 中不继承静态变量的基本原理是什么?

dart - 在 Dart 中创建扩展方法

Flutter:我在运行时遇到了这个问题:flutter pub run build_runner watch

list - 我怎样才能找到一个列表包含在任何元素中的另一个列表?

dart - Flutter - 更改 OutlineInputBorder 的边框颜色

flutter - 文本分隔符上的千位分隔符不带小数点分隔符