A commonly used scenario in App development is: click a button button, set button.setenabled (false), and then send a request, which will restore the button to the clickable state when the request returns successfully or fails: button.setenabled (true).

Can the same effect be achieved with Flutter? Reference: https://stackoverflow.com/questions/49351648/how-do-i-disable-a-button-in-flutter look at rendering:

Clicking on the second button increases the value of _counter, and if _counter is odd, the first button can be clicked, and if _counter is even, the first button cannot be clicked.

The implementation code is as follows:

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  TestMyAppState createState() => new TestMyAppState();
}

class TestMyAppState extends State<MyApp> {
  bool _isButton1Disabled;
  int _counter = 1;

  GlobalKey<ScaffoldState> _key;

  @override
  void initState() {
    _isButton1Disabled = false;
  }

  void _incrementCounter() {
    setState(() {
      _counter++;
      if (_counter % 2 == 0) {
        _isButton1Disabled = true;
      } else {
        _isButton1Disabled = false; }}); } @override Widget build(BuildContext context) { _key= new GlobalKey<ScaffoldState>();return new MaterialApp(
        home: new Scaffold(
            key: _key,
            appBar: new AppBar(title: new Text("test app")),
            body: new Container(
                alignment: Alignment.center,
                child: new Container(
                    child: new Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: buildButtons(),
                    )))));
  }

  List<Widget> buildButtons() {List<Widget> List = [_buildButton1(_counter), _buildSpaceView(20.0), // Add a little space between the two buttons _buildButton2()];return list;
  }

  Widget _buildSpaceView(double _height) {
    return new Container(height: _height);
  }

  RaisedButton _buildButton1(int counter) {
    returnNew RaisedButton(padding: new edgeinset.fromlTrb (10.0, 10.0, 10.0, 10.0), child: New Text('count: '+ counter.tostring (), style: new TextStyle(fontSize: 18.0, //textsize color: color.white, // textcolor),), color: Theme. Of (context). AccentColor, elevation: 4.0, // Shadow splashColor: Colors. BlueGrey, onPressed: _getBtn1ClickListener()); } RaisedButton_buildButton2() {
    returnNew RaisedButton(padding: new edgeinset.fromlTrb (10.0, 10.0, 10.0, 10.0), child: New Text('click me', style: new TextStyle(fontSize: 18.0, //textsize color: color.white, // textcolor),), color: Theme. Of (context). AccentColor, elevation: 4.0, // Shadow splashColor: Colors. BlueGrey, onPressed: _getBtn2ClickListener()); } Function_getBtn2ClickListener() {
    return () {
      _incrementCounter();
    };
  }

  _getBtn1ClickListener() {
    if (_isButton1Disabled) {
      return null;
    } else {
      return () {
        _key.currentState.showSnackBar(new SnackBar(
          content: new Text('Hello! '))); }; }}}Copy the code

We can see that stateless controls in flutter are actually cumbersome to use. If there are 10 buttons on the flutter interface, should we create 10 variables to store the disabled status of each button? Or is it wrong?