import 'dart:async';
import 'package:flutter/material.dart';
class homeScreen extends StatefulWidget {
const homeScreen({Key? key}) : super(key: key);
State<homeScreen> createState() => _homeScreenState();
}
class _homeScreenState extends State<homeScreen> {
Timer? timer;
PageController controller = PageController(initialPage: 0);
void initState() {
// TODO: implement initState
super.initState();
timer = Timer.periodic(
Duration(seconds: 3),
(timer) {
int currentPage = controller.page!.toInt();
int nextPage = currentPage + 1;
if (nextPage > 4) {
nextPage = 0;
}
controller.animateToPage(
nextPage,
duration: Duration(milliseconds: 400),
curve: Curves.linear,
); // controller.animateToPage
}, // timer
); // Timer.peridoic
} // initState
void dispose() {
// TODO: implement dispose
controller.dispose();
if (timer != null) {
timer!.cancel();
}
super.dispose();
} // dispose
Widget build(BuildContext context) {
return Scaffold(
body: PageView(
controller: controller,
children: [1, 2, 3, 4, 5]
.map((e) => Image.asset(
'asset/img/image_$e.jpeg',
fit: BoxFit.cover,
))
.toList(),
), // PageView
); // Scaffold
} // build
}
Dart
복사
