gpt4 book ai didi

dart - Flutter tween 基本动画在 `FutureBuilder` 内部不起作用

转载 作者:IT王子 更新时间:2023-10-29 07:05:35 24 4
gpt4 key购买 nike

Flutter tween 基本动画在 FutureBuilder 中不起作用

我已经使用 gridview.builder 创建了一个图片库页面,但出于某种原因,动画在 futurebuilder 中不起作用。我直接在 body 容器内的静态图像上尝试了相同的动画,它工作得非常好。需要一些方法来为 futurebuilder 中的网络图像设置动画。

class _GalleryGridState extends State<GalleryGrid>
with TickerProviderStateMixin {
AnimationController _controller;
Animation _squeezeOutAnimation, transformationAnim;

List<GalleryModel> lists = List();

Future<List<GalleryModel>> fetchPost() async {
final response =
await http.get("https://jsonplaceholder.typicode.com/photos");
if (response.statusCode == 200) {
var datas = json.decode(response.body);
lists = (datas as List)
.map((data) => new GalleryModel.fromJson(data))
.toList();
return lists;
} else {
throw Exception("Failed to load photos");
}
}

@override
void initState() {
super.initState();
_controller =
AnimationController(duration: Duration(seconds: 3), vsync: this);
transformationAnim = BorderRadiusTween(
begin: BorderRadius.circular(150.0),
end: BorderRadius.circular(0.0))
.animate(CurvedAnimation(parent: _controller, curve: Curves.ease));
_squeezeOutAnimation = Tween<double>(begin: 150.0, end: 1000.0)
.animate(CurvedAnimation(parent: _controller, curve: Curves.ease));
}

@override
void dispose() {
super.dispose();
_controller.dispose();
}

@override
Widget build(BuildContext context) {
return FutureBuilder(
future: fetchPost(),
builder: (context, data) {

switch (data.connectionState) {
case ConnectionState.waiting:
return Center(child: CircularProgressIndicator());
default:
return GridView.builder(
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
itemCount: lists.length,
itemBuilder: (BuildContext context, int index) {
return Stack(
children: <Widget>[
Container(
child: Card(
shape: BeveledRectangleBorder(
borderRadius: transformationAnim.value),
elevation: 10.0,
child: GestureDetector(
onTap: () {
_controller.forward();
},
child: Container(
width: _squeezeOutAnimation.value,
height: _squeezeOutAnimation.value,
child: Image.network(
lists[index].thumbnailUrl,
fit: BoxFit.fill,
width: _squeezeOutAnimation.value,
),
),
),
),
),
])

最佳答案

动画正在运行,但您的构建小部件未更新,即重建

尝试此代码,如果它按您预期的那样工作,请告诉我。

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'dart:convert';


void main() => runApp(GalleryGrid());

class GalleryGrid extends StatefulWidget {
@override
_GalleryGridState createState() => _GalleryGridState();
}

class GalleryModel {
final String thumbnailUrl;

GalleryModel(this.thumbnailUrl);

factory GalleryModel.fromJson(Map<String, dynamic> data) {
return GalleryModel(data['thumbnailUrl']);
}
}

class _GalleryGridState extends State<GalleryGrid>
{


List<GalleryModel> lists = List();

Future<List<GalleryModel>> fetchPost() async {
final response =
await http.get("https://jsonplaceholder.typicode.com/photos");
if (response.statusCode == 200) {
var datas = json.decode(response.body);
lists = (datas as List)
.map((data) => new GalleryModel.fromJson(data))
.toList();
return lists;
} else {
throw Exception("Failed to load photos");
}
}



@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: FutureBuilder(
future: fetchPost(),
builder: (context, data) {
switch (data.connectionState) {
case ConnectionState.waiting:
return Center(child: CircularProgressIndicator());
default:
return new GridWidget(lists: lists);
}
},
),
),
);
//),
//);
}
}

class GridWidget extends StatefulWidget {
const GridWidget({
Key key,
@required this.lists,

}) ;

final List<GalleryModel> lists;


@override
GridWidgetState createState() {
return new GridWidgetState();
}
}

class GridWidgetState extends State<GridWidget> with TickerProviderStateMixin{
AnimationController _controller;
Animation _squeezeOutAnimation, transformationAnim;
@override
void initState() {
super.initState();
_controller =
AnimationController(duration: Duration(seconds: 3), vsync: this);
transformationAnim = BorderRadiusTween(
begin: BorderRadius.circular(150.0),
end: BorderRadius.circular(0.0))
.animate(CurvedAnimation(parent: _controller, curve: Curves.ease))
..addListener(() {
setState(() {});
});
_squeezeOutAnimation = Tween<double>(begin: 150.0, end: 1000.0)
.animate(CurvedAnimation(parent: _controller, curve: Curves.ease));
}

@override
void dispose() {
super.dispose();
_controller.dispose();
}
@override
Widget build(BuildContext context) {
return GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2),
itemCount: widget.lists.length,
itemBuilder: (BuildContext context, int index) {
return Stack(children: <Widget>[
Container(
child: Card(
shape: BeveledRectangleBorder(
borderRadius: transformationAnim.value),
elevation: 10.0,
child: GestureDetector(
onTap: () {
print('Card $index is pressed');
_controller.reset();
_controller.forward();
},
child: Container(
width: _squeezeOutAnimation.value,
height: _squeezeOutAnimation.value,
child: Image.network(
widget.lists[index].thumbnailUrl,
fit: BoxFit.fill,
width: _squeezeOutAnimation.value,
),
),
),
),
),
]);
},
);
}
}

关于dart - Flutter tween 基本动画在 `FutureBuilder` 内部不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53532792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com