Flutter автоматически - PullRequest
       5

Flutter автоматически

0 голосов
/ 04 августа 2020

Я хочу убрать кнопку «Назад» с новой страницы в навигаторе, чтобы у меня была для этого собственная кнопка custon. Однако даже после установки для automaticImplyLeading значения false я получаю кнопку «Назад». Как я могу это исправить? Если пользователь нажмет кнопку «Назад» на своем устройстве, вернется ли страница go к предыдущей? Если да, то как фи c это? Это мой код: -


import 'package:flutter/material.dart';

class toDoList extends StatefulWidget
{
    bool data = false;
    @override
    createState() 
    {
        return new toDoListState();
    }
}

class toDoListState extends State<toDoList>
{
  List<String> tasks = [];
  List<String> completedTasks = [];
  List<String> descriptions = [];
  List<bool> importance = [];
  List<String> time2completion = [];var _chosenValue;
  
    @override
    Widget build(BuildContext context)
    {
        return Scaffold
        (
            body: buildToDoList(),
            floatingActionButton: new FloatingActionButton
            (
                onPressed: addToDoItemScreen, 
                tooltip: 'Add Task',
                child: new Icon(Icons.add),
            ),
        );
    }

    Widget buildToDoList()
    {
        return new ListView.builder
        (
            itemBuilder: (context, index)
            {
                if(index < tasks.length)
                {
                    return row(tasks[index], descriptions[index], index);
                }
            },
        );
    }

    Widget row(String task, String description, int index)
    {                  
        return Dismissible(
        key: UniqueKey(),
        background: Container(color: Colors.red, child: Align(alignment: Alignment.center, child: Text('DELETE', textAlign: TextAlign.center, style: TextStyle(color: Colors.white, fontSize: 18),))),
        direction: DismissDirection.horizontal,
        onDismissed: (direction) {
        setState(() {
          tasks.removeAt(index);
          if(completedTasks.contains(task))
          {
              completedTasks.removeAt(index);
          }
          descriptions.removeAt(index);
          importance.removeAt(index);
        });
          Scaffold.of(context).showSnackBar(SnackBar(content: Text(task+" dismissed")));
        },
        child: CheckboxListTile(
          controlAffinity: ListTileControlAffinity.leading,
          title: Text(task, style: (completedTasks.contains(task)) ? TextStyle(decoration: TextDecoration.lineThrough) : TextStyle(),),
          subtitle: Text(descriptions[index]),
          value: completedTasks.contains(task),
          onChanged: (bool value) {
           setState(() {
              if(!completedTasks.contains(task))
              {
                  completedTasks.add(task);
              }
              else
              {
                  completedTasks.remove(task);
              }
           });
          },
        ));
    }
  
  void addToDoItemScreen() {
    int index = tasks.length;
    while (importance.length > tasks.length) {
      importance.removeLast();
    }
    importance.add(false);
    
    Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
      return StatefulBuilder(builder: (context, setState) { // this is new
                return new Scaffold(
                    appBar: new AppBar(automaticallyImplyLeading: false, title: new Text('Add a new task')),
                    body: Form(
                      child: Column(
                        children: <Widget>[
                          TextField(
                            autofocus: true,
                            onSubmitted: (name) {
                              addToDoItem(name);
                              //Navigator.pop(context); // Close the add todo screen
                            },
                            decoration: new InputDecoration(
                                hintText: 'Enter something to do...',
                                contentPadding: const EdgeInsets.all(20.0),
                                border: OutlineInputBorder()),
                          ),
                          TextField(
                            //autofocus: true,
                            //enabled: descriptions.length > desc,
                            onSubmitted: (val) {
                              addDescription(val, index);
                            },
                            decoration: new InputDecoration(
                                hintText: 'Enter a task decription...',
                                contentPadding: const EdgeInsets.all(20.0),
                                border: OutlineInputBorder()),
                          ),
                          Row(
                            children: <Widget> [
                              Switch(
                              value: importance[index],
                              onChanged: (val) {
                                setState(() {
                                });
                                impTask(index);
                              },
                            ),
                            Text('Important Task', style: TextStyle(fontSize: 18)),
                            ],
                          ),
                          DropdownButton<String> 
                          (
                              value: _chosenValue,
                              items: <String>['none', '30 minute', '1 hour', '12 hours', '1 day', 'custom'].map<DropdownMenuItem<String>>((String value) {
                                  return DropdownMenuItem<String>(
                                  value: value,
                                  child: Text(value),
                          );
                            }).toList(),
                        onChanged: (String value) {
                          setState(() {
                            _chosenValue = value;
                        });
                      }),
                  RaisedButton(onPressed: () => Navigator.pop(context), child: Text('DONE', style: TextStyle(fontSize: 20)),)
                ],
              ),
            ));
      });
    }));
  }

    void addToDoItem(String task)
    {
        setState(() {
          tasks.add(task);
          descriptions.add("No description");
        });
    }

    void addDescription(String desc, int index)
    {
        setState(() {
          descriptions[index] = desc;
        });
    }

    void impTask(int index)
    {
        setState(() {
          if(importance[index])
          {
            importance[index] = false;
          }
          else 
          {
            importance[index] = true;
          }
        });
    }
}

1 Ответ

0 голосов
/ 04 августа 2020

Вы можете обернуть весь каркас виджетом WillPopScope

        WillPopScope(
           child:Scaffold(),onWillPop:(){
        } // Add back button logic here
),
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...