基本上,我正在嘗試在其中一個螢屏上制作一個應用程式 - 串列視圖的內容將通過選擇 toggleButtons 中列出的選項之一進行更新(一個僅顯示即將發生的事件,第二個選項是具有已通過)。但是當我嘗試設定新狀態時,它不會重新加載串列視圖,也不會為 ToggleButtons 中的選定選項著色。如何重繪 兩者?
以供參考:
列出filteredCands = []; //開始為空,當點擊其中一個按鈕時被事件填充
串列 isSelected = [true, false];
切換按鈕和 setState(():
child: ToggleButtons(
isSelected: isSelected,
selectedColor: Colors.white,
color: Color(0xFF33CCCC),
fillColor: Color(0xFF33CCCC),
renderBorder: true,
borderWidth: 1.5,
borderRadius: BorderRadius.circular(10),
children: const [
Padding(
padding: EdgeInsets.symmetric(horizontal: 12),
child: Icon(FontAwesomeIcons.calendarXmark, size: 25),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 12),
child: Icon(FontAwesomeIcons.calendarDay, size: 25),
),
],
onPressed: (int newIndex) {
final data = listViewGetCandListByManagerResponse
.jsonBody['candList'] as List;
List<CandModel> cands = data.map((e) => CandModel.fromJson(e))
.toList();
DateTime now = new DateTime.now();
DateTime currentDate = new DateTime(now.year, now.month, now.day);
setState(() {
//looping through the list of bool values
for (int index = 0; index < isSelected.length; index )
{
//Checking for the index value
if (index == newIndex)
{
isSelected[index] = true;
if (index == 0)
{
for (var i = 0; i < cands.length; i ) {
DateTime expirationDate = DateTime.parse(cands[i].dateEvent);
final bool isExpired = expirationDate.isBefore(currentDate);
if (isExpired == true) {
filteredCands.add(cands[i]);
}
}
}
if (index == 1)
{
for (var i = 0; i < cands.length; i ) {
DateTime expirationDate = DateTime.parse(cands[i].dateEvent);
final bool isFuture = currentDate.isBefore(expirationDate);
if (isFuture == true) {
filteredCands.add(cands[i]);
}
}
}
}
else
{isSelected[index] = false;}
}
});
},
),
那是串列視圖:
child: Padding(
padding: EdgeInsetsDirectional.fromSTEB(0, 8, 0, 0),
child: FutureBuilder<ApiCallResponse>(
future: GetCandListByManagerCall.call(
entityId: widget.entityId,
),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: SizedBox(
width: 50,
height: 50,
child: CircularProgressIndicator(
color:
FlutterFlowTheme.of(context).primaryColor,
),
),
);
}
return Builder(
builder: (context) {
if (filteredCands.isEmpty) {
return Center(
child: Text('Error - Looks like there are no events available'
),
);
}
return ListView.builder(
padding: EdgeInsets.zero,
scrollDirection: Axis.vertical,
itemCount: filteredCands.length,
itemBuilder: (context, dataIndex) {
if (!snapshot.hasData) {
return Center(
child: SizedBox(
width: 50,
height: 50,
child: CircularProgressIndicator(
color: FlutterFlowTheme.of(context).primaryColor,
),
),
);
}
int count = 0;
String date = (filteredCands[dataIndex].dateEvent).toString();
DateTime tempDate = DateTime.parse(date);
String dmy = DateFormat.yMMMd().format(tempDate);
String exDate = dmy; //Date Formatting
return InkWell(
child: SworkerContainerWidget(
desc: filteredCands[dataIndex].descEvent,
fname: filteredCands[dataIndex].wfirstName,
lname: filteredCands[dataIndex].wlastName,
dateEvent: exDate,
),
);
},
);
},
);
結果:

如您所見,我單擊了第二個選項,它既不著色也不重繪 串列視圖
uj5u.com熱心網友回復:
檢查您第一次在清單上宣告的位置。這可能是問題的原因(發現我放錯地方了)它需要在類 __widgetstate 下而不是在builder下。還建議每次單擊按鈕時清除串列視圖,除非您希望串列中有無限事件哈哈
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/520016.html
