declear a variable and use the math library

This commit is contained in:
Mukaila Semiu 2020-05-19 11:23:18 +01:00
parent 3934cca572
commit e7f29a8d94

View File

@ -1,3 +1,4 @@
import 'dart:math';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
void main() { void main() {
@ -15,29 +16,48 @@ void main() {
); );
} }
class DicePage extends StatelessWidget {
class DicePage extends StatefulWidget {
@override @override
Widget build(BuildContext context) { _DicePageState createState() => _DicePageState();
}
class _DicePageState extends State<DicePage> {
int leftDiceNumber = 1;
int rightDiceNumber = 1;
void changeDice() {
setState(() {
leftDiceNumber = Random().nextInt(6)+ 1;
rightDiceNumber = Random().nextInt(6) + 1;
});
}
@override
Widget build(BuildContext context) {
return Center( return Center(
child: Row( child: Row(
children: <Widget> [ children: <Widget> [
Expanded( Expanded(
child: FlatButton( child: FlatButton(
onPressed: () { onPressed: () {
print('left button is pressed'); changeDice();
}, },
child: Image.asset('images/dice1.png')), child: Image.asset('images/dice$leftDiceNumber.png'),
),
), ),
Expanded( Expanded(
child: FlatButton( child: FlatButton(
onPressed: () { onPressed: () {
print('right button is pressed'); changeDice();
}, },
child: Image.asset('images/dice1.png')), child: Image.asset('images/dice$rightDiceNumber.png')),
), ),
], ],
), ),
); );
} }
} }