【问题标题】:Flutter cant get ontap function to change boolian listFlutter 无法获取 ontap 函数来更改布尔列表
【发布时间】:2020-11-09 02:33:23
【问题描述】:

我一直在尝试让每个按钮更改 bool 列表,并且当列表针对某个索引为 true 时出现严重故障时,容器变得可见。问题是由于某种原因我的手势按钮不想更改布尔列表,但更改任何其他整数列表没有问题。

我已经尝试让这个简单的东西工作了几天,任何帮助都是好的。

我的代码:

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

class FilterRow extends StatefulWidget {
  @override
  FilterRow({Key key, this.title}) : super(key: key);

  final String title;
  _FilterRowState createState() => _FilterRowState();
}

class _FilterRowState extends State<FilterRow> {
  int _filterIndex = 0;
  List<bool> list = [
    false,
    false,
    false,
    false,
  ];

  List<IconData> _filter = [FontAwesomeIcons.filter];
  List<IconData> _filtericons = [
    FontAwesomeIcons.clock,
    FontAwesomeIcons.moneyBill,
    FontAwesomeIcons.globe,
  ];
  List<IconData> _uppericons = [
    FontAwesomeIcons.cog,
    FontAwesomeIcons.bookmark,
    FontAwesomeIcons.user,
  ];
  @override
  Widget build(BuildContext context) {
    return Row(children: <Widget>[
      SizedBox(
          width: 100.0,
          child: Container(
            height: 270.0,
            padding: EdgeInsets.only(
              left: 15,
            ),
            child: Column(children: <Widget>[
              Row(
                  mainAxisSize: MainAxisSize.min,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: <Widget>[
              ),
              Column(
                children: <Widget>[
                  GestureDetector(
                    behavior: HitTestBehavior.translucent,
                    onTap: () {
                      setState(() {
                        list[0] = false;
                        list[1] = true;
                        list[2] = false;
                        list[3] = false;
                      });
                    },
                    child: Container(
                      height: 60.0,
                      width: 60.0,
                      decoration: BoxDecoration(
                        color: Theme.of(context).accentColor,
                        borderRadius: BorderRadius.circular(30.0),
                      ),
                      child: Icon(_filtericons[0],
                          size: 25.0, color: Theme.of(context).primaryColor),
                    ),
                  ),
                    ),
                  ),
                ],
              )
            ]),
          )),
      Container(
        padding: EdgeInsets.only(left: 10),
        child: Column(
          children: <Widget>[
            Visibility(
                visible: false
                    ? list = [
                        false,
                        true,
                        false,
                        false,
                      ]
                    : true,
                child: Container(
                  height: 270.0,
                  width: 320.0,
                  decoration: BoxDecoration(
                    color: Theme.of(context).accentColor,
                    borderRadius: BorderRadius.circular(30.0),
                  ),
                )),
            Visibility(
                visible: false,
                child: Container(
                  height: 270.0,
                  width: 320.0,
                  decoration: BoxDecoration(
                    color: Theme.of(context).accentColor,
                    borderRadius: BorderRadius.circular(30.0),
                  ),
                )),
            Visibility(
                visible: false,
                child: Container(
                  height: 270.0,
                  width: 320.0,
                  decoration: BoxDecoration(
                    color: Theme.of(context).accentColor,
                    borderRadius: BorderRadius.circular(30.0),
                  ),
                )),
          ],
        ),
      )
    ]);
  }
}


【问题讨论】:

  • 我没有看到你在任何地方使用list,除了设置值。为什么不只粘贴相关代码?
  • 刚刚清理了代码,实际上我确实使用列表来更改我的 GestureDetectors 的 ontap 函数中的值。

标签: flutter


【解决方案1】:

通过删除 @override 解决了问题

【讨论】:

    【解决方案2】:

    将 List 变量放入 Widget 函数中。

    Widget build(Context context) {
    
     List<bool> list = [
        false,
        false,
        false,
        false,
      ];
    
    return // your code
    
    }
    

    【讨论】:

    • 我做了并且仍然认为除了在我的可见性小部件上我的visible: false ? list = [ false, true, false,false, ] : true, 被标记为死代码。
    猜你喜欢
    • 2023-03-13
    • 1970-01-01
    • 2020-06-10
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2018-02-19
    相关资源
    最近更新 更多