Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: What is the difference ButtonStyle() and .styleFrom()

I am new in Flutter.

Here is my code,

For ElevatedButton,

ElevatedButton(
                  onPressed: () {
                    // Add your onPressed code here!
                  },
                  child: Text("Login"),
                  style: ButtonStyle(
                      backgroundColor: MaterialStateProperty.all<Color>(
                          AppColors.SecondaryColor),
                      foregroundColor: MaterialStateProperty.all<Color>(
                          AppColors.PrimaryColor))),

For OutlinedButton,

OutlinedButton(
                  onPressed: () {
                    // Add your onPressed code here!
                  },
                  child: Text("Register Now"),
                  style: OutlinedButton.styleFrom(
                    side: BorderSide(width: 2, color: AppColors.SecondaryColor),
                  ))

My question is why I must use styleFrom for OutlinedButton instead of ButtonStyle? if substitute OutlinedButton.styleFrom with ButtonStyle, it gives error. Why?

I am very confuse with the usage of ButtonStyle and styleFrom. Because some of the example on the internet use ButtonStyle while some use styleFrom.

like image 971
Safiah Nadiah Avatar asked Nov 10 '20 06:11

Safiah Nadiah


People also ask

What is styleFrom in Flutter?

A static convenience method that constructs an elevated button ButtonStyle given simple values. The onPrimary , and onSurface colors are used to create a MaterialStateProperty ButtonStyle.

What is elevated button in Flutter?

An elevated button is a label child displayed on a Material widget whose Material. elevation increases when the button is pressed. The label's Text and Icon widgets are displayed in style's ButtonStyle. foregroundColor and the button's filled background is the ButtonStyle.

How many types of buttons are there in Flutter?

Flutter suggests using at most one FAB button per screen. There are two types of Floating Action Button: FloatingActionButton: It creates a simple circular floating button with a child widget inside it.

How do you change the button style in Flutter?

Go to your main. Inside the MaterialApp, find the ThemeData widget. Add the elevatedButtonTheme property inside and assign the ElevatedButtonThemeData(). Add the style property and change the color as you would change it for normal ElevatedButton. Place the ElevatedButton widget anywhere in your Flutter app and see.


2 Answers

  1. What error do you get?

    As the documentation says, both the ElevatedButton and OutlinedButton supports both ButtonStyle() and .styleFrom().

    With ButtonStyle() you've to define all the required properties and with ButtonStyle.styleFrom() picks the default set values and you only change the required values.

    It'll be a lot easier to help you if you tell what error you get in using ButtonStyle() in OutlinedButton

---------------------------------------------------------------------------------------------------------------------- UPDATED ANSWER

Yes, because the side parameter in ButtonStyle() requires value of MaterialStateProperty and not of BorderSide . Use this code to see how it works:

OutlinedButton(
              onPressed: null,
              child: Text('Outlined Button'),
              style: ButtonStyle(
                side: MaterialStateProperty.all(
                  BorderSide.lerp(
                      BorderSide(
                        style: BorderStyle.solid,
                        color: Color(0xffe4e978),
                        width: 10.0,
                      ),
                      BorderSide(
                        style: BorderStyle.solid,
                        color: Color(0xffe4e978),
                        width: 10.0,
                      ),
                      10.0),
                ),
              ),
            ),

Output:enter image description here

See this link for more understanding about this.

like image 78
Umar Calcuttawala Avatar answered Oct 14 '22 03:10

Umar Calcuttawala


As the documentation say, the styleFrom() method is a simpler way to apply Button Style :

A static convenience method that constructs an elevated button [ButtonStyle] given simple values.

You have the same behavior with all three new Material buttons (TextButton, OutlinedButton and ElevatedButton)

like image 41
F Perroch Avatar answered Oct 14 '22 03:10

F Perroch