flutter - 如何一起使用 AnimatedSwitcher 和 CustomScrollView
问题描述
我想用动画切换状态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,
);
}
}
解决方案
pub.dev 上有一个sliver_tools包,它有一个SliverAnimatedSwitcher。你会这样使用它:
SliverAnimatedSwitcher(
duration: kThemeAnimationDuration,
child: content,
)
推荐阅读
- javascript - 父组件传递新道具后,子组件未更新
- google-sheets - 下拉选择使用来自另一个选项卡的数据填充工作表
- windows - 解压多个文件到不同目录
- sql - 检索日期超过 6 个月的记录并更新同一表中的列
- excel - VBA,将前一列存储为变量时使用
- javascript - 有没有办法使用 Puppeteer 在站点中搜索多个 ID 并检查它们是否为空?
- php - 如何在没有插件的情况下为我的 wordpress 主题创建加载更多分页
- javascript - 节点中的正文解析器
- jenkins - 变量未按预期在 Jenkins 管道阶段之间的 shell 命令之间共享
- mysql - Flask-SQLAlchemy 错误 MySQL 服务器已消失