Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 传递ListView元素的位置';s复选框,用于在颤振中更改回调_Flutter_Checkbox_Dart_Callback - Fatal编程技术网

Flutter 传递ListView元素的位置';s复选框,用于在颤振中更改回调

Flutter 传递ListView元素的位置';s复选框,用于在颤振中更改回调,flutter,checkbox,dart,callback,Flutter,Checkbox,Dart,Callback,我在下面写了《代码的和平》: int numberLines = 50; List<bool> checkBoxValues = []; int i = 0; while(i < numberLines) { checkBoxValues.add(false); i++; } void medCheckedChanged(bool value) => setState(() => checkBoxValues[position] = value)

我在下面写了《代码的和平》:

int numberLines = 50;
List<bool> checkBoxValues = [];

int i = 0;
while(i < numberLines)
{
    checkBoxValues.add(false);
    i++;
}

void medCheckedChanged(bool value) => setState(() => checkBoxValues[position] = value);

ListView.builder
(
    itemCount: numberLines,
    itemBuilder: (context, position)
    {
        return Column
        (
            children: <Widget>
            [
                Container
                (
                    height: resLayout.getScreenHeightPercentage(context, 6),
                    width: resLayout.getScreenHeightPercentage(context, 14),
                    alignment: Alignment(0, 0),
                    child: Checkbox
                    (
                        value: checkBoxValues[position],
                        onChanged: medCheckedChanged,   //pass to medCheckedChanged() the position
                    ),
                )
            ]
        ),
    }
)
int numberLines=50;
列出checkBoxValues=[];
int i=0;
而(isetState(()=>checkBoxValues[位置]=值);
ListView.builder
(
itemCount:numberLines,
itemBuilder:(上下文、位置)
{
返回列
(
儿童:
[
容器
(
高度:resLayout.getScreenHeightPercentage(上下文,6),
宽度:resLayout.getScreenHeightPercentage(上下文,14),
对齐:对齐(0,0),
子项:复选框
(
值:复选框值[位置],
onChanged:medCheckedChanged,//将位置传递给medCheckedChanged()
),
)
]
),
}
)
如您所见,我需要在我的
medCheckedChanged(bool value)
回调中使用position变量,但我无法将其作为参数传递。。。。 还有别的办法吗


提前感谢并继续使用解决方案编码:匿名函数

在Dart中,您可以创建单线函数,并向其传递参数。希望这能奏效

int numberLines = 50;
List<bool> checkBoxValues = [];

int i = 0;
while(i < numberLines)
{
    checkBoxValues.add(false);
    i++;
}

void medCheckedChanged(bool value) => setState(() => checkBoxValues[position] = value);

ListView.builder
(
    itemCount: numberLines,
    itemBuilder: (context, position)
    {
        return Column
        (
            children: <Widget>
            [
                Container
                (
                    height: resLayout.getScreenHeightPercentage(context, 6),
                    width: resLayout.getScreenHeightPercentage(context, 14),
                    alignment: Alignment(0, 0),
                    child: Checkbox
                    (
                        value: checkBoxValues[position],
                        onChanged: (value) => medCheckedChanged(value),   //pass to medCheckedChanged() the position
                    ),
                )
            ]
        ),
    }
)
int numberLines=50;
列出checkBoxValues=[];
int i=0;
而(isetState(()=>checkBoxValues[位置]=值);
ListView.builder
(
itemCount:numberLines,
itemBuilder:(上下文、位置)
{
返回列
(
儿童:
[
容器
(
高度:resLayout.getScreenHeightPercentage(上下文,6),
宽度:resLayout.getScreenHeightPercentage(上下文,14),
对齐:对齐(0,0),
子项:复选框
(
值:复选框值[位置],
onChanged:(value)=>medCheckedChanged(value),//将位置传递给medCheckedChanged()
),
)
]
),
}
)

感谢您的回答更好的方法是传递与列表视图内容关联的对象。然后可以将列表视图重新定位到所选对象。void medCheckedChanged(bool值)=>setState(()=>checkBoxValues[position]=value);此代码看起来不正确。位置应引发错误,因为它未传递给void函数