gpt4 book ai didi

flutter - 为什么画面在每个时间段都没有变化?

转载 作者:行者123 更新时间:2023-12-03 04:18:11 25 4
gpt4 key购买 nike

我希望我的 float 网页显示一张照片,并每3秒更改一次以显示另一张照片。
这是我的代码

class _contactPageState extends State<contactPage> {

List<String> randomPics= ['profiles/github.png', 'profiles/linkedIn.png', 'profiles/stack.png'];
Timer timer;
var random= Random();
String photo;

@override
void initState() {
photo = randomPics[random.nextInt(randomPics.length)];
openLink = new linksToOpen();
super.initState();
timer = new Timer(new Duration(seconds: 1), ( ){
setState(() {
photo= randomPics[random.nextInt(randomPics.length)];
print('${photo}');
});
});
}
@override
Widget build(BuildContext context) {
Container(

child: Image(image : AssetImage(photo),gaplessPlayback: true,),
width: 400, height: 400,

),
我的代码有什么问题?
拜托,谁能帮我!

最佳答案

我做了一些测试,我认为这可能对您有用:

import 'dart:async';
import 'dart:math';

import 'package:flutter/widgets.dart';

class ContactPageState extends StatefulWidget {
ContactPageState({Key key}) : super(key: key);

@override
_ContactPageState createState() => _ContactPageState();
}

class _ContactPageState extends State<ContactPageState> {
List<String> randomPics = ['assets/a.jpg', 'assets/b.jpg', 'assets/c.jpg'];
String photo;
final random = new Random();

void initState() {
WidgetsBinding.instance.addPostFrameCallback((_) => configureTimer());
super.initState();
}

configureTimer() {
Timer.periodic(Duration(milliseconds: 3000), (timer) {
final int index = random.nextInt(randomPics.length);
setState(() {
photo = randomPics[index];
debugPrint('Select picture $photo in index $index');
});
});
}

@override
Widget build(BuildContext context) {
return Container(
child: Image(
image: AssetImage(photo != null ? photo : 'assets/a.jpg'),
gaplessPlayback: true,
),
width: 400,
height: 400,
);
}
}

关于flutter - 为什么画面在每个时间段都没有变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64199033/

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