gpt4 book ai didi

list - 如何刷新 ListView.builder flutter

转载 作者:行者123 更新时间:2023-12-03 03:15:38 25 4
gpt4 key购买 nike

将项目添加到列表时如何刷新我的 ListView.builder?

ListView.builder(
itemCount: propList.length,
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemBuilder: (context,index)=> Text(propList[index]),
)

我的按钮是
FlatButton(onPressed: (){propList.add('text');}, child: Text('add'))

最佳答案

假设您已扩展 StatefulWidget ,您必须调用 setState每次你改变状态。前任:

setState(() { propList.add('text'); });

这是一个简单的例子:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) => MaterialApp(home: MyList());
}

class MyList extends StatefulWidget {
@override
_MyListState createState() => _MyListState();
}

class _MyListState extends State<MyList> {
List<String> propList = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('My List'),
),
body: ListView.builder(
itemCount: propList.length,
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemBuilder: (context,index)=> Text(propList[index]),
),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => propList.add('text')),
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}

关于list - 如何刷新 ListView.builder flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59927528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com