例如点击删除,删除对应的数据,渲染数据是在data_class.dart中渲染的,在main.dart中获取点击删除的对应内容
main.dart代码:
import 'package:flutter/material.dart'; import './type.dart'; import './data_class.dart'; void main() => runApp(MyApp()); class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { List<Datas> dataList = [ Datas(title: '文章标题111', desc: '文章描述111'), Datas(title: '文章标题222', desc: '文章描述222'), Datas(title: '文章标题333', desc: '文章描述333'), ]; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( backgroundColor: Colors.white, appBar: AppBar( title: Text('案例演示'), centerTitle: true, backgroundColor: Colors.grey[850], ), body: Column( children: dataList .map((item) => CardTemplate(item:item, delete:() { setState(() { dataList.remove(item); }); })) .toList()), ), ); } }
data_class.dart代码:
import 'package:flutter/material.dart'; import 'type.dart'; class CardTemplate extends StatelessWidget{ final Datas item; final Function delete; CardTemplate({this.item,this.delete}); @override Widget build(BuildContext context) { return Card( child: Column( children: <Widget>[ ListTile( leading: Icon(Icons.album), title: Text('${item.title}'), subtitle: Text('${item.desc}'), ), FlatButton.icon( label: Text('删除'), icon:Icon(Icons.delete), onPressed: delete, ), ], ), ); } }
事件传递和传递参数的方法一致。
发表评论
侧栏公告
寄语
譬如朝露博客是一个分享前端知识的网站,联系方式11523518。
热评文章
标签列表
热门文章
友情链接