Home > database >  how to hide onStepContinue button for the last step and onStepCancel for the first step on flutter?
how to hide onStepContinue button for the last step and onStepCancel for the first step on flutter?

Time:02-10

This is my first time on flutter framework and Dart programming I have so manny miss of understanding Dart language.

I want to hide onStepContinue button for the last step and onStepCancel for the first step on flutter? this is my code I'm newbe in flutter any help?

class _MyHomePageState extends State<MyHomePage> {
  int _currentStep = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Stepper(
          controlsBuilder: (BuildContext context, ControlsDetails details) {
            return Row(
              children: <Widget>[
              TextButton(
                onPressed: details.onStepContinue,
                child: const Text('NEXT'),
              ),
              TextButton(
                onPressed: details.onStepCancel,
                child: const Text('BACK'),
              ),
            ],
            );
          },
          steps: const [
            Step(
              title: Text("Step 1"),
              content: Text("Information for step 1"),
            ),
            Step(
              title: Text("Step 2"),
              content: Text("Information for step 2"),
            ),
            Step(
              title: Text("Step 3"),
              content: Text("Information for step 3"),
            ),
          ],
          onStepTapped: (int newIndex){
            setState(() {
              _currentStep = newIndex;
            });
          },
          currentStep: _currentStep,
          onStepContinue: () {
            if (_currentStep != 2) {
              setState(() {
                _currentStep  = 1;
              });
            }
          },
          onStepCancel: () {
            if (_currentStep != 0) {
              setState(() {
                _currentStep -= 1;
              });
            }
          },
        ),
      ),
    );
  }
}

CodePudding user response:

Use conditional state to show the buttons.

controlsBuilder: (BuildContext context, ControlsDetails details) {
  return Row(
    children: <Widget>[
      if (_currentStep != 2) // skip on last step
        TextButton(
          onPressed: details.onStepContinue,
          child: const Text('NEXT'),
        ),
      if (_currentStep != 0)// skip on 1st step   
         TextButton(
          onPressed: details.onStepCancel,
          child: const Text('BACK'),
        ),
    ],
  );
},

CodePudding user response:

Wrap your widget using Visibility like this,

bool goneVisibilty=true;

Visibility(
  child: Text("Gone"),//replace with your widget
  visible: goneVisibility,
),

when ever u need to update this widget call setState to update.

setState((){
goneVisibility=false;
});

CodePudding user response:

To Enable/Disable Button you can use this

 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,
        );
      }
    }
  • Related