목록보기에 문제가 있습니다. 내 ListView 항목에서 개별 항목에 대한 작업을 수행하는 버튼을 제공하고 싶습니다. 그러나 내가 그들에게 전달하는 이벤트 핸들러는 예기치 않게 반복적으로 호출됩니다. 그러나 나는 아이의 버튼이 호출 될 때만 호출되기를 기대합니다. 내가 뭘 잘못 했어?
import 'package:flutter/material.dart';
import 'package:horaires_ratp/model/Database.dart';
class StationsList extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _StationsListState();
}
}
class _StationsListState extends State<StationsList> {
List<Station> _stations = List<Station>();
_StationsListState() {
Station().select().toList().then((value) {
setState(() {
_stations = value;
});
});
}
void _handleStationDelete(Station station){
//station.delete(true);
print("called");
Station().select().toList().then((value) {
setState(() {
_stations = value;
});
});
}
@override
Widget build(BuildContext context) {
return Container(
child: ListView.builder(
itemCount: _stations.length,
itemBuilder: (context, index) {
return StationComponent(_stations[index], _handleStationDelete);
}));
}
}
class StationComponent extends StatelessWidget {
final Station _station;
final Function _handleDelete;
StationComponent( this._station, this._handleDelete);
@override
Widget build(BuildContext context) {
return Container(
child: Card(
child: Container(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Text(_station.name, textAlign: TextAlign.left),
IconButton(icon: Icon(Icons.delete), onPressed: _handleDelete(_station))
],
),
Row(
children: <Widget>[Row(children: <Widget>[])],
)
],
)),
),
);
}
}
다음과 같은 함수를 전달합니다.
onPressed: onPressFunction
가치가 필요하다면
onPressed: () => onPressFunction(value)
그리고 싫어
onPressed: onPressFunction(value)
그래서 당신의 코드는
IconButton(icon: Icon(Icons.delete), onPressed:() => _handleDelete(_station))
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다