Advertisement
loreberti10

Untitled

Jan 8th, 2020
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.77 KB | None | 0 0
  1. class AddCartButton extends StatefulWidget {
  2.   @override
  3.   State<StatefulWidget> createState() {
  4.     return AddCartButtonState();
  5.   }
  6. }
  7. class AddCartButtonState extends State<AddCartButton>{
  8.   TextEditingController myController = TextEditingController();
  9.   bool isValid = false;
  10.   @override
  11.   Widget build(BuildContext context) {
  12.     void _addToCart(){
  13.       showDialog(
  14.         context: context,
  15.         builder: (BuildContext context) {
  16.           return AlertDialog(
  17.             title: Text("QUANTITY"),
  18.             content: Column(
  19.               children: <Widget>[
  20.                 TextFormField(
  21.                     controller: myController,
  22.                     decoration: new InputDecoration(labelText: "quantity"),
  23.                     keyboardType: TextInputType.numberWithOptions(decimal: true),
  24.                     inputFormatters: <TextInputFormatter>[],
  25.                     autovalidate: true,
  26.                     validator: (value) {
  27.                       if (value.isEmpty) {
  28.                         isValid  = false;
  29.                         return "the quantity cannot be empty";
  30.                       } else if (double.tryParse(value) == null) {
  31.                         isValid = false;
  32.                         return "the quantity must be valid number";
  33.                       } else {
  34.                         isValid = true;
  35.                         return null;
  36.                       }
  37.                     }
  38.                 )
  39.               ],
  40.             ),
  41.             actions: <Widget>[
  42.               FlatButton(
  43.                   disabledTextColor: Colors.grey,
  44.                   child: Text("add"),
  45.                   onPressed: isValid ? () { print("is valid"); }: null
  46.               )
  47.             ],
  48.           );
  49.         },
  50.       );
  51.     }
  52.   }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement