smartcomputing123

main.dart

Mar 11th, 2023
133
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 13.99 KB | Source Code | 0 0
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:fluttertoast/fluttertoast.dart';
  4.  
  5. void main() => runApp(MyApp());
  6.  
  7. class MyApp extends StatelessWidget {
  8.   // This widget is the root of your application.
  9.   @override
  10.   Widget build(BuildContext context) {
  11.     return MaterialApp(
  12.       title: 'Flutter UI Controls',
  13.       theme: ThemeData(
  14.         primarySwatch: Colors.blue,
  15.       ),
  16.       home: MyHomePage(title: 'Flutter UI Controls Home Page'),
  17.     );
  18.   }
  19. }
  20.  
  21. class MyHomePage extends StatefulWidget {
  22.   final String title;
  23.   const MyHomePage({Key? key, required this.title}) : super(key: key);
  24.  
  25.   @override
  26.   _MyHomePageState createState() => _MyHomePageState();
  27. }
  28.  
  29. class _MyHomePageState extends State<MyHomePage> {
  30.   final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  31.   String? _dropdownButtonValue = 'One';
  32.   String? _popupMenuButtonValue = 'One';
  33.   bool? _checkboxValue = true;
  34.   String? _radioBoxValue = 'One';
  35.   double _sliderValue = 10;
  36.   bool _switchValue = false;
  37.  
  38.   FToast? fToast;
  39.  
  40.   @override
  41.   void initState() {
  42.     super.initState();
  43.     fToast = FToast();
  44.     fToast?.init(context);
  45.   }
  46.  
  47.   @override
  48.   Widget build(BuildContext context) {
  49.     return Scaffold(
  50.       key: _scaffoldKey,
  51.       appBar: AppBar(
  52.         title: Text(widget.title),
  53.       ),
  54.       body: Container(
  55.         padding: EdgeInsets.only(left: 10, right: 10),
  56.         child: SingleChildScrollView(
  57.           child: Column(
  58.             mainAxisAlignment: MainAxisAlignment.start,
  59.             crossAxisAlignment: CrossAxisAlignment.start,
  60.             children: <Widget>[
  61.               // Buttons
  62.               Column(
  63.                 mainAxisAlignment: MainAxisAlignment.start,
  64.                 crossAxisAlignment: CrossAxisAlignment.start,
  65.                 children: <Widget>[
  66.                   _GroupText('Buttons'),
  67.                   ButtonBar(
  68.                     alignment: MainAxisAlignment.start,
  69.                     mainAxisSize: MainAxisSize.min,
  70.                     buttonMinWidth: 200,
  71.                     buttonHeight: 30,
  72.                     buttonAlignedDropdown: true,
  73.                     layoutBehavior: ButtonBarLayoutBehavior.padded,
  74.                     buttonPadding: EdgeInsets.symmetric(vertical: 10),
  75.                     children: <Widget>[
  76.                       ElevatedButton(
  77.                         onPressed: () => _showToast('Clicked on RaisedButton'),
  78.                         child: Text('Raised Button'),
  79.                       ),
  80.                       ElevatedButton(
  81.                         onPressed: () => _showToast('Clicked on FlatButton'),
  82.                         child: Text('Flat Button'),
  83.                       ),
  84.                       TextButton(
  85.                         onPressed: () => _showToast('Clicked on OutlineButton'),
  86.                         child: Text('OutlineButton'),
  87.                       ),
  88.                       Row(
  89.                         children: <Widget>[
  90.                           Text('IconButton: '),
  91.                           IconButton(
  92.                             onPressed: () =>
  93.                                 _showToast('Clicked on IconButton'),
  94.                             icon: Icon(Icons.build),
  95.                           ),
  96.                         ],
  97.                       ),
  98.                       Row(
  99.                         children: <Widget>[
  100.                           Text('DropdownButton: '),
  101.                           DropdownButton(
  102.                               value: _dropdownButtonValue,
  103.                               onChanged: (String? value) {
  104.                                 setState(() {
  105.                                   _dropdownButtonValue = value;
  106.                                 });
  107.                                 _showToast(
  108.                                     'Changed value of dropdown button to $value');
  109.                               },
  110.                               items: ['One', 'Two', 'Three', 'Four']
  111.                                   .map<DropdownMenuItem<String>>(
  112.                                       (String value) =>
  113.                                           DropdownMenuItem<String>(
  114.                                             value: value,
  115.                                             child: Text(value),
  116.                                           ))
  117.                                   .toList()),
  118.                         ],
  119.                       ),
  120.                       Row(
  121.                         children: <Widget>[
  122.                           Text('PopupMenuButton: '),
  123.                           Text(_popupMenuButtonValue!),
  124.                           PopupMenuButton<String>(
  125.                               onSelected: (String result) {
  126.                                 setState(() {
  127.                                   _popupMenuButtonValue = result;
  128.                                 });
  129.                                 _showToast(
  130.                                     'Selected \'$result\' item on PopupMenuButton');
  131.                               },
  132.                               itemBuilder: (BuildContext context) =>
  133.                                   <PopupMenuEntry<String>>[
  134.                                     const PopupMenuItem<String>(
  135.                                       value: 'One',
  136.                                       child: Text('One'),
  137.                                     ),
  138.                                     const PopupMenuItem<String>(
  139.                                       value: 'Two',
  140.                                       child: Text('Two'),
  141.                                     ),
  142.                                     const PopupMenuItem<String>(
  143.                                       value: 'Three',
  144.                                       child: Text('Three'),
  145.                                     ),
  146.                                     const PopupMenuItem<String>(
  147.                                       value: 'Four',
  148.                                       child: Text('Four'),
  149.                                     ),
  150.                                   ]),
  151.                         ],
  152.                       ),
  153.                     ],
  154.                   )
  155.                 ],
  156.               ),
  157.               _SpaceLine(),
  158.               // Checkbox
  159.               Column(
  160.                   mainAxisAlignment: MainAxisAlignment.start,
  161.                   crossAxisAlignment: CrossAxisAlignment.start,
  162.                   children: <Widget>[
  163.                     _GroupText('Checkbox'),
  164.                     Row(
  165.                       children: <Widget>[
  166.                         Text('Simple checkbox'),
  167.                         Checkbox(
  168.                             value: _checkboxValue,
  169.                             onChanged: (bool? newValue) {
  170.                               setState(() {
  171.                                 _checkboxValue = newValue;
  172.                               });
  173.                               _showToast(
  174.                                   'Changed value of checkbox to $_checkboxValue');
  175.                             }),
  176.                       ],
  177.                     )
  178.                   ]),
  179.               _SpaceLine(),
  180.               // Radio[Box]
  181.               Column(
  182.                 mainAxisAlignment: MainAxisAlignment.start,
  183.                 crossAxisAlignment: CrossAxisAlignment.start,
  184.                 children: <Widget>[
  185.                   _GroupText('Radio[Box]'),
  186.                   RadioListTile(
  187.                       title: const Text('One'),
  188.                       value: 'One',
  189.                       groupValue: _radioBoxValue,
  190.                       onChanged: (String? value) {
  191.                         setState(() {
  192.                           _radioBoxValue = value;
  193.                         });
  194.                         _showToast('Changed value of Radio[Box] to $value');
  195.                       }),
  196.                   RadioListTile(
  197.                       title: const Text('Two'),
  198.                       value: 'Two',
  199.                       groupValue: _radioBoxValue,
  200.                       onChanged: (String? value) {
  201.                         setState(() {
  202.                           _radioBoxValue = value;
  203.                         });
  204.                         _showToast('Changed value of Radio[Box] to $value');
  205.                       }),
  206.                   RadioListTile(
  207.                       title: const Text('Three'),
  208.                       value: 'Three',
  209.                       groupValue: _radioBoxValue,
  210.                       onChanged: (String? value) {
  211.                         setState(() {
  212.                           _radioBoxValue = value;
  213.                         });
  214.                         _showToast('Changed value of Radio[Box] to $value');
  215.                       }),
  216.                   RadioListTile(
  217.                       title: const Text('Four'),
  218.                       value: 'Four',
  219.                       groupValue: _radioBoxValue,
  220.                       onChanged: (String? value) {
  221.                         setState(() {
  222.                           _radioBoxValue = value;
  223.                         });
  224.                         _showToast('Changed value of Radio[Box] to $value');
  225.                       }),
  226.                 ],
  227.               ),
  228.               _SpaceLine(),
  229.               // Slider
  230.               Column(
  231.                 mainAxisAlignment: MainAxisAlignment.start,
  232.                 crossAxisAlignment: CrossAxisAlignment.start,
  233.                 children: <Widget>[
  234.                   _GroupText('Slider'),
  235.                   Slider(
  236.                     value: _sliderValue,
  237.                     onChanged: (newValue) {
  238.                       setState(() {
  239.                         _sliderValue = newValue;
  240.                       });
  241.                     },
  242.                     min: 0,
  243.                     max: 100,
  244.                     divisions: 50,
  245.                     label: _sliderValue.toInt().toString(),
  246.                   ),
  247.                 ],
  248.               ),
  249.               _SpaceLine(),
  250.               // Switch
  251.               Column(
  252.                 mainAxisAlignment: MainAxisAlignment.start,
  253.                 crossAxisAlignment: CrossAxisAlignment.start,
  254.                 children: <Widget>[
  255.                   _GroupText('Switch'),
  256.                   Row(
  257.                     children: <Widget>[
  258.                       Text('Simple switch'),
  259.                       Switch(
  260.                         value: _switchValue,
  261.                         onChanged: (newValue) {
  262.                           setState(() {
  263.                             _switchValue = newValue;
  264.                           });
  265.                           _showToast('Changed value of Switch to $newValue');
  266.                         },
  267.                       )
  268.                     ],
  269.                   ),
  270.                 ],
  271.               ),
  272.               _SpaceLine(),
  273.               // TextField
  274.               Column(
  275.                 mainAxisAlignment: MainAxisAlignment.start,
  276.                 crossAxisAlignment: CrossAxisAlignment.start,
  277.                 children: <Widget>[
  278.                   _GroupText('TextField'),
  279.                   TextField(
  280.                     obscureText: false,
  281.                     decoration: InputDecoration(
  282.                       border: OutlineInputBorder(),
  283.                       labelText: 'TextField',
  284.                     ),
  285.                   ),
  286.                 ],
  287.               ),
  288.               _SpaceLine(),
  289.               // DateTimePicker
  290.               Column(
  291.                 mainAxisAlignment: MainAxisAlignment.start,
  292.                 crossAxisAlignment: CrossAxisAlignment.start,
  293.                 children: <Widget>[
  294.                   _GroupText('DateTimePicker'),
  295.                   OutlinedButton(
  296.                     onPressed: () {
  297.                       showDatePicker(
  298.                         context: context,
  299.                         initialDate: DateTime.now(),
  300.                         firstDate: DateTime(1970),
  301.                         lastDate: DateTime.now(),
  302.                       ).then((value) {
  303.                         _showToast('Selected date $value');
  304.                       });
  305.                     },
  306.                     child: Text('Open DatePicker'),
  307.                   ),
  308.                   OutlinedButton(
  309.                     onPressed: () {
  310.                       showTimePicker(
  311.                         context: context,
  312.                         initialTime: TimeOfDay.now(),
  313.                       ).then((value) {
  314.                         _showToast('Selected time $value');
  315.                       });
  316.                     },
  317.                     child: Text('Open TimePicker'),
  318.                   ),
  319.                 ],
  320.               ),
  321.               SizedBox(
  322.                 height: 50,
  323.               )
  324.             ],
  325.           ),
  326.         ),
  327.       ),
  328.       floatingActionButton: FloatingActionButton(
  329.         onPressed: () => _showToast('Clicked on float action button'),
  330.         tooltip: 'Increment',
  331.         child: Icon(Icons.add),
  332.       ),
  333.     );
  334.   }
  335.  
  336.   void _showToast(String text) {
  337.     /* do something*/
  338.     Widget toast = Container(
  339.       padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0),
  340.       decoration: BoxDecoration(
  341.         borderRadius: BorderRadius.circular(25.0),
  342.         color: Colors.blueGrey,
  343.       ),
  344.       child: Text(
  345.         text,
  346.         style: TextStyle(color: Colors.white),
  347.       ),
  348.     );
  349.  
  350.     fToast?.showToast(
  351.       child: toast,
  352.       toastDuration: const Duration(seconds: 3),
  353.     );
  354.   }
  355. }
  356.  
  357. class _GroupText extends StatelessWidget {
  358.   final String text;
  359.  
  360.   const _GroupText(this.text);
  361.  
  362.   @override
  363.   Widget build(BuildContext context) {
  364.     return Padding(
  365.       padding: EdgeInsets.symmetric(vertical: 5, horizontal: 15),
  366.       child: Text(
  367.         text,
  368.         style: TextStyle(fontSize: 25, fontWeight: FontWeight.w500),
  369.       ),
  370.     );
  371.   }
  372. }
  373.  
  374. class _SpaceLine extends StatelessWidget {
  375.   @override
  376.   Widget build(BuildContext context) {
  377.     return SizedBox(
  378.       height: 5,
  379.       child: Container(
  380.         color: Colors.grey,
  381.       ),
  382.     );
  383.   }
  384. }
  385.  
Tags: flutter
Advertisement
Comments
  • # text 0.17 KB | 0 0
    1. packages:
    2. https://pub.dev/packages/fluttertoast
    3. install:
    4. flutter pub add fluttertoast
    5. tutorial:
    6. https://medium.flutterdevs.com/custom-toast-message-in-flutter-102d3aabdb
    7.  
Add Comment
Please, Sign In to add comment