android - 当用户单击 appBar 中的“+”号时,如何向 ListView 添加卡片?
问题描述
我正在开发一个颤振应用程序。现在,我没有太多,但我的问题是,如何通过单击 appBar 中的加号图标将卡片添加到列表(ListView)?代码:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Todo'),
centerTitle: true,
actions: <Widget>[
IconButton(
icon: Icon(Icons.add),
tooltip: 'Add a todo card',
onPressed: () {`
},
),
],
),
那不是整个班级,而是您需要看到的东西。我有我的 appBar 和我的 IconButton。这就是我的一张卡片的外观,它是一个占位符:
children: <Widget>[
Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const ListTile(
leading: Icon(Icons.album),
title: Text('The Enchanted Nightingale'),
subtitle:
Text('Music by Julie Gable. Lyrics by Sidney Stein.'),
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
TextButton(
child: const Text('BUY TICKETS'),
onPressed: () {},
),
const SizedBox(width: 8),
TextButton(
child: const Text('LISTEN'),
onPressed: () {},
),
const SizedBox(width: 8),
],
),
],
),
),
我想将这种卡片(自定义)添加到 ListView(正文)。
解决方案
实现这一目标的步骤:
- 创建一个无状态小部件以将您的提取
TodoCard
为单独的小部件。 - 创建一个小部件列表以将您的
TodoCard
小部件保存在TodoScreen
. - 在其中添加一个
TodoCard
,initState
以便在TodoScreen
构建后呈现一个 - 当按下添加待办事项卡时,将 a 添加
TodoCard
到创建的小部件列表并调用setState
以触发重建。 - 使用扩展运算符渲染
TodoCard
小部件列表。ListView
我以您的代码为例添加了一个演示:
TODOCARD 小工具
// create a stateless widget to extract your TodoCard
class TodoCard extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const ListTile(
leading: Icon(Icons.album),
title: Text('The Enchanted Nightingale'),
subtitle: Text('Music by Julie Gable. Lyrics by Sidney Stein.'),
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
TextButton(
child: const Text('BUY TICKETS'),
onPressed: () {},
),
const SizedBox(width: 8),
TextButton(
child: const Text('LISTEN'),
onPressed: () {},
),
const SizedBox(width: 8),
],
),
],
),
);
}
}
待办事项屏幕
class TodoScreen extends StatefulWidget {
@override
_TodoScreenState createState() => _TodoScreenState();
}
class _TodoScreenState extends State<TodoScreen> {
// create a list of widget to hold your TodoCard widgets
List<Widget> _allCards = [];
@override
void initState() {
super.initState();
// add a single TodoCard so one will be rendered once the page is built
_allCards.add(TodoCard());
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Todo'),
centerTitle: true,
actions: <Widget>[
IconButton(
icon: Icon(Icons.add),
tooltip: 'Add a todo card',
onPressed: () {
// when the add todo card is pressed, add a TodoCard to the list of widgets created and call setstate to trigger a rebuild
setState(() {
_allCards.add(TodoCard());
});
},
),
],
),
body: ListView(
children: [
// reder the list of TodoCard widgets using the spread operator
..._allCards,
],
),
);
}
}
推荐阅读
- html - Rails:CSS未在检查器中显示且未应用
- tensorflow - TensorBoard 2.0.0 不更新火车标量
- markdown - R markdown 文本和代码中的双空格问题
- javascript - Magento 2 中未定义 jQuery
- javascript - 如何将几个 HTML 文件加载为“文档”,然后使用 Javascript 操作它们?
- python - python字典参数到带有烧瓶模板的JS字典
- angular - Angular 5:如何在不同的组件中使用一个表单组?
- packet - 一旦满足条件,我想在连续嗅探时停止数据包捕获
- angular - Angular7在运行下一个函数之前接收API数据
- javascript - 评估问题的顺序反应本机与浏览器