Skip to content

Commit

Permalink
feat: onpressed feature enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushcodes0 committed Apr 12, 2024
1 parent 580c666 commit 9aede7b
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions lib/dice_roller.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:math';

//hint for genrating number
final randomiser = Random();

class DiceRoller extends StatefulWidget {
const DiceRoller({super.key});
const DiceRoller({Key? key}) : super(key: key);
@override
State<DiceRoller> createState() => _DiceRollerState();
}

class _DiceRollerState extends State<DiceRoller> {
var currentDiceRoll = 5;
late int currentDiceRoll;


@override
void initState() {
super.initState();
currentDiceRoll = rollDice();
}

int rollDice() {
return randomiser.nextInt(6) + 1;
}

void roll() {
setState(() {
currentDiceRoll = rollDice();
});
}

@override
Widget build(BuildContext context) {
Expand All @@ -27,13 +42,18 @@ class _DiceRollerState extends State<DiceRoller> {
const SizedBox(
height: 30,
),
TextButton(
onPressed: (){},
child: Text('Roll'),
),


TextButton(
onPressed: roll,
child: Text('ROLL' , style: TextStyle(fontWeight: FontWeight.w500),),
),
],
);
}
}
}

/* In onpressed I am calling roll function
inside roll function I am calling rolldice function
in rolldice function I am generating random no.
And also assingning random no to currentdiceroll variable which is
changing the image no as dice$currentdiceroll. so it can change image
as random no is changing.*/

0 comments on commit 9aede7b

Please sign in to comment.