declear a variable and use the math library
This commit is contained in:
parent
3934cca572
commit
e7f29a8d94
@ -1,3 +1,4 @@
|
||||
import 'dart:math';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() {
|
||||
@ -15,29 +16,48 @@ void main() {
|
||||
);
|
||||
}
|
||||
|
||||
class DicePage extends StatelessWidget {
|
||||
|
||||
class DicePage extends StatefulWidget {
|
||||
@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(
|
||||
child: Row(
|
||||
children: <Widget> [
|
||||
Expanded(
|
||||
child: FlatButton(
|
||||
onPressed: () {
|
||||
print('left button is pressed');
|
||||
changeDice();
|
||||
},
|
||||
child: Image.asset('images/dice1.png')),
|
||||
child: Image.asset('images/dice$leftDiceNumber.png'),
|
||||
),
|
||||
),
|
||||
|
||||
Expanded(
|
||||
child: FlatButton(
|
||||
onPressed: () {
|
||||
print('right button is pressed');
|
||||
changeDice();
|
||||
},
|
||||
child: Image.asset('images/dice1.png')),
|
||||
child: Image.asset('images/dice$rightDiceNumber.png')),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user