我在使用SliverAppBar
时遇到问题。
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Demo Home Page'),
),
body: Center(
child: Row(
children: [Colors.red, Colors.blue, Colors.yellow]
.map((e) => GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => ScrollPage(color: e),
));
},
child: Hero(
tag: e,
child: Container(
width: 100,
height: 100,
color: e,
),
),
))
.toList(),
),
),
);
}
}
import 'package:flutter/material.dart';
class ScrollPage extends StatefulWidget {
const ScrollPage({super.key, required this.color});
final Color color;
@override
State<ScrollPage> createState() => _ScrollPageState();
}
class _ScrollPageState extends State<ScrollPage> {
// final GlobalKey key = GlobalKey<SliverState>();
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: [
Hero(
tag: widget.color,
child: SliverAppBar.large(
key: UniqueKey(),
expandedHeight: 200,
backgroundColor: widget.color,
title: const Text(
'This is a title blblaa',
),
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => ListTile(
title: Text('Item $index'),
),
childCount: 100,
),
),
],
),
);
}
}
我收到如下错误:
════════ Exception caught by widgets library ═══════════════════════════════════
'package:flutter/src/widgets/framework.dart': Failed assertion: line 6405 pos 12: 'renderObject.child == child': is not true.
以及
════════ Exception caught by widgets library ═══════════════════════════════════
'package:flutter/src/widgets/framework.dart': Failed assertion: line 6369 pos 12: 'child == _child': is not true.
framework.dart:6369
The relevant error-causing widget was
MaterialApp
有时候:
════════ Exception caught by widgets library ═══════════════════════════════════
Duplicate GlobalKey detected in widget tree.
════════════════════════════════════════════════════════════════════════════════
我做了类似的不使用Appbar,我实现了它。演示视频here。但是,我真的想在这种情况下使用SliverAppbar。
完整的最小可复制项目:https://github.com/iqfareez/flutter_hero_sliver
如何让SliverAppBar与Hero一起工作?
2条答案
按热度按时间voase2hg1#
您可以尝试将Hero小工具移出SliverAppBar,将其环绕在整个CustomScrollView周围。以下是您可以修改代码以实现此目的的方法:
uyhoqukh2#
这里的问题是
Hero
是一个通用的小部件,而不是一个小部件,这是在用Hero
小部件 PackageSliverAppBar
时出现的问题。你能做到
此外,您可以将
Scaffold
Package 为Hero小部件,但它将显示略有不同的动画。您可以创建
SliverPersistentHeaderDelegate
。检查pr并提交差异。