我已尽我所能回答以下问题: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"
。我希望我错过了一些有关设置状态的基本知识(仅从昨天开始),但是我对下一步该去哪里了解更多。
我可以使用@ 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,
),
);
}
}
答案 0 :(得分:2)
由于_seedSection
被修改,因此您必须使用ListView.builder
ListView.builder(
itemCount: _seedSection.length,
itemBuilder: (context, index) {
return _seedSection[index];
},
),
此外,最好只将值存储在状态中并将小部件放在itemBuilder
中。
var _seeds = [
["Leek", "Bulgaarse Reuzen - Lincoln"],
["Kale", "Jardin mix"],
["Sunflower", "Helianthus Red"],
];
ListView.builder(
itemCount: _seeds.length,
itemBuilder: (context, index) {
return seedSectionMethod(_seeds[index][0], _seeds[index][1]);
},
),
setState(() {
_seeds.add(["New Seed", "With notes"]);
});
答案 1 :(得分:2)
尝试这样。
body: ListView.builder(
itemCount: this._seedSection.length,
itemBuilder: (context, index) => this._seedSection[index],
),
此处正在演示。 https://dartpad.dev/43a5b08327ed0f64fd084947873a3e52
答案 2 :(得分:1)
尝试添加此内容:
body: ListView(
children: this._seedSection.map((data) {
return data;
}).toList(),
),