Advertisement
nio74

Untitled

May 6th, 2022
1,297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 7.57 KB | None | 0 0
  1. class InsModRepair extends StatefulWidget {
  2.   InsModRepair({Key? key}) : super(key: key);
  3.  
  4.   @override
  5.   State<InsModRepair> createState() => _InsModRepairState();
  6. }
  7.  
  8. class _InsModRepairState extends State<InsModRepair> {
  9.   final _formKey = GlobalKey<FormState>();
  10.   final TextEditingController _clientController = TextEditingController();
  11.   final TextEditingController _objectController = TextEditingController();
  12.   final TextEditingController _workTodoController = TextEditingController();
  13.  
  14.   bool _formValid = false;
  15.   bool autocomleteValid = false;
  16.  
  17.   @override
  18.   void dispose() {
  19.     _objectController.dispose();
  20.     _workTodoController.dispose();
  21.     super.dispose();
  22.   }
  23.  
  24.   @override
  25.   Widget build(BuildContext context) {
  26.     return Scaffold(
  27.       appBar: AppBar(),
  28.       drawer: const DrawerCustomWidget(),
  29.       body: Column(
  30.         children: [
  31.           Row(children: [
  32.             Expanded(
  33.               child: Card(
  34.                 child: Form(
  35.                   key: _formKey,
  36.                   autovalidateMode: AutovalidateMode.onUserInteraction,
  37.                   onChanged: () {
  38.                     final isValid = _formKey.currentState?.validate() ?? false;
  39.                     setState(() {
  40.                       _formValid = isValid;
  41.                     });
  42.                   },
  43.                   child: Column(
  44.                     children: [
  45.                       const SizedBox(
  46.                         height: 30,
  47.                       ),
  48.  
  49.                       //CustomTextFormField(controller: _clientController, txtLable: '',),
  50.                       EasyAutocompleteWidget(
  51.                         txtLable: 'Inserire Cliente',
  52.                         controller: _clientController,
  53.                         errorText: _errorText,
  54.                       ),
  55.                       const SizedBox(
  56.                         height: 10,
  57.                       ),
  58.                       CustomTextFormField(
  59.                           maxline: 5,
  60.                           errorText: _errorText,
  61.                           controller: _objectController,
  62.                           txtLable: 'Oggetti'),
  63.                       const SizedBox(
  64.                         height: 10,
  65.                       ),
  66.                       CustomTextFormField(
  67.                           maxline: 5,
  68.                           errorText: _errorText,
  69.                           controller: _workTodoController,
  70.                           txtLable: 'Lavorazioni d aeseguire'),
  71.                       const SizedBox(
  72.                         height: 10,
  73.                       ),
  74.                       ElevatedButton(
  75.                           onPressed: _formValid ? () {} : null,
  76.                           child: const Text('SALVA')),
  77.                     ],
  78.                   ),
  79.                 ),
  80.               ),
  81.             )
  82.           ])
  83.         ],
  84.       ),
  85.     );
  86.   }
  87.  
  88.   get _errorText {
  89.     final textClient = _clientController.value.text;
  90.     final textObject = _objectController.value.text;
  91.     final textworkToDo = _workTodoController.value.text;
  92.     // Note: you can do your own custom validation here
  93.     // Move this logic this outside the widget for more testable code
  94.     if (textClient.isEmpty && textObject.isEmpty && textworkToDo.isEmpty) {
  95.       return 'Il campo non puo essere vuto';
  96.     }
  97.     /* if (textObject.isEmpty) {
  98.       return 'Il campo non puo essere vuto';
  99.      
  100.     }
  101.     if (textworkToDo.isEmpty) {
  102.       return 'Il campo non puo essere vuto';
  103.     }  */
  104.     else {
  105.       setState(() {
  106.         _formValid = true;
  107.       });
  108.     }
  109.   }
  110. }
  111.  
  112. /*class CustoSimpleTextFieldAutocomplete extends StatelessWidget {
  113.   const CustoSimpleTextFieldAutocomplete({Key? key}) : super(key: key);
  114.  
  115.   @override
  116.   Widget build(BuildContext context) {
  117.     return BlocBuilder<ClientsBloc, ClientsState>(builder: (context, state) {
  118.       return BlocBuilder<ClientsBloc, ClientsState>(builder: (context, state) {
  119.         if (state is ClientsLoading) {
  120.           return const Center(
  121.             child: CircularProgressIndicator(),
  122.           );
  123.         } else {
  124.           final clients = (state as ClientsLoaded).clients;
  125.           return SimpleAutocompleteFormField(
  126.             itemBuilder: (context, _) => Padding(
  127.               padding: EdgeInsets.all(8.0),
  128.              
  129.               child: Column(
  130.                   crossAxisAlignment: CrossAxisAlignment.start,
  131.                   children: [
  132.                     Text(clients.map((e) => e.nameClient).toList().toString(),
  133.                         style: TextStyle(fontWeight: FontWeight.bold)),
  134.                   ]),
  135.             ),
  136.             onSearch: (search) async => clients
  137.                 .where((clients) => clients.nameClient
  138.                     .toLowerCase()
  139.                     .contains(search.toLowerCase()))
  140.                 .toList(),
  141.             itemFromString: (string) {
  142.               final matches = clients.where((clients) =>
  143.                   clients.nameClient
  144.                       .toLowerCase()
  145.                       .contains(clients.nameClient()) ==
  146.                   string.toLowerCase());
  147.               return matches.isEmpty ? null : matches.first;
  148.              
  149.             },
  150.           );
  151.         }
  152.       });
  153.     });
  154.   }
  155.  
  156.  
  157. } */
  158.  
  159. class EasyAutocompleteWidget extends StatelessWidget {
  160.   EasyAutocompleteWidget({
  161.     required TextEditingController controller,
  162.     required errorText,
  163.     required String txtLable,
  164.     Key? key,
  165.   })  : _controller = controller,
  166.         _txtLable = txtLable,
  167.         _errorText = errorText,
  168.         super(key: key);
  169.  
  170.   final TextEditingController _controller;
  171.   final String? _errorText;
  172.   final String _txtLable;
  173.  
  174.   @override
  175.   Widget build(BuildContext context) {
  176.     return BlocBuilder<ClientsBloc, ClientsState>(
  177.       builder: (context, state) {
  178.         if (state is ClientsLoading) {
  179.           return const Center(
  180.             child: CircularProgressIndicator(),
  181.           );
  182.         } else {
  183.           final clients = (state as ClientsLoaded).clients;
  184.           return EasyAutocomplete(
  185.               controller: _controller,
  186.               decoration: InputDecoration(
  187.                   errorText: _errorText,
  188.                   label: Text(_txtLable),
  189.                   contentPadding:
  190.                       const EdgeInsets.symmetric(vertical: 0, horizontal: 10),
  191.                   focusedBorder: OutlineInputBorder(
  192.                       borderRadius: BorderRadius.circular(5),
  193.                       borderSide: BorderSide(
  194.                           color: Theme.of(context).primaryColor,
  195.                           style: BorderStyle.solid)),
  196.                   enabledBorder: OutlineInputBorder(
  197.                       borderRadius: BorderRadius.circular(5),
  198.                       borderSide: BorderSide(
  199.                           color: Theme.of(context).primaryColor,
  200.                           style: BorderStyle.solid))),
  201.               suggestionBuilder: (data) {
  202.                 return Container(
  203.                     margin: const EdgeInsets.all(1),
  204.                     padding: const EdgeInsets.all(5),
  205.                     decoration: BoxDecoration(
  206.                         color: Theme.of(context).primaryColor,
  207.                         borderRadius: BorderRadius.circular(5)),
  208.                     child: Text(data,
  209.                         style: Theme.of(context).textTheme.headline3));
  210.               },
  211.               suggestions: clients.map((e) => e.nameClient).toList(),
  212.               onChanged: (value) {
  213.                 print('onChange Value: $value');
  214.               });
  215.         }
  216.       },
  217.     );
  218.   }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement