본문 바로가기
Flutter/- Useful Code

image 시간에 맞게 경로 변경해주기

by 고고 뜌지 2021. 12. 5.

고고뜌지입니다.

타이머를 사용해서 시간당 image 경로 변경해서 UI화면에 보여주는 방법입니다.

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:overflown/Model/Image.dart';

//intro image change 변수선언
int routeNo = 1;
ValueNotifier<String> imgRoute = ValueNotifier<String>('');
Timer _timer;

class Easy extends StatefulWidget{
  @override
  State<StatefulWidget> createState() => Intro();
}

class Intro extends State<Easy>{
  Widget build(BuildContext context) {
    Intro_Image_Route(); //intro image route set 하는 함수
    return Scaffold(
      appBar: AppBar(
          backgroundColor: Colors.deepOrangeAccent,
          automaticallyImplyLeading: false
      ),
      backgroundColor: Colors.white,
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ValueListenableBuilder(
              valueListenable: imgRoute,
              builder: (BuildContext context, String value, Widget child) {
                return Image(
                    image: AssetImage(imgRoute.value)
                );
              },
            )
          ]
        ),
      );
  }

  Intro_Image_Route(){
    ImageRoute Route = new ImageRoute(routeNo);
    imgRoute.value = Route.ImageRouteGo();

    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      routeNo++;
      ImageRoute Route = new ImageRoute(routeNo);
      imgRoute.value = Route.ImageRouteGo();

      if(routeNo == 21){
        _timer?.cancel();
      }
    });
  }
}

 

댓글