我正在制作餐饮店的应用程序,它有多种类型的食物,如素食,等等,作为布尔变量。一个页面过滤掉了用户必须看到的食物类型。因此,我使用SwitchListTile小部件来更改它的bool值。为此,我创建了小部件生成器方法,但此处的开关按钮没有工作。这是密码。
import 'package:flutter/material.dart';
class FilterPage extends StatefulWidget {
static const route = '/filter-page';
@override
State<FilterPage> createState() => _FilterPageState();
}
class _FilterPageState extends State<FilterPage> {
var _gluttenFree = false;
var _lectosFree = false;
var _vegan = false;
var _vegitarian = false;
Widget _buildSwitch(
String title, String descreption, var curValue, Function updateValue) {
return SwitchListTile(
value: curValue,
onChanged: (_) => updateValue(),
title: Text(title),
subtitle: Text(description),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Filter settings'),
),
drawer: DrawerWidget(),
body: Column(
children: [
Text(
'Adjust your meal selection',
),
Expanded(
child: ListView(
children: [
_buildSwitch(
'Gluteen-Free',
'It is include Gluteen-free meals',
_gluttenFree,
(newValue) {
setState(() {
_gluttenFree = newValue;
});
},
),
],
),
)
],
),
);
}
}发布于 2022-06-22 14:05:53
onChanged在回调时提供了一个bool,它是SwitchListTile的选择值。
就像。
Widget _buildSwitch(String title, String descreption, bool curValue,
Function(bool) updateValue) {
return SwitchListTile(
value: curValue,
onChanged: (value) => updateValue(value),
title: Text(title),
subtitle: Text(description),
);
}https://stackoverflow.com/questions/72716774
复制相似问题