Advertisement
htamayo

Flutter ToDo pop a form

Jan 19th, 2023 (edited)
1,516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.83 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:fluttertodo/models/task.dart';
  3.  
  4. class AddTask extends StatefulWidget {
  5.   final Function(Task) addTask;
  6.   const AddTask(this.addTask, {super.key});
  7.  
  8.   @override
  9.   _AddTaskState createState() {
  10.     return _AddTaskState();
  11.   }
  12. }
  13.  
  14. class _AddTaskState extends State<AddTask> {
  15.   @override
  16.   Widget build(BuildContext context) {
  17.     Widget buildTextField(String hint, TextEditingController controller) {
  18.       return Container(
  19.         margin: const EdgeInsets.all(4),
  20.         child: TextField(
  21.           decoration: InputDecoration(
  22.             labelText: hint,
  23.             border: const OutlineInputBorder(
  24.               borderSide: BorderSide(
  25.                 color: Colors.black38,
  26.               ),
  27.             ),
  28.           ),
  29.           controller: controller,
  30.         ),
  31.       );
  32.     }
  33.  
  34.     var titleController = TextEditingController();
  35.     var bodyController = TextEditingController();
  36.  
  37.     return Container(
  38.       padding: const EdgeInsets.all(8),
  39.       height: 350,
  40.       width: 400,
  41.       child: SingleChildScrollView(
  42.         child: Column(
  43.           children: [
  44.             const Text(
  45.               'Add New Task',
  46.               style: TextStyle(
  47.                 fontWeight: FontWeight.bold,
  48.                 fontSize: 32,
  49.                 color: Colors.blueGrey,
  50.               ),
  51.             ),
  52.             buildTextField('Title', titleController),
  53.             buildTextField('Body', bodyController),
  54.             ElevatedButton(
  55.               onPressed: () {
  56.                 final task = Task(titleController.text, bodyController.text);
  57.                 widget.addTask(task);
  58.                 Navigator.of(context).pop(task);
  59.               },
  60.               child: const Text('Add Task'),
  61.             ),
  62.           ],
  63.         ),
  64.       ),
  65.     );
  66.   }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement