首页 > 解决方案 > 从列表视图构建器中删除某些内容后如何刷新它?

问题描述

我已经设置了一个列表视图构建器,它显示日记帐分录日期,您可以删除它们,但每次我删除它们时,我都必须返回并再次打开页面以刷新它。当我删除一个项目时,有没有办法自动刷新它?

class ListOfEntries extends StatelessWidget{

  @override
  Widget build(BuildContext context) {
   return Scaffold(
     appBar: AppBar(
       title: Text("Your entries"),
       toolbarHeight: 50,
     ),
     body: FutureBuilder<List>(
       future: Database_().entries(),
       builder: (context, snapshot) {
         return snapshot.hasData ?
         new ListView.builder(
           shrinkWrap: true,
           scrollDirection: Axis.vertical,
           padding: const EdgeInsets.all(10.0),
           itemCount: snapshot.data.length,
           itemBuilder: (context, i) {
             return Container(
               decoration: BoxDecoration(
                   color: Colors.grey[900],
                   boxShadow: [BoxShadow(
                     color: Colors.grey,
                     blurRadius: 3.0,
                     spreadRadius: 2,
                   )],
                   borderRadius: BorderRadius.only(
                       topLeft: Radius.zero,
                       topRight: Radius.zero,
                       bottomLeft: Radius.circular(40),
                       bottomRight: Radius.circular(40)
                   )
               ),
               child: Padding(
                 padding: EdgeInsets.all(30),
                 child: Center(
                   child: Column(
                     children: [
                       Container(
                         child: Padding(
                           child: Text(
                             "${snapshot.data[snapshot.data.length - i - 1].date}",
                             style: TextStyle(
                               fontSize: 17
                             ),
                           ),
                           padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
                         ),
                         decoration: BoxDecoration(
                             color: Colors.purple[900],
                             borderRadius: BorderRadius.all(
                               Radius.circular(20)
                             ),
                         ),
                       ),
                       Text(""),
                       Text(
                         "${snapshot.data[snapshot.data.length - i - 1].entry}",
                         style: TextStyle(fontSize: 23),
                       ),
                       Text(""),
                       TextButton(
                         onPressed: ()=>{
                           Delete(snapshot.data[snapshot.data.length - i - 1].entry),
                         },
                           child: Text("Delete")
                       )
                     ],
                   ),
                 ),
               ),
             );
           },
         )
             : Center(
           child: Column(
           crossAxisAlignment: CrossAxisAlignment.center,
             mainAxisAlignment: MainAxisAlignment.center,
             children: [
               CircularProgressIndicator(),
               Text(""),
               Text("No entries yet:("),
             ],

           )
         );
       }
    ),

    );

  }


}

Delete(String entry_)async{
  await Database_().deleteEntry(entry_);

}

我正在使用可能并不理想的无状态小部件。

标签: flutterdart

解决方案


将其更改为StatefulWidget并使用setState函数更新您的列表


推荐阅读