Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:math';
- import 'package:flutter/material.dart';
- void main() => runApp(MyApp());
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Guess the number',
- home: Scaffold(
- backgroundColor: Colors.grey[900],
- appBar: AppBar(
- backgroundColor: Colors.grey[900],
- title: Text('Guess the number'),
- centerTitle: true,
- elevation: 0,
- ),
- body: Body(),
- ),
- );
- }
- }
- class Body extends StatefulWidget {
- const Body({
- Key key,
- }) : super(key: key);
- @override
- _BodyState createState() => _BodyState();
- }
- class _BodyState extends State<Body> {
- int wrongCount = 0;
- int rightCount = 0;
- int currentMark = 0;
- int barValue = 0;
- String answerValue = '0';
- var randomValue = new Random();
- @override
- void initState() {
- // TODO: implement initState
- super.initState();
- barValue = randomValue.nextInt(99);
- print(barValue);
- }
- void refreshBarValue() {
- barValue = randomValue.nextInt(99);
- answerValue = '0';
- print(barValue);
- setState(() {});
- }
- void clearAnswer() {
- answerValue = '0';
- setState(() {});
- }
- void submitAnswer() {
- if (int.parse(answerValue) == barValue) {
- rightCount = rightCount + 1;
- currentMark = currentMark + 1;
- } else {
- wrongCount = wrongCount + 1;
- if (currentMark > 0) {
- currentMark = currentMark - 1;
- }
- }
- barValue = randomValue.nextInt(99);
- print(barValue);
- setState(() {});
- }
- void numberClicked(number) {
- if (answerValue.length > 1) {
- answerValue = '0';
- }
- if (answerValue == '0') {
- answerValue = number.toString();
- print(number);
- } else {
- answerValue = answerValue + number.toString();
- }
- setState(() {});
- }
- @override
- Widget build(BuildContext context) {
- return Column(
- children: [
- Container(
- margin: EdgeInsets.only(top: 20.0),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceEvenly,
- children: [
- Container(
- child: Row(
- children: [
- Icon(
- Icons.error,
- color: Colors.red,
- ),
- SizedBox(
- width: 5.0,
- ),
- Text(
- '$wrongCount',
- style: TextStyle(color: Colors.white),
- ),
- ],
- ),
- ),
- Container(
- child: Row(
- children: [
- Icon(
- Icons.check_circle,
- color: Colors.green,
- ),
- SizedBox(
- width: 5.0,
- ),
- Text(
- '$rightCount',
- style: TextStyle(color: Colors.white),
- ),
- ],
- ),
- ),
- Container(
- child: Row(
- children: [
- Icon(
- Icons.mark_chat_unread,
- color: Colors.yellow,
- ),
- SizedBox(
- width: 5.0,
- ),
- Text(
- '$currentMark',
- style: TextStyle(color: Colors.white),
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- Container(
- margin: EdgeInsets.only(top: 100.0),
- child: Slider(
- min: 0,
- max: 99,
- activeColor: Colors.yellow,
- inactiveColor: Colors.yellow[100],
- onChanged: (value) {},
- value: barValue.toDouble(),
- ),
- ),
- Container(
- margin: EdgeInsets.only(top: 20.0),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Text(
- 'Your Answer : $answerValue',
- style: TextStyle(color: Colors.white),
- ),
- SizedBox(width: 10.0),
- Icon(
- Icons.check_circle,
- color: Colors.green,
- )
- ],
- ),
- ),
- SizedBox(
- height: 10.0,
- ),
- Expanded(
- child: Container(
- padding: EdgeInsets.all(10.0),
- child: Column(
- children: [
- Expanded(
- child: Row(
- children: [
- Expanded(
- child: InkWell(
- onTap: () => numberClicked(1),
- child: Container(
- decoration: BoxDecoration(
- color: Colors.grey[800],
- borderRadius: BorderRadius.circular(10)),
- child: Center(
- child: Text(
- '1',
- style: TextStyle(
- color: Colors.white,
- fontSize: 20.0,
- ),
- ),
- ),
- ),
- ),
- ),
- SizedBox(
- width: 10.0,
- ),
- Expanded(
- child: InkWell(
- onTap: () => numberClicked(2),
- child: Container(
- decoration: BoxDecoration(
- color: Colors.grey[800],
- borderRadius: BorderRadius.circular(10)),
- child: Center(
- child: Text(
- '2',
- style: TextStyle(
- color: Colors.white,
- fontSize: 20.0,
- ),
- ),
- ),
- ),
- ),
- ),
- SizedBox(
- width: 10.0,
- ),
- Expanded(
- child: InkWell(
- onTap: () => numberClicked(3),
- child: Container(
- decoration: BoxDecoration(
- color: Colors.grey[800],
- borderRadius: BorderRadius.circular(10)),
- child: Center(
- child: Text(
- '3',
- style: TextStyle(
- color: Colors.white,
- fontSize: 20.0,
- ),
- ),
- ),
- ),
- ),
- ),
- ],
- ),
- ),
- SizedBox(
- height: 10.0,
- ),
- Expanded(
- child: Row(
- children: [
- Expanded(
- child: InkWell(
- onTap: () => numberClicked(4),
- child: Container(
- decoration: BoxDecoration(
- color: Colors.grey[800],
- borderRadius: BorderRadius.circular(10)),
- child: Center(
- child: Text(
- '4',
- style: TextStyle(
- color: Colors.white,
- fontSize: 20.0,
- ),
- ),
- ),
- ),
- ),
- ),
- SizedBox(
- width: 10.0,
- ),
- Expanded(
- child: InkWell(
- onTap: () => numberClicked(5),
- child: Container(
- decoration: BoxDecoration(
- color: Colors.grey[800],
- borderRadius: BorderRadius.circular(10)),
- child: Center(
- child: Text(
- '5',
- style: TextStyle(
- color: Colors.white,
- fontSize: 20.0,
- ),
- ),
- ),
- ),
- ),
- ),
- SizedBox(
- width: 10.0,
- ),
- Expanded(
- child: InkWell(
- onTap: () => numberClicked(6),
- child: Container(
- decoration: BoxDecoration(
- color: Colors.grey[800],
- borderRadius: BorderRadius.circular(10)),
- child: Center(
- child: Text(
- '6',
- style: TextStyle(
- color: Colors.white,
- fontSize: 20.0,
- ),
- ),
- ),
- ),
- ),
- ),
- ],
- ),
- ),
- SizedBox(
- height: 10.0,
- ),
- Expanded(
- child: Row(
- children: [
- Expanded(
- child: InkWell(
- onTap: () => numberClicked(7),
- child: Container(
- decoration: BoxDecoration(
- color: Colors.grey[800],
- borderRadius: BorderRadius.circular(10)),
- child: Center(
- child: Text(
- '7',
- style: TextStyle(
- color: Colors.white,
- fontSize: 20.0,
- ),
- ),
- ),
- ),
- ),
- ),
- SizedBox(
- width: 10.0,
- ),
- Expanded(
- child: InkWell(
- onTap: () => numberClicked(8),
- child: Container(
- decoration: BoxDecoration(
- color: Colors.grey[800],
- borderRadius: BorderRadius.circular(10)),
- child: Center(
- child: Text(
- '8',
- style: TextStyle(
- color: Colors.white,
- fontSize: 20.0,
- ),
- ),
- ),
- ),
- ),
- ),
- SizedBox(
- width: 10.0,
- ),
- Expanded(
- child: InkWell(
- onTap: () => numberClicked(9),
- child: Container(
- decoration: BoxDecoration(
- color: Colors.grey[800],
- borderRadius: BorderRadius.circular(10)),
- child: Center(
- child: Text(
- '9',
- style: TextStyle(
- color: Colors.white,
- fontSize: 20.0,
- ),
- ),
- ),
- ),
- ),
- ),
- ],
- ),
- ),
- SizedBox(
- height: 10.0,
- ),
- Expanded(
- child: Row(
- children: [
- Expanded(
- child: InkWell(
- onTap: () => numberClicked(0),
- child: Container(
- decoration: BoxDecoration(
- color: Colors.grey[800],
- borderRadius: BorderRadius.circular(10)),
- child: Center(
- child: Text(
- '0',
- style: TextStyle(
- color: Colors.white,
- fontSize: 20.0,
- ),
- ),
- ),
- ),
- ),
- ),
- SizedBox(
- width: 10.0,
- ),
- Expanded(
- child: InkWell(
- onTap: clearAnswer,
- child: Container(
- decoration: BoxDecoration(
- color: Colors.red[800],
- borderRadius: BorderRadius.circular(10)),
- child: Center(
- child: Text(
- 'Clear',
- style: TextStyle(
- color: Colors.white,
- fontSize: 20.0,
- ),
- ),
- ),
- ),
- ),
- ),
- SizedBox(
- width: 10.0,
- ),
- Expanded(
- child: InkWell(
- onTap: submitAnswer,
- child: Container(
- decoration: BoxDecoration(
- color: Colors.green[800],
- borderRadius: BorderRadius.circular(10)),
- child: Center(
- child: Text(
- 'Submit',
- style: TextStyle(
- color: Colors.white,
- fontSize: 20.0,
- ),
- ),
- ),
- ),
- ),
- ),
- ],
- ),
- )
- ],
- ),
- ),
- ),
- ],
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement