Placing dropdownbutton selections into a to-do list

51 views Asked by At

I am very new to Flutter/Dart, so please forgive my ignorance and please explain everything to me like I am a five year old.

I have a screen on my app where I would like users to have two options: enter text using a textfield or select an option from a dropdown menu. Whichever they choose will be placed on a to-do list.

However, I can't seem to figure out how to get the selection from the DropdownButton widget to be saved to the to-do list the same way a textfield entry would be.

Below is my code for the screen where new items are entered.

import 'package:provider/provider.dart';
import 'task_data.dart';

class AddTaskScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    String newTaskTitle = '';
    String _dropdownValue = '';


    return Container(
      color: const Color(0xFF757575),
      child: Container(
        decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(20.0), topRight: Radius.circular(20.0)),
        ),
        padding: const EdgeInsets.symmetric(horizontal: 60.0, vertical: 30.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            const Text(
              'Add a Task',
              textAlign: TextAlign.center,
              style: TextStyle(
                color: Colors.blue,
                fontSize: 30.0,
                fontWeight: FontWeight.w500,
              ),
            ),
            TextField(
              autofocus: true,
              textAlign: TextAlign.center,
              style: const TextStyle(
                fontSize: 25.0,
              ),
              onChanged: (newText) {
                newTaskTitle = newText;
              },
            ),
            TaskSelect(),
            const SizedBox(
              height: 20.0,
            ),
            MaterialButton(
              onPressed: () {
                Provider.of<TaskData>(context, listen: false)
                    .addTask(newTaskTitle);
                Navigator.pop(context);
              },
              color: Colors.blue,
              child: const Text(
                'Add',
                style: TextStyle(
                  fontSize: 20.0,
                ),
              ),
              textColor: Colors.white,
              height: 50.0,
            )
          ],
        ),
      ),
    );
  }
}

class TaskSelect extends StatefulWidget {
  const TaskSelect({Key? key}) : super(key: key);

  @override
  State<TaskSelect> createState() => _TaskSelectState();
}

class _TaskSelectState extends State<TaskSelect> {
  String dropdownValue = 'Select a task';
  String newTaskTitle = '';

  @override
  Widget build(BuildContext context) {
    return DropdownButton<String>(
      value: dropdownValue,
      icon: const Icon(Icons.arrow_downward),
      elevation: 16,
      style: const TextStyle(color: Colors.blueAccent),
      underline: Container(
        height: 2,
        color: Colors.blueAccent,
      ),
      onChanged: (String? newValue) {
        setState(() {dropdownValue = newValue!;});
        newTaskTitle = dropdownValue;

      },
      items: <String>[
        'Task 1',
        'Task 2',
        'Task 3',
      ]
          .map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(value),
        );
      }).toList(),
    );
  }
}
1

There are 1 answers

3
Zaid Alazwi On BEST ANSWER

Your problem is how to access the value selected from the other class

you can achieve that in many ways one is to make the widget accept a variable (which is your titleText) and when it's changed in that class (widget) (TaskSelcted) it will change the variable value in the other class (widget)(AddTaskScreen)

class TaskSelect extends StatefulWidget {

//Telling the class you will get a value of  string when ever someone used ue
      String? taskTitle;
      TaskSelect({Key? key, required this.taskTitle}) : super(key: key);
    
      @override
      State<TaskSelect> createState() => _TaskSelectState();
    }

.

//Telling the class here you are being used, this is the value you are promised to get. you can change its value as i tell you
TaskSelect(newTaskTitle),
            const SizedBox(
              height: 20.0,
            ),

.

onChanged: (String? newValue) {
        setState(() {dropdownValue = newValue!;});

//Change the variable you took to this value
        widget.textTitle = dropdownValue;
}