【发布时间】:2020-03-29 13:36:11
【问题描述】:
我已尽我所能关注这个问题:Adding new ListTile item on click
虽然它不一定像它可能的那样可重现,但我创建了一些可以编译但没有按预期运行的东西:
import 'package:flutter/material.dart';
void main() => runApp(SeedBank());
class SeedBank extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: Colors.lightGreen,
),
home: Seeds(),
);
}
}
class Seeds extends StatefulWidget {
@override
SeedsState createState() => SeedsState();
}
class SeedsState extends State<Seeds> {
var _seedSection = List<Widget>();
Card seedSectionMethod(String title, String subtitle) {
return new Card(
child: ListTile(
title: Text(
title,
style: TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text(subtitle),
trailing: Icon(Icons.more_vert)),
);
}
void initState() {
super.initState();
_seedSection.add(
seedSectionMethod("Leek", "Bulgaarse Reuzen - Lincoln"),
);
_seedSection.add(
seedSectionMethod("Kale", "Jardin mix"),
);
_seedSection.add(
seedSectionMethod("Sunflower", "Helianthus Red"),
);
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Seed Bank'),
),
body: ListView(
children: this._seedSection,
),
floatingActionButton: FloatingActionButton(
child: Text("+"),
onPressed: () {
setState(() {
_seedSection.add(
seedSectionMethod("New Seed", "With notes"),
);
});
},
backgroundColor: Colors.lightGreenAccent,
),
);
}
}
期望的行为是:
- 在应用启动时,填充了三个带有种子的“卡片”。
- 按下按钮时,会添加一个带有值
"new Seed"和"with notes"的新“卡片”。
我想我错过了一些关于设置状态的基本知识(昨天才开始),但我对下一步该去哪里了解更多。
V2:
使用@andrea689,我可以编译,但又遇到了同样的问题:
class SeedsState extends State<Seeds> {
var _seedSection = List<Widget>();
var _seeds = [
["Leek", "Bulgaarse Reuzen - Lincoln"],
["Kale", "Jardin mix"],
["Sunflower", "Helianthus Red"],
];
Card seedSectionMethod(String title, String subtitle) {
return new Card(
child: ListTile(
title: Text(
title,
style: TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text(subtitle),
trailing: Icon(Icons.more_vert)),
);
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Seed Bank'),
),
body: ListView.builder(
itemCount: _seedSection.length,
itemBuilder: (context, index) {
return seedSectionMethod(_seeds[index][0], _seeds[index][1]);
},
),
floatingActionButton: FloatingActionButton(
child: Text("+"),
onPressed: () {
setState(() {
_seeds.add(["New Seed", "With notes"]);
});
},
backgroundColor: Colors.lightGreenAccent,
),
);
}
}
这是该类的外观吗?它现在不再添加新项目。
最终解决方案
import 'package:flutter/material.dart';
void main() => runApp(SeedBank());
class SeedBank extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: Colors.lightGreen,
),
home: Seeds(),
);
}
}
class Seeds extends StatefulWidget {
@override
SeedsState createState() => SeedsState();
}
class SeedsState extends State<Seeds> {
var _seeds = [
["Leek", "Bulgaarse Reuzen - Lincoln"],
["Kale", "Jardin mix"],
["Sunflower", "Helianthus Red"],
];
Card seedSectionMethod(String title, String subtitle) {
return new Card(
child: ListTile(
title: Text(
title,
style: TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text(subtitle),
trailing: Icon(Icons.more_vert)),
);
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Seed Bank'),
),
body: ListView.builder(
itemCount: _seeds.length,
itemBuilder: (context, index) {
return seedSectionMethod(_seeds[index][0], _seeds[index][1]);
},
),
floatingActionButton: FloatingActionButton(
child: Text("+"),
onPressed: () {
setState(() {
setState(() {
_seeds.add(["New Seed", "With notes"]);
});
});
},
backgroundColor: Colors.lightGreenAccent,
),
);
}
}
【问题讨论】:
标签: user-interface flutter dart state