Flutter의 요령을 이해하기 시작했지만 버튼의 활성화 상태를 설정하는 방법을 파악하는 데 어려움이 있습니다.
문서에서 onPressed버튼을 비활성화하려면 null로 설정하고 활성화하려면 값을 지정하십시오. 버튼이 수명주기 동안 계속 동일한 상태에 있으면 괜찮습니다.
어떻게 든 버튼의 활성화 상태 (또는 onPressed 콜백)를 업데이트 할 수있는 맞춤형 Stateful 위젯을 만들어야한다는 인상을 받았습니다.
그래서 제 질문은 어떻게할까요? 이것은 매우 간단한 요구 사항처럼 보이지만 문서에서 그것을 수행하는 방법에 대한 내용을 찾을 수 없습니다.
감사.
답변
build버튼에 몇 가지 도우미 기능을 도입 하고 키 오프 할 속성과 함께 Stateful 위젯 을 도입하고 싶을 수도 있습니다 .
- (예를 StatefulWidget / 주를 사용하여 상태를 유지하기 위해 변수를 생성 
isButtonDisabled) - 처음에는 true로 설정하십시오 (원하는 경우).
 - 버튼을 렌더링 할 때 값을 하나 또는 일부 기능으로 직접 설정하지 마십시오.
onPressednullonPressed: () {} - 대신 삼항 또는 도우미 함수를 사용하여 조건부로 설정하십시오 (아래 예).
 isButtonDisabled이 조건부의 일부로 확인하고null함수 중 하나 또는 일부를 반환합니다 .- 버튼을 눌렀을 때 (또는 버튼을 비활성화 할 때마다)를 사용 
setState(() => isButtonDisabled = true)하여 조건부 변수를 뒤집습니다. - Flutter는 
build()새로운 상태로 메서드를 다시 호출하고 버튼은null프레스 핸들러 로 렌더링되고 비활성화됩니다. 
다음은 Flutter 카운터 프로젝트를 사용하는 몇 가지 컨텍스트입니다.
class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  bool _isButtonDisabled;
  @override
  void initState() {
    _isButtonDisabled = false;
  }
  void _incrementCounter() {
    setState(() {
      _isButtonDisabled = true;
      _counter++;
    });
  }
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("The App"),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'You have pushed the button this many times:',
            ),
            new Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
            _buildCounterButton(),
          ],
        ),
      ),
    );
  }
  Widget _buildCounterButton() {
    return new RaisedButton(
      child: new Text(
        _isButtonDisabled ? "Hold on..." : "Increment"
      ),
      onPressed: _isButtonDisabled ? null : _incrementCounter,
    );
  }
}
이 예에서 나는 설정 조건에 인라인 원을 사용하고 Text하고 onPressed있지만, 당신이 함수에이를 추출하기에 더 적합 할 수있다 (당신은 버튼의 텍스트뿐만 아니라를 변경하려면이 같은 방법을 사용할 수 있습니다)
Widget _buildCounterButton() {
    return new RaisedButton(
      child: new Text(
        _isButtonDisabled ? "Hold on..." : "Increment"
      ),
      onPressed: _counterButtonPress(),
    );
  }
  Function _counterButtonPress() {
    if (_isButtonDisabled) {
      return null;
    } else {
      return () {
        // do anything else you may want to here
        _incrementCounter();
      };
    }
  }
답변
문서에 따르면 :
“onPressed 콜백이 null이면 버튼이 비활성화되고 기본적으로 disabledColor의 평면 버튼과 유사합니다.”
https://docs.flutter.io/flutter/material/RaisedButton-class.html
따라서 다음과 같이 할 수 있습니다.
    RaisedButton(
      onPressed: calculateWhetherDisabledReturnsBool() ? null : () => whatToDoOnPressed,
      child: Text('Button text')
    );
답변
간단한 대답은 onPressed : null비활성화 된 버튼을 제공하는 것입니다.
답변
환경
onPressed: null // disables click
과
onPressed: () => yourFunction() // enables click
답변
특정 및 제한된 수의 위젯의 경우 위젯을 IgnorePointer로 래핑하면 정확히이 작업이 수행됩니다. ignoring속성이 true로 설정되면 하위 위젯 (실제로는 전체 하위 트리)을 클릭 할 수 없습니다.
IgnorePointer(
    ignoring: true, // or false
    child: RaisedButton(
        onPressed: _logInWithFacebook,
        child: Text("Facebook sign-in"),
        ),
),
그렇지 않고 전체 하위 트리를 비활성화하려면 AbsorbPointer ()를 살펴보십시오.
답변
활성화 및 비활성화 기능은 대부분의 위젯에서 동일합니다.
예, 버튼, 스위치, 체크 박스 등
onPressed아래와 같이 속성을 설정하십시오.
onPressed : null비활성화 된 위젯을 반환합니다.
onPressed : (){}또는 활성화 된 위젯을onPressed : _functionName 반환 합니다.
답변
AbsorbPointer를 사용할 수도 있으며 다음과 같은 방법으로 사용할 수 있습니다.
AbsorbPointer(
      absorbing: true, // by default is true
      child: RaisedButton(
        onPressed: (){
          print('pending to implement onPressed function');
        },
        child: Text("Button Click!!!"),
      ),
    ),
이 위젯에 대해 더 알고 싶다면 다음 링크를 확인하세요. Flutter Docs
