我試圖做到這一點,所以當你按下搜索時,它會創建一個串列。它可以作業,但要讓它作業,我必須單擊該按鈕,然后重建應用程式以使其出現。我正在查看其他一些帖子,但我找不到任何有用的東西。要查看的主要部分是我有一個添加 listtile 的功能。我有一個按下按鈕來創建瓷磚。我在底部有容器的子項作為創建的串列塊串列。
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
List<Widget> _listOfWidgets = [];
@override
Widget build(BuildContext context) {
_addItemToList() {
List<Widget> tempList =
_listOfWidgets; // defining a new temporary list which will be equal to our other list
tempList.add(ListTile(
key: UniqueKey(),
leading: Icon(Icons.list),
trailing: FlatButton(
onPressed: () async {},
//Download Link
child: Text(
"Download",
style: TextStyle(color: Colors.green, fontSize: 15),
),
),
title: Text("")));
this.setState(() {
_listOfWidgets =
tempList; // this will trigger a rebuild of the ENTIRE widget, therefore adding our new item to the list!
});
}
return MaterialApp(
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
themeMode: currentTheme.currentTheme(),
home: Scaffold(
appBar: AppBar(
actions: [
IconButton(
onPressed: () {
setState(() {
currentTheme.switchTheme();
});
},
icon: Icon(Icons.wb_sunny),
),
IconButton(
onPressed: () async {
await FirebaseAuth.instance.signOut();
},
icon: Icon(Icons.exit_to_app),
),
],
backgroundColor: Colors.blue,
title: Text("Home"),
),
body: ListView(
children: [
ListTile(
leading: SizedBox(
width: 300,
child: TextField(
controller: search,
decoration: InputDecoration(
labelText: "Enter Manga Name",
),
),
),
trailing: ElevatedButton(
onPressed: () async {
_addItemToList();
},
child: Text("Search")),
),
Container(
margin: EdgeInsets.all(15),
width: 100,
height: 515,
color: Colors.black12,
child: ListView(
children: _listOfWidgets,
))
],
)));
}
}
uj5u.com熱心網友回復:
嘗試添加以下代碼
如果你更新你需要的狀態 setState()
更好的狀態管理方法是使用 BLoC 或 Provider 包。
...
onPressed: () {
setState(() {
_addItemToList();
});
},
...
uj5u.com熱心網友回復:
經過一番折騰后想通了。對我來說,解決方法是將密鑰:UniqueKey() 添加到我的 ListView。我將鍵添加到我的 ListTiles 而不是實際的 ListView。
uj5u.com熱心網友回復:
onPressed: () {
setState(() {
_addItemToList();
});
},
問題解決方案是:
List<Widget> tempList = <Widget>[];
_addItemToList() {
tempList.addAll(
_listOfWidgets); // defining a new temporary list which will be equal to our other list
tempList.add(ListTile(
key: UniqueKey(),
leading: Icon(Icons.list),
trailing: FlatButton(
onPressed: () async {},
//Download Link
child: Text(
"Download",
style: TextStyle(color: Colors.green, fontSize: 15),
),
),
title: Text("")));
this.setState(() {
_listOfWidgets =
tempList; // this will trigger a rebuild of the ENTIRE widget, therefore adding our new item to the list!
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/408811.html
標籤:
下一篇:Dart中的列舉有比較運算子嗎?
