4

I have been working on a to-do list app in Flutter but in the process, I realized that the add button was not working when it was supposed to add a new task. I don't know why it doesn't work, because I am very new to Flutter. Why is this problem occurring!?

main.dart

  List<String> _toDoItems = [];

  void _addToDoItem(String task) {
    if(task.length > 0) {
      setState(() {  
        _toDoItems.add(task);
      });
    }
  }
  Expanded(
    flex: 8,
    child: TextField(
      autofocus: true,
      onSubmitted: (val) {
        _addToDoItem(val);
      },
    ),
  ),
  Expanded(
    flex: 2,
      child: RaisedButton(
        child: Text('ADD'),
          onPressed: () => _addToDoItem,
      ),
  ),

3 Answers 3

7

The RaisedButton widget was deprecated in Flutter 2.0 and replaced with the ElevatedButton widget. It's possible that you're using a newer version of Flutter where RaisedButton is no longer available. Try using ElevatedButton instead.

Sign up to request clarification or add additional context in comments.

Comments

2

You can copy paste run full code below
You can use TextEditingController and pass controller.text to _addToDoItem
code snippet

TextEditingController _controller = TextEditingController();

Expanded(
      flex: 8,
      child: TextField(
        controller: _controller,
        
Expanded(
      flex: 2,
      child: RaisedButton(
        child: Text('ADD'),
        onPressed: () => _addToDoItem(_controller.text),    

enter image description here

working demo

full code

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  List<String> _toDoItems = [];
  TextEditingController _controller = TextEditingController();

  void _addToDoItem(String task) {
    if (task.length > 0) {
      setState(() {
        _toDoItems.add(task);
      });
    }
  }

  Widget _buildToDoItem(String toDoText) {
    return ListTile(title: Text(toDoText));
  }

  Widget _buildToDoList() {
    return Expanded(
      child: ListView.builder(
        itemBuilder: (context, index) {
          if (index < _toDoItems.length) {
            return _buildToDoItem(_toDoItems[index]);
          }
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: PreferredSize(
            preferredSize: Size.fromHeight(50),
            child: AppBar(
              centerTitle: true,
              backgroundColor: Colors.red,
              title: Text(
                'To Do List',
                style: TextStyle(
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
                ),
              ),
            )),
        backgroundColor: Colors.white,
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Container(
                margin: EdgeInsets.all(22),
                child: Row(
                  children: [
                    Expanded(
                      flex: 8,
                      child: TextField(
                        controller: _controller,
                        autofocus: true,
                        onSubmitted: (val) {
                          _addToDoItem(val);
                        },
                        decoration: InputDecoration(
                          hintText: 'Add a tak here...',
                          enabledBorder: OutlineInputBorder(
                            borderRadius:
                                BorderRadius.all(Radius.circular(12.0)),
                            borderSide: BorderSide(color: Colors.red, width: 2),
                          ),
                          focusedBorder: OutlineInputBorder(
                            borderRadius:
                                BorderRadius.all(Radius.circular(12.0)),
                            borderSide:
                                BorderSide(color: Colors.red, width: 1.5),
                          ),
                        ),
                      ),
                    ),
                    Expanded(
                      flex: 2,
                      child: RaisedButton(
                        child: Text('ADD'),
                        onPressed: () => _addToDoItem(_controller.text),
                      ),
                    )
                  ],
                )),
            _buildToDoList(),
          ],
        ),
      ),
    );
  }
}

Comments

0

You're not adding a parameter..

Expanded(
    flex: 2,
      child: RaisedButton(
        child: Text('ADD'),
          onPressed: () => {
           _addToDoItem(something_here),
        },
      ),
  ),

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.