【发布时间】:2020-01-01 19:40:43
【问题描述】:
我正在尝试在我的应用中创建新闻部分。在这个将显示新闻的页面中,我希望能够单击页面上的任何位置并获取列表中的下一个新闻。到目前为止没有问题,但我希望它有一个漂亮的动画,所以我尝试实现 AnimatedSwitcher,但我不知道为什么没有动画显示。
我尝试更改代码的层次结构。将手势检测器放在动画切换器中,反之亦然。让主容器也放在外面或里面。我尝试了一个动画构建器,它可以缩放它以防万一它不够明显但没有。也尝试更改持续时间,但不是这样。
class ShowNews extends StatefulWidget {
@override
_ShowNewsState createState() => _ShowNewsState();
}
class _ShowNewsState extends State<ShowNews> {
List<News> _news = [
News(title: 'OYÉ OYÉ', desc: 'bla bla bla bla bla'),
News(title: 'another one', desc: 'plus de bout d\'histoire'),
News(title: 'boum', desc: 'attention à l\'accident'),
News(title: 'Lorem ipsum', desc: 'Lorem ipsum in doloris'),
];
int _currentIndex = 0;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
setState(() {
if (_currentIndex < _news.length - 1) {
_currentIndex++;
} else {
_currentIndex = 0;
}
});
},
child: Container(
height: 160,
padding: EdgeInsets.all(20.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.0),
topRight: Radius.circular(20.0),
),
),
child: AnimatedSwitcher(
duration: Duration(seconds: 5),
child: ColumnArticle(_news, _currentIndex),
),
),
);
}
}
除了动画,一切都很好。
编辑:我尝试添加一个键以使其不同,但仍然没有动画。
class ColumnArticle extends StatelessWidget {
final List<News> _news;
final int _currentIndex;
ColumnArticle(this._news, this._currentIndex);
@override
Widget build(BuildContext context) {
return Column(
key: ValueKey<int>(_currentIndex),
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(
_news[_currentIndex].title,
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 20.0,
),
),
SizedBox(
height: 10.0,
),
Text(
_news[_currentIndex].desc,
style: TextStyle(
fontSize: 14.0,
),
),
],
);
}
}
【问题讨论】:
-
即使没有动画,是否至少会更改到相关新闻页面?
-
是的,但没有动画。
标签: flutter dart flutter-animation