Advertisement
Pit_Anonim

practice flutter course

Nov 17th, 2021
922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.22 KB | None | 0 0
  1. import 'dart:math';
  2.  
  3. import 'package:flutter/material.dart';
  4.  
  5. void main() {
  6.   runApp(const MyApp());
  7. }
  8.  
  9. class MyApp extends StatelessWidget {
  10.   const MyApp({Key? key}) : super(key: key);
  11.  
  12.   @override
  13.   Widget build(BuildContext context) {
  14.     return MaterialApp(
  15.       title: 'Flutter Demo',
  16.       theme: ThemeData(
  17.         primarySwatch: Colors.blue,
  18.       ),
  19.       home: const MyHomePage(title: 'Flutter Demo Home Page'),
  20.     );
  21.   }
  22. }
  23.  
  24. class MyHomePage extends StatefulWidget {
  25.   const MyHomePage({Key? key, required this.title}) : super(key: key);
  26.  
  27.   final String title;
  28.  
  29.   @override
  30.   State<MyHomePage> createState() => _MyHomePageState();
  31. }
  32.  
  33. class _MyHomePageState extends State<MyHomePage> {
  34.  
  35.   int a = 0, b = 0, c = 0;
  36.  
  37.   static List<double> calculate(int a, int b, int c) {
  38.     double discr = (b*b - 4*a*c).toDouble();
  39.     if (discr < 0) throw Exception();
  40.  
  41.     if(discr > 0) {
  42.       double x1 = (-b + sqrt(discr)) / 2 * a;
  43.       double x2 = (-b - sqrt(discr)) / 2 * a;
  44.       return [x1, x2];
  45.     } else {
  46.       double x1 = (-b) / 2 * a;
  47.       return [x1];
  48.     }
  49.   }
  50.  
  51.   String check() {
  52.     try {
  53.       List n = calculate(a,b,c);
  54.       return n.toString();
  55.     } catch(e) {
  56.       return 'Некорректное уравнение!';
  57.     }
  58.   }
  59.  
  60.   @override
  61.   Widget build(BuildContext context) {
  62.     return DefaultTabController(
  63.       initialIndex: 0,
  64.       length: 2,
  65.       child: Scaffold(
  66.         appBar: AppBar(
  67.           title: Text(widget.title),
  68.           bottom: const TabBar(
  69.             tabs: <Widget>[
  70.               Tab(
  71.                 icon: Text('Calculate')
  72.               ),
  73.               Tab(
  74.                 icon: Text('Result')
  75.               ),
  76.             ],
  77.           ),
  78.         ),
  79.         body: TabBarView(
  80.           children: <Widget>[
  81.             Center(
  82.               child: Container(
  83.                 child: Column(
  84.                   children: [
  85.                     Align(
  86.                       alignment: Alignment.centerLeft,
  87.                       child: Text('Ваше уравнение:', style: TextStyle(fontSize: 30),),
  88.                     ),
  89.                     Text('$a*x^2 + $b*x + $c = 0'),
  90.                     Row(
  91.                       children: [
  92.                         Expanded(
  93.                           flex: 1,
  94.                           child: TextField(
  95.                             onChanged: (t) {setState(() {a = int.parse(t);});},
  96.                           ),
  97.                         ),
  98.                         Expanded(
  99.                           flex: 1,
  100.                           child: TextField(
  101.                             onChanged: (t) {setState(() {b = int.parse(t);});},
  102.                           ),
  103.                         ),
  104.                         Expanded(
  105.                           flex: 1,
  106.                           child: TextField(
  107.                             onChanged: (t) {setState(() {c = int.parse(t);});},
  108.                           ),
  109.                         ),
  110.                       ],
  111.                     )
  112.                   ],
  113.                 ),
  114.               ),
  115.             ),
  116.             Center(
  117.               child: Text(check()),
  118.             ),
  119.           ],
  120.         ),
  121.       )
  122.     );
  123.   }
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement