Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:habit_tracker/models/habit.dart';
- import 'package:habit_tracker/services/providers/edit_habit_provider.dart';
- import 'package:provider/provider.dart';
- final formkey = GlobalKey<FormState>();
- class MyTextFieldSection extends StatelessWidget {
- final String title;
- final bool limitation;
- final TextEditingController textEditingController;
- final int maxLines;
- final int maxLength;
- const MyTextFieldSection({
- super.key,
- required this.title,
- required this.limitation,
- required this.textEditingController,
- required this.maxLines,
- required this.maxLength,
- });
- @override
- Widget build(BuildContext context) {
- TextTheme textTheme = Theme.of(context).textTheme;
- ColorScheme colorScheme = Theme.of(context).colorScheme;
- EditHabitProvider editHabitProvider =
- Provider.of<EditHabitProvider>(context);
- bool edetingMode = editHabitProvider.editingMode;
- Habit? habitToEdit = editHabitProvider.habitToEdit;
- edetingMode && textEditingController.text.isEmpty
- ? textEditingController.text =
- limitation ? habitToEdit!.name : habitToEdit!.details ?? ""
- : null;
- TextFormField textFormField = TextFormField(
- validator: (inputTitle) =>
- inputTitle!.isEmpty ? "please provide a Title" : null,
- autovalidateMode: AutovalidateMode.onUnfocus,
- controller: textEditingController,
- maxLines: maxLines,
- maxLength: maxLength,
- decoration: InputDecoration(
- // button to clear the TextField
- suffixIcon: IconButton(
- onPressed: textEditingController.clear,
- icon: const Icon(Icons.close_rounded),
- ),
- ),
- );
- TextField textField = TextField(
- controller: textEditingController,
- maxLines: maxLines,
- maxLength: maxLength,
- decoration: InputDecoration(
- // button to clear the TextField
- suffixIcon: IconButton(
- onPressed: textEditingController.clear,
- icon: const Icon(Icons.close_rounded),
- ),
- ),
- );
- return Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- // title of the section
- Text(
- title,
- style: textTheme.bodyMedium!.copyWith(
- color: colorScheme.onSecondaryContainer,
- ),
- ),
- const SizedBox(
- height: 16,
- ),
- // TextField wraped in a container
- Container(
- decoration: BoxDecoration(
- color: colorScheme.primaryContainer,
- borderRadius: BorderRadius.circular(16),
- border: Border.all(
- color: colorScheme.outline,
- ),
- ),
- child: Padding(
- padding: const EdgeInsets.all(8.0),
- child: limitation
- ? Form(key: formkey, child: textFormField)
- : textField,
- ),
- ),
- ],
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement