gpt4 book ai didi

flutter - 如果我习惯用 Switch 小部件上的新 val 更改状态,如何使用函数回调?

转载 作者:行者123 更新时间:2023-12-05 08:21:38 24 4
gpt4 key购买 nike

我尝试使用 Switch 小部件更改新状态但无法使用,以防万一我创建动态函数并将用于小部件树,在函数中我在 Switch 小部件的参数处发送回调函数,但实际上它无法使用。

code base

import 'package:flutter/material.dart';
import 'package:navigate_app/widgets/main_drawer.dart';

class FilterScreen extends StatefulWidget {
static const routeNamed = '/filter';

@override
State<FilterScreen> createState() => _FilterScreenState();
}

class _FilterScreenState extends State<FilterScreen> {
bool _glutenFree = false;
bool _vegetarian = false;
bool _vegan = false;
bool _lactoseFree = false;

Widget _buildSwitchFilter(String title, String description, bool currentValue,
Function updateValue) {
return SwitchListTile(
title: Text(title),
subtitle: Text(description),
value: currentValue,
onChanged: (val) => updateValue);
}

@override
Widget build(BuildContext context) {
return Scaffold(
drawer: MainDrawer(),
appBar: AppBar(),
body: Column(
children: [
Expanded(
child: ListView(
children: [
_buildSwitchFilter('Gluten-free',
'Only include gluten-free meals', _glutenFree, (newVal) {
setState(() {
_glutenFree == newVal;
});
}),
_buildSwitchFilter(
'Lactose-free',
'Only include lactose-free meals',
_glutenFree,
(newVal) => _lactoseFree == newVal),
_buildSwitchFilter(
'Vegan-free',
'Only include vegan-free meals',
_glutenFree,
(newVal) => _vegan == newVal),
_buildSwitchFilter(
'Vegetarian-free',
'Only include vegetarian-free meals',
_glutenFree,
(newVal) => _vegetarian == newVal),
],
))
],
));
}
}

it's pict

最佳答案

这是不正确的

_glutenFree == newVal;

== 是一个相等运算符 https://api.dart.dev/be/137051/dart-core/Object/operator_equals.html

你需要的是使用 =

赋值

正确:

_glutenFree = newVal;

对其他人做同样的事情

关于flutter - 如果我习惯用 Switch 小部件上的新 val 更改状态,如何使用函数回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71835637/

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