如何检查按钮是否被按下

时间:2019-08-29 10:29:33

标签: android ios flutter dart bloc

我需要一个赞按钮,当您单击一次时,该数字将增加,但是当您再次单击该按钮时,该数字将减少,如何检查是否单击了按钮

这是我的客户颤动小部件练习

按钮小部件

lass ButtonBuilder extends StatefulWidget{
  final Icon icon;

  ButtonBuilder({this.icon,});

  _ButtonBuilderState createState()=>_ButtonBuilderState(this.icon);
}

class _ButtonBuilderState extends State<ButtonBuilder>{
  final Icon _icon;

   _ButtonBuilderState(this._icon);

  final _bloc=ButtonCounterBloc();

  @override
  Widget build(BuildContext context) {
    return RootButton(
      child: StreamBuilder(
        stream: _bloc.counter,
        initialData: 0,
        builder: (BuildContext context,AsyncSnapshot snapshot){
          return _buildButton(_icon,'${snapshot.data}');
        },
      ),
    );
  }

  _buildButton(Icon _icon,String _text){
    return Column(
      children: <Widget>[
        IconButton(
          icon: _icon,
          onPressed: ()=>_bloc.buttonEventSink.add(FirstOnPressedEvent()),
        ),
        Text(_text),
      ],
    );
  }
}

main.dart

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

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ButtonBuilder(icon: Icon(Icons.thumb_up),),
            ButtonBuilder(icon: Icon(Icons.thumb_down),),
          ],
        ),
      ),
    );
  }
}

我不知道如何检查按钮是否被点击

1 个答案:

答案 0 :(得分:2)

您可以将名为isPressed的标志初始设置为false,该标志在每次按下按钮时都会被否定。

onPressed回调中,您可以根据变量编写不同的逻辑。

但是请记住,在以下示例中,_isPressed变量将在按钮按钮变宽的State被释放后被重置。

如果希望有一个持久按钮,则可以在ButtonCounterBloc内添加类似的参数,并将其值分配给_isPressed类的initState方法内的_ButtonBuilderState

这样,即使应用程序重新启动或状态被释放并再次安装,您的按钮也将保持不变。

class ButtonBuilder extends StatefulWidget{
  final Icon icon;

  ButtonBuilder({this.icon,});

  _ButtonBuilderState createState()=>_ButtonBuilderState(this.icon);
}

class _ButtonBuilderState extends State<ButtonBuilder>{
  final Icon _icon;

   _ButtonBuilderState(this._icon);

  final _bloc=ButtonCounterBloc();
  bool _isPressed = false;

  @override
  Widget build(BuildContext context) {
    return RootButton(
      child: StreamBuilder(
        stream: _bloc.counter,
        initialData: 0,
        builder: (BuildContext context,AsyncSnapshot snapshot){
          return _buildButton(_icon,'${snapshot.data}');
        },
      ),
    );
  }

  _buildButton(Icon _icon,String _text){
    return Column(
      children: <Widget>[
        IconButton(
          icon: _icon,
          onPressed: () {
            setState(() {
              _isPressed = !_isPressed;
            });
            if(_isPressed) {
              // This block will be executed when button is pressed odd number of times.
              _bloc.buttonEventSink.add(FirstOnPressedEvent());
            } else {
              // This block will be executed when button is pressed even number of times;
            }
          }
        ),
        Text(_text),
      ],
    );
  }
}