【问题标题】:Flutter Container not changing on tapFlutter Container 在点击时不会改变
【发布时间】:2023-02-09 19:14:16
【问题描述】:

我正在尝试创建一个带有手势检测器的容器,该检测器可以在点击时更改颜色,但由于某种原因它没有这样做。我有一个 bool 和一个函数来设置和更改它,并且在容器的 backgroundColor 中我让它根据 bool 的颜色进行更改。任何建议将不胜感激。

import 'package:flutter/material.dart';

class VotingButton extends StatefulWidget {
  @override
  State<VotingButton> createState() => _VotingButtonState();
}

class _VotingButtonState extends State<VotingButton> {
  bool savePressed = false;

  void buttonPressed() {
    setState(() {
      if (savePressed == false) {
        savePressed == true;
      } else if (savePressed == true) {
        savePressed == false;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 18.0),
      child: GestureDetector(
        onTap: () {
          buttonPressed;
          print(savePressed); //stays false for some reason
        },
        child: Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(6),
            color: savePressed ? Colors.blue : Colors.red[400],
          ),
          child: Padding(
            padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 40),
            child: Text(
              'I\'ll be Here!',
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
            ),
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter flutter-animation


    【解决方案1】:
    import 'package:flutter/material.dart';
    
    class VotingButton extends StatefulWidget {
      @override
      State<VotingButton> createState() => _VotingButtonState();
    }
    
    class _VotingButtonState extends State<VotingButton> {
      bool savePressed = false;
    
      void buttonPressed() {
        setState(() {
          if (savePressed == false) {
            savePressed == true;
          } else if (savePressed == true) {
            savePressed == false;
          }
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Padding(
          padding: const EdgeInsets.only(top: 18.0),
          child: GestureDetector(
            onTap: () {
              //You are referencing but not calling here, you should use ()
              buttonPressed();
              print(savePressed); //stays false for some reason
            },
            child: Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(6),
                color: savePressed ? Colors.blue : Colors.red[400],
              ),
              child: Padding(
                padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 40),
                child: Text(
                  'I'll be Here!',
                  style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                ),
              ),
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-03
      • 1970-01-01
      • 2011-08-24
      • 2015-02-15
      • 1970-01-01
      相关资源
      最近更新 更多