플러터(Flutter)
플러터 이미지 주사위
xemaker
2024. 10. 24. 15:41
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.red),
useMaterial3: true,
),
home: const DicePage(),
);
}
}
class DicePage extends StatefulWidget{
const DicePage({super.key});
@override
State<StatefulWidget> createState() => _DicePageState();
}
class _DicePageState extends State<DicePage>{
// states
int _firstDiceNumber=3;
int _secondDiceNumber=5;
void _onDiceTap() {
// Generate two random numbers between 1 and 6
setState(() {
_firstDiceNumber = Random().nextInt(6) + 1; // Random number between 1 and 6
_secondDiceNumber = Random().nextInt(6) + 1; // Random number between 1 and 6
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).colorScheme.primary,
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.primary,
title: Text('Dice App'),
),
body: Center(
child: Row(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: GestureDetector(
onTap: _onDiceTap,
child: Image.asset('images/dice$_firstDiceNumber.png'),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: GestureDetector(
onTap: _onDiceTap,
child: Image.asset('images/dice$_secondDiceNumber.png'),
),
),
),
],
),
),
);
}
}
주사위 이미지를 누를때 마다 바뀐다.
assets:
- images/
pubspec.yaml