Flutter 如何延迟颤振中的列表项?
我试图在flifter中生成一个列表,它会在延迟一段时间后延迟每个项目。 我尝试使用Flutter 如何延迟颤振中的列表项?,flutter,flutter-animation,Flutter,Flutter Animation,我试图在flifter中生成一个列表,它会在延迟一段时间后延迟每个项目。 我尝试使用FutureBuilder和AnimatedList但是我没有得到它 import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; void main(){ runApp(MyApp()); } class MyApp extends StatelessWidget
FutureBuilder
和AnimatedList
但是我没有得到它
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main(){
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Example(),
);
}
}
class Example extends StatefulWidget {
@override
_ExampleState createState() => new _ExampleState();
}
class _ExampleState extends State<Example> with TickerProviderStateMixin{
AnimationController listViewController;
final Animation listView;
Duration duration = new Duration(seconds: 3);
Timer _timer;
@override
void initState() {
listViewController = new AnimationController(
duration: new Duration(seconds: 2),
vsync: this
);
super.initState();
}
FlutterLogoStyle _logoStyle = FlutterLogoStyle.markOnly;
item() {
_timer = new Timer(const Duration(seconds: 1), () {
return Text("cdscs");
});
}
@override
void dispose() {
listViewController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("hello"),
),
// ListView Builder
body: AnimatedList(
initialItemCount: 10,
itemBuilder: (BuildContext context,
int index,
Animation<double> animation){
return item();
},
)
);
}
}
导入'dart:async';
进口“包装:颤振/cupertino.dart”;
进口“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
home:Example(),
);
}
}
类示例扩展StatefulWidget{
@凌驾
_ExampleState createState()=>新建_ExampleState();
}
类_ExampleState使用TickerProviderStateMixin扩展状态{
动画控制器listViewController;
最终动画列表视图;
持续时间=新的持续时间(秒:3);
定时器(u定时器),;
@凌驾
void initState(){
listViewController=新的AnimationController(
持续时间:新的持续时间(秒数:2),
vsync:这个
);
super.initState();
}
FlatterLogoStyle _logoStyle=flatterLogoStyle.markOnly;
项目({
_计时器=新计时器(常数持续时间(秒:1),(){
返回文本(“cdscs”);
});
}
@凌驾
无效处置(){
dispose();
super.dispose();
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:新的appBar(
标题:新文本(“你好”),
),
//列表视图生成器
主体:动画列表(
initialItemCount:10,
itemBuilder:(BuildContext上下文,
整数索引,
动画(动画){
退货项目();
},
)
);
}
}
您可以为每个孩子使用AnimationController
和Animation
,如下示例:
class Example extends StatefulWidget {
@override
_ExampleState createState() => new _ExampleState();
}
class _ExampleState extends State<Example> with TickerProviderStateMixin {
AnimationController _animationController;
double animationDuration = 0.0;
int totalItems = 10;
@override
void initState() {
super.initState();
final int totalDuration = 4000;
_animationController = AnimationController(
vsync: this, duration: new Duration(milliseconds: totalDuration));
animationDuration = totalDuration/(100*(totalDuration/totalItems));
_animationController.forward();
}
FlutterLogoStyle _logoStyle = FlutterLogoStyle.markOnly;
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("hello"),
),
// ListView Builder
body: ListView.builder(
itemCount: totalItems,
itemBuilder: (BuildContext context, int index) {
return new Item(index: index, animationController: _animationController, duration: animationDuration);
},
));
}
}
class Item extends StatefulWidget {
final int index;
final AnimationController animationController;
final double duration;
Item({this.index, this.animationController, this.duration});
@override
_ItemState createState() => _ItemState();
}
class _ItemState extends State<Item> {
Animation _animation;
double start;
double end;
@override
void initState() {
super.initState();
start = (widget.duration * widget.index ).toDouble();
end = start + widget.duration;
print("START $start , end $end");
_animation = Tween<double>(
begin: 0.0,
end: 1.0,
).animate(
CurvedAnimation(
parent: widget.animationController,
curve: Interval(
start,
end,
curve: Curves.easeIn,
),
),
)..addListener((){
setState(() {
});
});
}
@override
Widget build(BuildContext context) {
return Opacity(
opacity: _animation.value,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: new Text("New Sample Item ${widget.index}"),
),
);
}
}
类示例扩展了StatefulWidget{
@凌驾
_ExampleState createState()=>新建_ExampleState();
}
类_ExampleState使用TickerProviderStateMixin扩展状态{
AnimationController _AnimationController;
双动画持续时间=0.0;
int totalItems=10;
@凌驾
void initState(){
super.initState();
最终整数总持续时间=4000;
_animationController=animationController(
vsync:this,duration:newduration(毫秒:totalDuration));
animationDuration=totalDuration/(100*(totalDuration/totalItems));
_animationController.forward();
}
FlatterLogoStyle _logoStyle=flatterLogoStyle.markOnly;
@凌驾
无效处置(){
_animationController.dispose();
super.dispose();
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:新的appBar(
标题:新文本(“你好”),
),
//列表视图生成器
正文:ListView.builder(
itemCount:totalItems,
itemBuilder:(构建上下文,int索引){
返回新项目(索引:索引,animationController:_animationController,持续时间:animationDuration);
},
));
}
}
类项扩展StatefulWidget{
最终整数指数;
最终AnimationController AnimationController;
最后双倍持续时间;
项({this.index,this.animationController,this.duration});
@凌驾
_ItemState createState()=>\u ItemState();
}
类_ItemState扩展了状态{
动画(动画),;
双启动;
双端;
@凌驾
void initState(){
super.initState();
start=(widget.duration*widget.index).toDouble();
结束=开始+小部件。持续时间;
打印(“开始$START,结束$end”);
_动画=吐温(
开始:0.0,
完:1.0,,
).制作动画(
曲线化(
父项:widget.animationController,
曲线:区间(
开始
完,,
曲线:Curves.easeIn,
),
),
)…addListener(){
设置状态(){
});
});
}
@凌驾
小部件构建(构建上下文){
返回不透明度(
不透明度:_animation.value,
孩子:填充(
填充:常数边集全部(8.0),
子项:新文本(“新示例项${widget.index}”),
),
);
}
}
您可以更改动画,在本例中,我使用了
不透明度
来模拟淡入淡出动画。也许您可以检查此响应:
for(变量i=0;i
请将您的代码整齐地格式化。请进一步解释。项目来自何处?您希望列表一个接一个地增长吗?还是希望看到完整的列表,并将它们从最初的内容更改为什么?我需要类似以下内容:
for (var i = 0; i < fetchedList.length; i++) {
future = future.then((_) {
return Future.delayed(Duration(milliseconds: 100), () {
// add/remove item
});
});