我想創建過濾器串列,如果用戶選擇它們中的每一個做一些適當的東西。我發現
這部分的部分代碼:
...
ChipsChoice<int>.single(
value: tag,
onChanged: (value) {
setState(() {
tag = value;
});
},
...
但是為了做其他事情,我需要獲取所選專案的索引,添加了一個回呼函式來獲取索引,但 IDK 有任何想法更新 的值tag以更改所選專案的 UI:

這部分的代碼:
import 'package:flutter/material.dart';
import 'package:flutter_chip_choice/flutter_chip_choice.dart';
class TagChoices extends StatefulWidget {
const TagChoices(
{required this.options, required this.itemCallBack, Key? key})
: super(key: key);
final List<Enum> options;
final Function(int?) itemCallBack;
@override
_TagChoicesState createState() => _TagChoicesState();
}
class _TagChoicesState extends State<TagChoices> {
int tag = 0;
@override
Widget build(BuildContext context) {
return ChipsChoice<int>.single(
value: tag,
onChanged: widget.itemCallBack,
choiceItems: C2Choice.listFrom<int, Enum>(
source: widget.options,
style: (i, v) {
return const C2ChoiceStyle(
borderRadius: BorderRadius.all(Radius.circular(10)),
showCheckmark: false,
);
},
activeStyle: (i, v) {
return const C2ChoiceStyle(
brightness: Brightness.dark,
);
},
value: (i, v) => i,
label: (i, v) =>
v.toString().split(".").last.replaceAll("_", " ").toUpperCase(),
),
choiceStyle: const C2ChoiceStyle(
color: Colors.blueGrey,
avatarBorderColor: Colors.white,
),
choiceActiveStyle: const C2ChoiceStyle(
color: Color(0xFFff004d),
),
);
}
}
uj5u.com熱心網友回復:
我認為你不能那樣設定tag,因為widget.itemcallback來自另一個班級。但是你可以tag像另一個引數一樣宣告:
class TagChoices extends StatefulWidget {
const TagChoices(
{required this.options, required this.itemCallBack, Key? key})
: super(key: key);
final List<Enum> options;
int tag; // <-- tag variable
final Function(int?) itemCallBack;
@override
_TagChoicesState createState() => _TagChoicesState();
}
并且回呼函式可以使用tag變數:
ChipsChoice<int>.single(
value: widget.tag,
onChanged: widget.itemCallBack,
...
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/350650.html
下一篇:浮動圖片在其容器外顫動
