生成函数返回空值

时间:2019-09-13 03:46:25

标签: flutter

我有一个应用程序,当按下Card的FAB时,它会产生一个包裹在GestureDetector中的新Scaffold。该应用程序运行正常,但我想实现删除卡功能,当我添加该功能时,该应用程序无法识别return函数中的build语句。我感觉自己似乎缺少一些明显的东西,但是由于我是新手,所以我一直在努力寻找问题所在。

整个代码:

class _Starting_screenState extends State<Starting_screen> {
  int _count = 1;



  @override
  Widget build(BuildContext context) {
    {
      List<Widget> cardList = new List.generate(
          _count, (int i) => new createCard());
      SystemChrome.setEnabledSystemUIOverlays([]);

      _deleteNoDo(int id, int index) async {
        debugPrint("Deleted Item!");
        setState(() {
          cardList.removeAt(index);
        });


        void addItems() async {
          setState(() {
            cardList.insert(0, new GestureDetector(
              onTap: () async {
                await Navigator.push(context, MaterialPageRoute(
                  builder: (context) =>
                      TodoList(), // this just navigates to another screen ; not important in this question
                )
                );
              },
              child: Card(
                  child: ListTile(
                      title: Text("project 1"),
                      trailing: new Listener(
                        key: new Key(UniqueKey().toString()),
                        child: new Icon(Icons.remove_circle,
                          color: Colors.redAccent,),
                        onPointerDown: (pointerEvent) => _deleteNoDo(id, index),
                      ),
                      subtitle: whitefontstylemont(text: "project 1",
                        size: 20,)) //this is just a custom TextStyle
              ),
            ));
          });
        }

        return Scaffold(
            floatingActionButton: FloatingActionButton(
              onPressed: () async {
                setState(() {
                  _count += 1;
                });
              },
              heroTag: "btn2",
              child: Icon(Icons.add, color: Color(whitecolor),),
              backgroundColor: Color(redcolor),),
            body: CustomScrollView(
                slivers: <Widget>[
                  SliverAppBar(
                    pinned: true,
                    flexibleSpace: FlexibleSpaceBar(
                    ),
                    actions: <Widget>[
                      Expanded(
                        child: Padding(
                          padding: const EdgeInsets.only(top: 20, right: 10),
                          child: whitefontstyle(
                            text: "Remaining tasks for today - ${cardList
                                .length}", size: 20,),
                        ),
                      ),
                    ],
                  ),
                  SliverGrid(
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 2
                      ),
                      delegate: new SliverChildBuilderDelegate((context,
                          index) {
                        return cardList[index];
                      },
                          childCount: cardList.length
                      )
                  ),
                ]
            )
        );
      }
    }
  }
}

删除功能:

 _deleteNoDo(int id, int index) async {
        debugPrint("Deleted Item!");
        setState(() {
          cardList.removeAt(index);
        });

添加卡片的功能:

  void addItems() async {
          setState(() {
            cardList.insert(0, new GestureDetector(
              onTap: () async {
                await Navigator.push(context, MaterialPageRoute(
                  builder: (context) =>
                      TodoList(), // this just navigates to another screen ; not important in this question
                )
                );
              },
              child: Card(
                  child: ListTile(
                      title: Text("project 1"),
                      trailing: new Listener(
                        key: new Key(UniqueKey().toString()),
                        child: new Icon(Icons.remove_circle,
                          color: Colors.redAccent,),
                        onPointerDown: (pointerEvent) => _deleteNoDo(id, index),
                      ),
                      subtitle: whitefontstylemont(text: "project 1", size: 20,)) //this is just a custom TextStyle
              ),
            ));
          });
        }

在列表中显示卡片的代码

SliverGrid(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
         crossAxisCount: 2
          ),
         delegate: new SliverChildBuilderDelegate((context, index) {
                        return cardList[index]; // this is where the cards are displayed in a list
            },
           childCount: cardList.length
)
)

0 个答案:

没有答案