Advertisement
saimain-dev

Guess the number homework

Nov 6th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 16.25 KB | None | 0 0
  1. import 'dart:math';
  2.  
  3. import 'package:flutter/material.dart';
  4.  
  5. void main() => runApp(MyApp());
  6.  
  7. class MyApp extends StatelessWidget {
  8.   @override
  9.   Widget build(BuildContext context) {
  10.     return MaterialApp(
  11.       title: 'Guess the number',
  12.       home: Scaffold(
  13.         backgroundColor: Colors.grey[900],
  14.         appBar: AppBar(
  15.           backgroundColor: Colors.grey[900],
  16.           title: Text('Guess the number'),
  17.           centerTitle: true,
  18.           elevation: 0,
  19.         ),
  20.         body: Body(),
  21.       ),
  22.     );
  23.   }
  24. }
  25.  
  26. class Body extends StatefulWidget {
  27.   const Body({
  28.     Key key,
  29.   }) : super(key: key);
  30.  
  31.   @override
  32.   _BodyState createState() => _BodyState();
  33. }
  34.  
  35. class _BodyState extends State<Body> {
  36.   int wrongCount = 0;
  37.   int rightCount = 0;
  38.   int currentMark = 0;
  39.   int barValue = 0;
  40.   String answerValue = '0';
  41.  
  42.   var randomValue = new Random();
  43.  
  44.   @override
  45.   void initState() {
  46.     // TODO: implement initState
  47.     super.initState();
  48.     barValue = randomValue.nextInt(99);
  49.     print(barValue);
  50.   }
  51.  
  52.   void refreshBarValue() {
  53.     barValue = randomValue.nextInt(99);
  54.     answerValue = '0';
  55.     print(barValue);
  56.     setState(() {});
  57.   }
  58.  
  59.   void clearAnswer() {
  60.     answerValue = '0';
  61.     setState(() {});
  62.   }
  63.  
  64.   void submitAnswer() {
  65.     if (int.parse(answerValue) == barValue) {
  66.       rightCount = rightCount + 1;
  67.       currentMark = currentMark + 1;
  68.     } else {
  69.       wrongCount = wrongCount + 1;
  70.       if (currentMark > 0) {
  71.         currentMark = currentMark - 1;
  72.       }
  73.     }
  74.  
  75.     barValue = randomValue.nextInt(99);
  76.     print(barValue);
  77.     setState(() {});
  78.   }
  79.  
  80.   void numberClicked(number) {
  81.     if (answerValue.length > 1) {
  82.       answerValue = '0';
  83.     }
  84.  
  85.     if (answerValue == '0') {
  86.       answerValue = number.toString();
  87.       print(number);
  88.     } else {
  89.       answerValue = answerValue + number.toString();
  90.     }
  91.     setState(() {});
  92.   }
  93.  
  94.   @override
  95.   Widget build(BuildContext context) {
  96.     return Column(
  97.       children: [
  98.         Container(
  99.           margin: EdgeInsets.only(top: 20.0),
  100.           child: Row(
  101.             mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  102.             children: [
  103.               Container(
  104.                 child: Row(
  105.                   children: [
  106.                     Icon(
  107.                       Icons.error,
  108.                       color: Colors.red,
  109.                     ),
  110.                     SizedBox(
  111.                       width: 5.0,
  112.                     ),
  113.                     Text(
  114.                       '$wrongCount',
  115.                       style: TextStyle(color: Colors.white),
  116.                     ),
  117.                   ],
  118.                 ),
  119.               ),
  120.               Container(
  121.                 child: Row(
  122.                   children: [
  123.                     Icon(
  124.                       Icons.check_circle,
  125.                       color: Colors.green,
  126.                     ),
  127.                     SizedBox(
  128.                       width: 5.0,
  129.                     ),
  130.                     Text(
  131.                       '$rightCount',
  132.                       style: TextStyle(color: Colors.white),
  133.                     ),
  134.                   ],
  135.                 ),
  136.               ),
  137.               Container(
  138.                 child: Row(
  139.                   children: [
  140.                     Icon(
  141.                       Icons.mark_chat_unread,
  142.                       color: Colors.yellow,
  143.                     ),
  144.                     SizedBox(
  145.                       width: 5.0,
  146.                     ),
  147.                     Text(
  148.                       '$currentMark',
  149.                       style: TextStyle(color: Colors.white),
  150.                     ),
  151.                   ],
  152.                 ),
  153.               ),
  154.             ],
  155.           ),
  156.         ),
  157.         Container(
  158.           margin: EdgeInsets.only(top: 100.0),
  159.           child: Slider(
  160.             min: 0,
  161.             max: 99,
  162.             activeColor: Colors.yellow,
  163.             inactiveColor: Colors.yellow[100],
  164.             onChanged: (value) {},
  165.             value: barValue.toDouble(),
  166.           ),
  167.         ),
  168.         Container(
  169.           margin: EdgeInsets.only(top: 20.0),
  170.           child: Row(
  171.             mainAxisAlignment: MainAxisAlignment.center,
  172.             children: [
  173.               Text(
  174.                 'Your Answer : $answerValue',
  175.                 style: TextStyle(color: Colors.white),
  176.               ),
  177.               SizedBox(width: 10.0),
  178.               Icon(
  179.                 Icons.check_circle,
  180.                 color: Colors.green,
  181.               )
  182.             ],
  183.           ),
  184.         ),
  185.         SizedBox(
  186.           height: 10.0,
  187.         ),
  188.         Expanded(
  189.           child: Container(
  190.             padding: EdgeInsets.all(10.0),
  191.             child: Column(
  192.               children: [
  193.                 Expanded(
  194.                   child: Row(
  195.                     children: [
  196.                       Expanded(
  197.                         child: InkWell(
  198.                           onTap: () => numberClicked(1),
  199.                           child: Container(
  200.                             decoration: BoxDecoration(
  201.                                 color: Colors.grey[800],
  202.                                 borderRadius: BorderRadius.circular(10)),
  203.                             child: Center(
  204.                               child: Text(
  205.                                 '1',
  206.                                 style: TextStyle(
  207.                                   color: Colors.white,
  208.                                   fontSize: 20.0,
  209.                                 ),
  210.                               ),
  211.                             ),
  212.                           ),
  213.                         ),
  214.                       ),
  215.                       SizedBox(
  216.                         width: 10.0,
  217.                       ),
  218.                       Expanded(
  219.                         child: InkWell(
  220.                           onTap: () => numberClicked(2),
  221.                           child: Container(
  222.                             decoration: BoxDecoration(
  223.                                 color: Colors.grey[800],
  224.                                 borderRadius: BorderRadius.circular(10)),
  225.                             child: Center(
  226.                               child: Text(
  227.                                 '2',
  228.                                 style: TextStyle(
  229.                                   color: Colors.white,
  230.                                   fontSize: 20.0,
  231.                                 ),
  232.                               ),
  233.                             ),
  234.                           ),
  235.                         ),
  236.                       ),
  237.                       SizedBox(
  238.                         width: 10.0,
  239.                       ),
  240.                       Expanded(
  241.                         child: InkWell(
  242.                           onTap: () => numberClicked(3),
  243.                           child: Container(
  244.                             decoration: BoxDecoration(
  245.                                 color: Colors.grey[800],
  246.                                 borderRadius: BorderRadius.circular(10)),
  247.                             child: Center(
  248.                               child: Text(
  249.                                 '3',
  250.                                 style: TextStyle(
  251.                                   color: Colors.white,
  252.                                   fontSize: 20.0,
  253.                                 ),
  254.                               ),
  255.                             ),
  256.                           ),
  257.                         ),
  258.                       ),
  259.                     ],
  260.                   ),
  261.                 ),
  262.                 SizedBox(
  263.                   height: 10.0,
  264.                 ),
  265.                 Expanded(
  266.                   child: Row(
  267.                     children: [
  268.                       Expanded(
  269.                         child: InkWell(
  270.                           onTap: () => numberClicked(4),
  271.                           child: Container(
  272.                             decoration: BoxDecoration(
  273.                                 color: Colors.grey[800],
  274.                                 borderRadius: BorderRadius.circular(10)),
  275.                             child: Center(
  276.                               child: Text(
  277.                                 '4',
  278.                                 style: TextStyle(
  279.                                   color: Colors.white,
  280.                                   fontSize: 20.0,
  281.                                 ),
  282.                               ),
  283.                             ),
  284.                           ),
  285.                         ),
  286.                       ),
  287.                       SizedBox(
  288.                         width: 10.0,
  289.                       ),
  290.                       Expanded(
  291.                         child: InkWell(
  292.                           onTap: () => numberClicked(5),
  293.                           child: Container(
  294.                             decoration: BoxDecoration(
  295.                                 color: Colors.grey[800],
  296.                                 borderRadius: BorderRadius.circular(10)),
  297.                             child: Center(
  298.                               child: Text(
  299.                                 '5',
  300.                                 style: TextStyle(
  301.                                   color: Colors.white,
  302.                                   fontSize: 20.0,
  303.                                 ),
  304.                               ),
  305.                             ),
  306.                           ),
  307.                         ),
  308.                       ),
  309.                       SizedBox(
  310.                         width: 10.0,
  311.                       ),
  312.                       Expanded(
  313.                         child: InkWell(
  314.                           onTap: () => numberClicked(6),
  315.                           child: Container(
  316.                             decoration: BoxDecoration(
  317.                                 color: Colors.grey[800],
  318.                                 borderRadius: BorderRadius.circular(10)),
  319.                             child: Center(
  320.                               child: Text(
  321.                                 '6',
  322.                                 style: TextStyle(
  323.                                   color: Colors.white,
  324.                                   fontSize: 20.0,
  325.                                 ),
  326.                               ),
  327.                             ),
  328.                           ),
  329.                         ),
  330.                       ),
  331.                     ],
  332.                   ),
  333.                 ),
  334.                 SizedBox(
  335.                   height: 10.0,
  336.                 ),
  337.                 Expanded(
  338.                   child: Row(
  339.                     children: [
  340.                       Expanded(
  341.                         child: InkWell(
  342.                           onTap: () => numberClicked(7),
  343.                           child: Container(
  344.                             decoration: BoxDecoration(
  345.                                 color: Colors.grey[800],
  346.                                 borderRadius: BorderRadius.circular(10)),
  347.                             child: Center(
  348.                               child: Text(
  349.                                 '7',
  350.                                 style: TextStyle(
  351.                                   color: Colors.white,
  352.                                   fontSize: 20.0,
  353.                                 ),
  354.                               ),
  355.                             ),
  356.                           ),
  357.                         ),
  358.                       ),
  359.                       SizedBox(
  360.                         width: 10.0,
  361.                       ),
  362.                       Expanded(
  363.                         child: InkWell(
  364.                           onTap: () => numberClicked(8),
  365.                           child: Container(
  366.                             decoration: BoxDecoration(
  367.                                 color: Colors.grey[800],
  368.                                 borderRadius: BorderRadius.circular(10)),
  369.                             child: Center(
  370.                               child: Text(
  371.                                 '8',
  372.                                 style: TextStyle(
  373.                                   color: Colors.white,
  374.                                   fontSize: 20.0,
  375.                                 ),
  376.                               ),
  377.                             ),
  378.                           ),
  379.                         ),
  380.                       ),
  381.                       SizedBox(
  382.                         width: 10.0,
  383.                       ),
  384.                       Expanded(
  385.                         child: InkWell(
  386.                           onTap: () => numberClicked(9),
  387.                           child: Container(
  388.                             decoration: BoxDecoration(
  389.                                 color: Colors.grey[800],
  390.                                 borderRadius: BorderRadius.circular(10)),
  391.                             child: Center(
  392.                               child: Text(
  393.                                 '9',
  394.                                 style: TextStyle(
  395.                                   color: Colors.white,
  396.                                   fontSize: 20.0,
  397.                                 ),
  398.                               ),
  399.                             ),
  400.                           ),
  401.                         ),
  402.                       ),
  403.                     ],
  404.                   ),
  405.                 ),
  406.                 SizedBox(
  407.                   height: 10.0,
  408.                 ),
  409.                 Expanded(
  410.                   child: Row(
  411.                     children: [
  412.                       Expanded(
  413.                         child: InkWell(
  414.                           onTap: () => numberClicked(0),
  415.                           child: Container(
  416.                             decoration: BoxDecoration(
  417.                                 color: Colors.grey[800],
  418.                                 borderRadius: BorderRadius.circular(10)),
  419.                             child: Center(
  420.                               child: Text(
  421.                                 '0',
  422.                                 style: TextStyle(
  423.                                   color: Colors.white,
  424.                                   fontSize: 20.0,
  425.                                 ),
  426.                               ),
  427.                             ),
  428.                           ),
  429.                         ),
  430.                       ),
  431.                       SizedBox(
  432.                         width: 10.0,
  433.                       ),
  434.                       Expanded(
  435.                         child: InkWell(
  436.                           onTap: clearAnswer,
  437.                           child: Container(
  438.                             decoration: BoxDecoration(
  439.                                 color: Colors.red[800],
  440.                                 borderRadius: BorderRadius.circular(10)),
  441.                             child: Center(
  442.                               child: Text(
  443.                                 'Clear',
  444.                                 style: TextStyle(
  445.                                   color: Colors.white,
  446.                                   fontSize: 20.0,
  447.                                 ),
  448.                               ),
  449.                             ),
  450.                           ),
  451.                         ),
  452.                       ),
  453.                       SizedBox(
  454.                         width: 10.0,
  455.                       ),
  456.                       Expanded(
  457.                         child: InkWell(
  458.                           onTap: submitAnswer,
  459.                           child: Container(
  460.                             decoration: BoxDecoration(
  461.                                 color: Colors.green[800],
  462.                                 borderRadius: BorderRadius.circular(10)),
  463.                             child: Center(
  464.                               child: Text(
  465.                                 'Submit',
  466.                                 style: TextStyle(
  467.                                   color: Colors.white,
  468.                                   fontSize: 20.0,
  469.                                 ),
  470.                               ),
  471.                             ),
  472.                           ),
  473.                         ),
  474.                       ),
  475.                     ],
  476.                   ),
  477.                 )
  478.               ],
  479.             ),
  480.           ),
  481.         ),
  482.       ],
  483.     );
  484.   }
  485. }
  486.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement