具有Futurebuilder和Listview.Builder的Sqlite-添加新项后更新ListView

时间:2020-07-20 10:44:06

标签: android sqlite flutter listview

在我的应用程序的一部分中,我让用户创建了Todos。创建的待办事项存储在TodoPage类中,而用户创建的新待办事项将在NewTodoPage中处理。用户打开NewTodoPage并输入待办事项信息后,我将验证所有内容并创建一个sqlite条目。从sqlite方面,一切正常。创建待办事项后,直接显示在TodoPage列表视图中,重新打开应用程序或更改标签后(TodoPage是我的底部导航栏中的“标签”。

我的问题是:

在创建待办事项后如何立即更新listview构建器?我的代码:

TodoPage

class TodoPage extends StatefulWidget {
  @override
  _TodoPageState createState() => _TodoPageState();
}

class _TodoPageState extends State<TodoPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
          padding: const EdgeInsets.only(top: 60.0, left: 8.0, right: 8.0),
          child: FutureBuilder(
            future: DB.query('todo'),
            builder:
                (BuildContext context, AsyncSnapshot<List<Todo>> snapshot) {
              if (snapshot.hasData) {
                if (snapshot.data != null) {
                  return ListView.builder(
                      itemCount: snapshot.data.length,
                      itemBuilder: (BuildContext context, int position) {
                        Todo todo = snapshot.data[position];
                        return Card(
                          child: ListTile(
                            title: Text(todo.title),
                          ),
                        );
                      });
                }
              } else {
                return Center(
                  child: Text('Noch keine Aufgaben erstellt'),
                );
              }
            },
          )),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.push(
              context, MaterialPageRoute(builder: (context) => NewTodoPage()));
        },
        child: Icon(
          Icons.add,
          size: 35,
        ),
      ),
    );
  }
}

NewTodoPage(目前是一个大类,我只向您展示此用途的主要部分)

class NewTodoPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _NewTodoPageState();
}

class _NewTodoPageState extends State<NewTodoPage> {
  final _formKey = GlobalKey<FormState>();
  final TextEditingController _titleController = TextEditingController();
  final TextEditingController _infoController = TextEditingController();
  DateTime _selectedDate;
  bool _deadlinePicked = false;
  bool _imageSelected = false;
  File _selectedImage;
  final picker = ImagePicker();

...

CircleAvatar(
                              backgroundColor: Colors.green,
                              radius: 35,
                              child: IconButton(
                                padding: EdgeInsets.zero,
                                icon: Icon(Icons.done,
                                    size: 30, color: Colors.white),
                                onPressed: () async {
                                  //TODO Save a Todo Object to db
                                  //title is mandatory for creating a task, the rest can be filled out
                                  if(_formKey.currentState.validate()){
                                    Todo todo = Todo(
                                      title: _titleController.text, 
                                      info: _infoController.text.isNotEmpty ? _infoController.text : null, 
                                      deadline: _selectedDate != null ? _getPrettyDate(_selectedDate.toIso8601String()) : null, 
                                      additional: _selectedImage != null ? base64Encode(_selectedImage.readAsBytesSync()) : null,
                                      isDone: false
                                      );
                                    await DB.insert('todo', todo);
                                    Navigator.pop(context);                               
                                  }
                                },
                              ),
                            ),

任何帮助将不胜感激! 谢谢

0 个答案:

没有答案