Advertisement
harmonyV

TextFieldSection

Jul 8th, 2024
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.00 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:habit_tracker/models/habit.dart';
  3. import 'package:habit_tracker/services/providers/edit_habit_provider.dart';
  4. import 'package:provider/provider.dart';
  5.  
  6. final formkey = GlobalKey<FormState>();
  7.  
  8. class MyTextFieldSection extends StatelessWidget {
  9.   final String title;
  10.   final bool limitation;
  11.   final TextEditingController textEditingController;
  12.   final int maxLines;
  13.   final int maxLength;
  14.  
  15.   const MyTextFieldSection({
  16.     super.key,
  17.     required this.title,
  18.     required this.limitation,
  19.     required this.textEditingController,
  20.     required this.maxLines,
  21.     required this.maxLength,
  22.   });
  23.  
  24.   @override
  25.   Widget build(BuildContext context) {
  26.     TextTheme textTheme = Theme.of(context).textTheme;
  27.     ColorScheme colorScheme = Theme.of(context).colorScheme;
  28.  
  29.     EditHabitProvider editHabitProvider =
  30.         Provider.of<EditHabitProvider>(context);
  31.     bool edetingMode = editHabitProvider.editingMode;
  32.     Habit? habitToEdit = editHabitProvider.habitToEdit;
  33.  
  34.     edetingMode && textEditingController.text.isEmpty
  35.         ? textEditingController.text =
  36.             limitation ? habitToEdit!.name : habitToEdit!.details ?? ""
  37.         : null;
  38.  
  39.     TextFormField textFormField = TextFormField(
  40.       validator: (inputTitle) =>
  41.           inputTitle!.isEmpty ? "please provide a Title" : null,
  42.       autovalidateMode: AutovalidateMode.onUnfocus,
  43.       controller: textEditingController,
  44.       maxLines: maxLines,
  45.       maxLength: maxLength,
  46.       decoration: InputDecoration(
  47.         // button to clear the TextField
  48.         suffixIcon: IconButton(
  49.           onPressed: textEditingController.clear,
  50.           icon: const Icon(Icons.close_rounded),
  51.         ),
  52.       ),
  53.     );
  54.  
  55.     TextField textField = TextField(
  56.       controller: textEditingController,
  57.       maxLines: maxLines,
  58.       maxLength: maxLength,
  59.       decoration: InputDecoration(
  60.         // button to clear the TextField
  61.         suffixIcon: IconButton(
  62.           onPressed: textEditingController.clear,
  63.           icon: const Icon(Icons.close_rounded),
  64.         ),
  65.       ),
  66.     );
  67.  
  68.     return Column(
  69.       crossAxisAlignment: CrossAxisAlignment.start,
  70.       children: [
  71.         // title of the section
  72.         Text(
  73.           title,
  74.           style: textTheme.bodyMedium!.copyWith(
  75.             color: colorScheme.onSecondaryContainer,
  76.           ),
  77.         ),
  78.         const SizedBox(
  79.           height: 16,
  80.         ),
  81.  
  82.         // TextField wraped in a container
  83.         Container(
  84.           decoration: BoxDecoration(
  85.             color: colorScheme.primaryContainer,
  86.             borderRadius: BorderRadius.circular(16),
  87.             border: Border.all(
  88.               color: colorScheme.outline,
  89.             ),
  90.           ),
  91.           child: Padding(
  92.             padding: const EdgeInsets.all(8.0),
  93.             child: limitation
  94.                 ? Form(key: formkey, child: textFormField)
  95.                 : textField,
  96.           ),
  97.         ),
  98.       ],
  99.     );
  100.   }
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement