Home > Mobile >  How can I upgrade a "RaisedButton" to "ElevatedButton" in Flutter?
How can I upgrade a "RaisedButton" to "ElevatedButton" in Flutter?

Time:12-28

I got the following code and want to make it work:

              RaisedButton(
                child:
                    Text(_authMode == AuthMode.Login ? 'LOGIN' : 'SIGN UP'),
                onPressed: _submit,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(30),
                ),
                padding:
                    EdgeInsets.symmetric(horizontal: 30.0, vertical: 8.0),
                color: Theme.of(context).primaryColor,
                textColor: Theme.of(context).primaryTextTheme.button.color,
              ),

I tried to change it to some point as the following:

          ElevatedButton(
            child:
                Text(_authMode == AuthMode.Login ? 'LOGIN' : 'SIGN UP'),
            onPressed: _submit,
            style: ButtonStyle(
              shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(30),
                )
              )
            ),

            padding:
                EdgeInsets.symmetric(horizontal: 30.0, vertical: 8.0),
            color: Theme.of(context).primaryColor,
            textColor: Theme.of(context).primaryTextTheme.button.color,
          ),

But I don't know what to do with padding, color, and textColor?

CodePudding user response:

Here's how to convert the RaisedButton to an ElevatedButton.

On the ElevatedButton use:

  • For the textColor use the TextStyle on the Text widget.
  • For rounded corners, instead of shape, use the style property.
  • For the color, use the style property.
  • For padding, use the style property.

Your code should look like this:

ElevatedButton(
      style: ElevatedButton.styleFrom(
        padding: EdgeInsets.symmetric(horizontal: 30.0, vertical: 8.0),
        backgroundColor: Theme.of(context).primaryColor,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(30),
        ),
      ),
      child: Text(
        _authMode == AuthMode.Login ? 'LOGIN' : 'SIGN UP',
        style: TextStyle(
            color: Theme.of(context).primaryTextTheme.button?.color),
      ),
      onPressed: _submit,
    )

See also:

CodePudding user response:

Try this

    ElevatedButton(
          child: Text(_authMode == AuthMode.Login ? 'LOGIN' : 'SIGN UP',
           style:TextStyle(color: 
              Theme.of(context).primaryTextTheme.button?.color)),
          onPressed: _submit,
          style: ElevatedButton.styleFrom(
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
            padding: EdgeInsets.symmetric(horizontal: 30.0, vertical: 8.0),
            backgroundColor: Theme.of(context).primaryColor,
          ),
        )
  • Related