我想在CustomScrollView中切换动画的状态,但它抛出错误。
class SliverAnimatedSwitcher extends StatefulWidget {
final state;
const SliverAnimatedSwitcher({Key key, this.state}) : super(key: key);
@override
_SliverAnimatedSwitcherState createState() => _SliverAnimatedSwitcherState();
}
class _SliverAnimatedSwitcherState extends State<SliverAnimatedSwitcher> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
title: Text('SliverAnimatedSwitcher'),
),
_buildContent(),
],
),
);
}
get state => widget.state;
Widget _buildContent() {
var content;
if (state.isNotEmpty == true) {
content = SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
var item = state.items[index];
return ListTile(
title: Text(item.title),
);
},
childCount: state.items.length,
),
);
} else if (state.isError) {
content = SliverFillRemaining(
key: Key('error'),
child: Container(alignment: Alignment.center, child: Text('Error')),
);
} else if (state.isLoading) {
content = SliverFillRemaining(
key: Key('loading'),
child: Container(alignment: Alignment.center, child: Text('Loading')),
);
} else {
content = SliverFillRemaining(
key: Key('empty'),
child: Container(alignment: Alignment.center, child: Text('Empty')),
);
}
return AnimatedSwitcher(
duration: Duration(milliseconds: 300),
child: content,
);
}
}发布于 2021-02-21 03:52:23
在pub.dev上有一个包含SliverAnimatedSwitcher的sliver_tools包。你可以这样使用它:
SliverAnimatedSwitcher(
duration: kThemeAnimationDuration,
child: content,
)发布于 2020-10-19 21:58:07
这是因为sliver小部件不支持在AnimatedSwitcher中用作布局构建器的Stack。
我找到了一个临时的解决方案。虽然结果不等于原始的结果(只显示结果中的最后一个小部件),但我认为现在我可以接受它,只需要这么少的努力。
为了将(defaultLayoutBuilder)
返回给
SliverAnimatedSwitcher hereStack part的代码完全相同的currentChild SliverAnimatedSwitcher。
static Widget defaultLayoutBuilder(Widget currentChild, List<Widget> previousChildren) {
return currentChild;
}。
static Widget defaultTransitionBuilder(Widget child, Animation<double> animation) {
return SliverFadeTransition(
opacity: animation,
sliver: child,
);
}如果有人能找到一种方法将Sliver小部件堆叠在彼此之上,结果可能会更完美。
发布于 2020-08-08 04:38:35
我找到了一种方法,可以用FadeTransition代替AnimatedSwitcher。
试试这个:
SliverAnimatedOpacity(
opacity: yourCondition ? 1.0 : 0.0,
duration: Duration(seconds: 1),
sliver: yourCondition
? SliverGrid()
: SliverToBoxAdapter(
child: Center(
child: CircularProgressIndicator(),
),
));更多信息请点击此处:https://api.flutter.dev/flutter/widgets/SliverAnimatedOpacity-class.html
https://stackoverflow.com/questions/58551756
复制相似问题