Advertisement
ZawXtut

flutter_SanDev008

Oct 30th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 6.42 KB | None | 0 0
  1. import 'dart:math';
  2. import 'package:flutter/material.dart';
  3. import 'package:math_expressions/math_expressions.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 Slider Game',
  12.       home: Scaffold(
  13.         appBar: AppBar(
  14.           title: Text('Guess Slider Game'),
  15.         ),
  16.         body: Center(
  17.           child: Container(
  18.             child: MainActivity(),
  19.           ),
  20.         ),
  21.       ),
  22.     );
  23.   }
  24. }
  25.  
  26. class MainActivity extends StatefulWidget {
  27.   MainActivity({Key key}) : super(key: key);
  28.  
  29.   @override
  30.   _MainActivityState createState() => _MainActivityState();
  31. }
  32.  
  33. class _MainActivityState extends State<MainActivity> {
  34.   Random _random = Random();
  35.  
  36.   double _value =Random().nextInt(99).toDouble();
  37.   var userAns = '';
  38.   var _mTx = '';
  39.   var _Wtext = 0;
  40.   var _Rtext = 0;
  41.   final btns = [
  42.     '1',
  43.     '2',
  44.     '3',
  45.     '4',
  46.     '5',
  47.     '6',
  48.     '7',
  49.     '8',
  50.     '9',
  51.     '0',
  52.     '⟲Re',
  53.     'enter'
  54.   ];
  55.  
  56.   _SldChange(double value) {
  57.     setState(() {
  58.       _value = value;
  59.     });
  60.   }
  61.  
  62.   @override
  63.   Widget build(BuildContext context) {
  64.     return Container(
  65.       child: Column(
  66.         children: [
  67.           Expanded(
  68.             child: Column(children: [
  69.               Container(
  70.                 padding: EdgeInsets.all(20),
  71.                 margin: EdgeInsets.only(top: 10),
  72.                 child: Row(
  73.                   mainAxisAlignment: MainAxisAlignment.spaceBetween,
  74.                   children: [
  75.                     Column(
  76.                       children: [
  77.                         Text("M"),
  78.                         Text(_mTx)
  79.                         // Text(_value.toStringAsFixed(50).toString()),
  80.                       ],
  81.                     ),
  82.                     Column(
  83.                       children: [
  84.                         Text("W"),
  85.                         Text('$_Wtext'),
  86.                       ],
  87.                     ),
  88.                     Column(
  89.                       children: [
  90.                         Text("R"),
  91.                         Text('$_Rtext'),
  92.                       ],
  93.                     ),
  94.                   ],
  95.                 ),
  96.               ),
  97.               Container(
  98.                   margin: EdgeInsets.only(top: 10),
  99.                   child: Slider(
  100.                     max: 99,
  101.                     min: 0,
  102.                     value: _value,
  103.                     onChanged: _SldChange,
  104.  
  105.                     // value: _value, onChanged: _setvalue
  106.                   )),
  107.               Container(
  108.                 margin: EdgeInsets.only(top: 10),
  109.                 padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
  110.                 child: Row(
  111.                   mainAxisAlignment: MainAxisAlignment.spaceBetween,
  112.                   children: [
  113.                     Text(_value.round().toString()),
  114.                     Text(
  115.                       userAns,
  116.                     ),
  117.                     RaisedButton(
  118.                       shape: RoundedRectangleBorder(
  119.                           borderRadius: BorderRadius.circular(20),
  120.                           side: BorderSide(color: Colors.blueAccent, width: 3)),
  121.                       textColor: Colors.white,
  122.                       color: Colors.blueGrey,
  123.                       onPressed: () {
  124.                         setState(() {
  125.                           userAns = userAns.substring(0, userAns.length - 1);
  126.                         });
  127.                       },
  128.                       child: const Text(
  129.                         'β«· delete',
  130.                         style: TextStyle(fontSize: 20),
  131.                       ),
  132.                     ),
  133.                   ],
  134.                 ),
  135.               )
  136.             ]),
  137.           ),
  138.           Expanded(
  139.             flex: 2,
  140.             child: Container(
  141.               child: GridView.builder(
  142.                 itemCount: btns.length,
  143.                 gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  144.                     crossAxisCount: 4),
  145.                 itemBuilder: (BuildContext context, int index) {
  146.                   if (index == 11) {
  147.                     return NumBtn(
  148.                       btn_clck: () {
  149.                         var userStr = num.parse(userAns);
  150.                         var _userSlid = _value.round().toInt();
  151.  
  152.                         if (_userSlid == userStr) {
  153.                           _mTx = '2';
  154.                           _Wtext++;
  155.                         } else {
  156.                           _Rtext--;
  157.                         }
  158.  
  159.                         setState(() {});
  160.                       },
  161.                       btntxt: btns[index],
  162.                       color: Colors.blueGrey,
  163.                       txtcolor: Colors.green,
  164.                     );
  165.                   } else if (index == 10) {
  166.                     return NumBtn(
  167.                       btntxt: btns[index],
  168.                       color: Colors.blueGrey,
  169.                       txtcolor: Colors.green,
  170.                       btn_clck: () {
  171.                         _mTx = '';
  172.                         setState(() {});
  173.                       },
  174.                     );
  175.                   } else {
  176.                     return NumBtn(
  177.                       btn_clck: () {
  178.                         setState(() {
  179.                           userAns += btns[index];
  180.                         });
  181.                       },
  182.                       btntxt: btns[index],
  183.                       color: Colors.blueGrey,
  184.                       txtcolor: Colors.white,
  185.                     );
  186.                   }
  187.                 },
  188.               ),
  189.             ),
  190.           )
  191.         ],
  192.       ),
  193.     );
  194.   }
  195. }
  196.  
  197. class NumBtn extends StatelessWidget {
  198.   final txtcolor;
  199.   final color;
  200.   final String btntxt;
  201.   final btn_clck;
  202.   NumBtn({this.btntxt, this.txtcolor, this.color, this.btn_clck});
  203.   @override
  204.   Widget build(BuildContext context) {
  205.     return Container(
  206.         child: GestureDetector(
  207.       onTap: btn_clck,
  208.       child: Padding(
  209.         padding: const EdgeInsets.all(5),
  210.         child: ClipRRect(
  211.           borderRadius: BorderRadius.circular(15),
  212.           child: Container(
  213.             color: color,
  214.             child: Center(
  215.               child: Text(
  216.                 btntxt,
  217.                 style: TextStyle(color: txtcolor),
  218.               ),
  219.             ),
  220.           ),
  221.         ),
  222.       ),
  223.     ));
  224.   }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement