Last active
February 22, 2024 15:10
-
-
Save gildaswise/c1332cc136c96fa5980c3e05c7faeb4b to your computer and use it in GitHub Desktop.
Quick transition between list of widgets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | |
// for details. All rights reserved. Use of this source code is governed by a | |
// BSD-style license that can be found in the LICENSE file. | |
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key, required this.title}); | |
final String title; | |
@override | |
MyHomePageState createState() => MyHomePageState(); | |
} | |
const List<IconData> icons = [ | |
Icons.brightness_1, | |
Icons.brightness_2, | |
Icons.brightness_3 | |
]; | |
class MyHomePageState extends State<MyHomePage> { | |
int _currentIndex = 0; | |
Timer? _timer; | |
@override | |
void initState() { | |
super.initState(); | |
_timer = Timer.periodic(const Duration(seconds: 1), (timer) async { | |
if (mounted) { | |
setState(() { | |
if (_currentIndex + 1 == icons.length) { | |
_currentIndex = 0; | |
} else { | |
_currentIndex = _currentIndex + 1; | |
} | |
}); | |
} | |
}); | |
} | |
@override | |
void dispose() { | |
_timer?.cancel(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(), | |
body: Center( | |
child: AnimatedSwitcher( | |
duration: const Duration(milliseconds: 500), | |
child: Icon( | |
icons[_currentIndex], | |
key: ValueKey<int>(_currentIndex), | |
size: 96, | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment