"in this article you will learn best thing some important like if you want touch or icon touch or button tap in any android application build through the flutter language or technology so you will follow these point in this article."
First thing in the flutter you create a project and learn some basic concept:-
Gesture Detector
Random Number Generation
Switch Case Statement Understanding
Assets Uses
One thing is about gesture Detection if you have any widget touch any app the performs an action called gesture detection.
Gesture Detection Contains Some Property like that:-
A Child that contains all type of Widget and more & more operation/function/method
like that:- onTap(), OnLongPress(),ontapDown(),onTopUp() etc.
so here use the this code for change the image value or Image Change.
GestureDetector(
child: Image(
image: diceimage,
width: 100,
height: 100,
),
onTap: () => diceroller(),
),
And the main/changedice Method is Describe here this use switch case statement:-
voiddiceroller() {
AssetImage newimage;
var randomnum = (1 + Random().nextInt(6));
switch (randomnum) {
case1:
newimage = one;
break;
case2:
newimage = two;
break;
case3:
newimage = three;
break;
case4:
newimage = four;
break;
case5:
newimage = five;
break;
case6:
newimage = six;
break;
}
setState(() {
diceimage = newimage;
});
}
Random Number Generation Method:-
use a library that is math
import'dart:math';
and Random Number method:-
var randomnum = (1 + Random().nextInt(6));
Random Number Return Value min Zero(0) to max 5 So that reason use 1+
How to access predefined images and other data like that:
assets:
- images/one.png
- images/two.png
- images/three.png
- images/four.png
- images/five.png
- images/six.png
AssetImage one = newAssetImage("images/one.png");
AssetImage two = newAssetImage("images/two.png");
AssetImage three = newAssetImage("images/three.png");
AssetImage four = newAssetImage("images/four.png");
AssetImage five = newAssetImage("images/five.png");
AssetImage six = newAssetImage("images/six.png");
AssetImage diceimage;
voidinitState() {
super.initState();
setState(() {
diceimage = two;
});
}
Final Code Here:-
import'package:flutter/material.dart';
import'dart:math';
voidmain() => runApp(MyApp());
classMyAppextendsStatelessWidget {
@override
Widgetbuild(BuildContext context) {
returnMaterialApp(
home: HomePage(),
debugShowCheckedModeBanner: false,
);
}
}
classHomePageextendsStatefulWidget {
@override
_HomePageStatecreateState() => _HomePageState();
}
class_HomePageStateextendsState<HomePage> {
AssetImage one = newAssetImage("images/one.png");
AssetImage two = newAssetImage("images/two.png");
AssetImage three = newAssetImage("images/three.png");
AssetImage four = newAssetImage("images/four.png");
AssetImage five = newAssetImage("images/five.png");
AssetImage six = newAssetImage("images/six.png");
AssetImage diceimage;
voidinitState() {
super.initState();
setState(() {
diceimage = two;
});
}
voiddiceroller() {
AssetImage newimage;
var randomnum = (1 + Random().nextInt(6));
switch (randomnum) {
case1:
newimage = one;
break;
case2:
newimage = two;
break;
case3:
newimage = three;
break;
case4:
newimage = four;
break;
case5:
newimage = five;
break;
case6:
newimage = six;
break;
}
setState(() {
diceimage = newimage;
});
}
@override
Widgetbuild(BuildContext context) {
returnScaffold(
appBar: AppBar(
title: Text("Dice Roller"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GestureDetector(
child: Image(
image: diceimage,
width: 100,
height: 100,
),
onTap: () => diceroller(),
),
//https://programmerhubs.in
],
),
),
);
}
}
Video lecture on that application so you can follow or watch it.
1 Comments
Club of Developers Blog
ReplyDeletethanks for your suggestion and improving quality of the content