Den*_*hmd 3 callback dart flutter
我尝试使用 Switch 小部件更改新状态,但它无法使用,以防万一我创建动态函数并将在小部件树中使用,在函数中我在 Switch 小部件的参数处发送回调函数,但实际上它不能用过的。
代码库
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),
],
))
],
));
}
}
Run Code Online (Sandbox Code Playgroud)
这是不正确的
_glutenFree == newVal;
Run Code Online (Sandbox Code Playgroud)
==是一个相等运算符
https://api.dart.dev/be/137051/dart-core/Object/operator_equals.html
你需要的是使用分配值=
正确的:
_glutenFree = newVal;
Run Code Online (Sandbox Code Playgroud)
对其他人也做同样的事情
| 归档时间: |
|
| 查看次数: |
1500 次 |
| 最近记录: |