Random Image changer Using Random() in Flutter
Code =====
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: MyApp(),
),
);
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List l1 = [
"assets/images/anim1.jpg",
"assets/images/anim2.jpg",
"assets/images/anim3.jpg",
"assets/images/anim4.jpg",
"assets/images/anim5.jpg",
"assets/images/anim6.jpg"
];
int i = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text("Home Screen"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
InkWell(
onTap: () {
Random random = Random();
int no = random.nextInt(6);
setState(() {
i = no;
});
},
child: Container(
height: 100,
width: 100,
color: Colors.blue,
child: Image.asset("${l1[i]}"),
),
),
SizedBox(
height: 50,
),
],
),
),
);
}
}
0 Comments