首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >小部件SwitchListTile的开关在使用小部件生成器方法时不能正常工作

小部件SwitchListTile的开关在使用小部件生成器方法时不能正常工作
EN

Stack Overflow用户
提问于 2022-06-22 13:50:50
回答 1查看 18关注 0票数 0

我正在制作餐饮店的应用程序,它有多种类型的食物,如素食,等等,作为布尔变量。一个页面过滤掉了用户必须看到的食物类型。因此,我使用SwitchListTile小部件来更改它的bool值。为此,我创建了小部件生成器方法,但此处的开关按钮没有工作。这是密码。

代码语言:javascript
复制
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;
                });
              },
            ),
          ],
        ),
      )
    ],
    ),
   );
 }
 }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-22 14:05:53

onChanged在回调时提供了一个bool,它是SwitchListTile的选择值。

就像。

代码语言:javascript
复制
  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),
    );
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72716774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档