A simple dice rolling app.
This commit is contained in:
71
lib/main.dart
Normal file
71
lib/main.dart
Normal file
@@ -0,0 +1,71 @@
|
||||
import 'dart:math';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() {
|
||||
return runApp(
|
||||
MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
backgroundColor: Colors.red,
|
||||
appBar: AppBar(title: Text('Dicee'), backgroundColor: Colors.red),
|
||||
body: DicePage(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class DicePage extends StatefulWidget {
|
||||
const DicePage({super.key});
|
||||
|
||||
@override
|
||||
State<DicePage> createState() => _DicePageState();
|
||||
}
|
||||
|
||||
class _DicePageState extends State<DicePage> {
|
||||
int leftDie = 0;
|
||||
int rightDie = 0;
|
||||
|
||||
int diceRoll() {
|
||||
return Random().nextInt(6) + 1;
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
leftDie = diceRoll();
|
||||
rightDie = diceRoll();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
margin: EdgeInsets.all(16.0),
|
||||
child: Center(
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
leftDie = diceRoll();
|
||||
});
|
||||
},
|
||||
child: Image.asset('assets/dice$leftDie.png'),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: TextButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
rightDie = diceRoll();
|
||||
});
|
||||
},
|
||||
child: Image.asset('assets/dice$rightDie.png'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user