Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change border color and width of OutlineButton globally?

Tags:

flutter

dart

I need to change the border color and width of the OutlineButton, I know one way of doing this by directly mentioning it inline as below:

OutlineButton(
  child: Text('SIGN IN'),
  padding: EdgeInsets.all(8.0),
  onPressed: handleSignIn,
  borderSide: BorderSide(
    color: Colors.white, //Color of the border
    style: BorderStyle.solid, //Style of the border
    width: 1, //width of the border
  ),
)

If I do it as mentioned below, it's effecting FlatButton and RaisedButton and this is NOT effecting OutlineButton.

MaterialApp(
  title: 'MyApp',
  theme: ThemeData(
    buttonTheme: ButtonThemeData(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(20),

        // this effects to FlatButton and RaisedButton
        side: BorderSide(
          color: Colors.white, //Color of the border
          style: BorderStyle.solid, //Style of the border
          width: 1, //width of the border
        ),
      ),
    ),
  ),
);

So, How to change border color and width ONLY of OutlineButton globally (through out the app)?

like image 460
Syed Avatar asked Nov 06 '19 11:11

Syed


People also ask

How do you change the Outlinebutton border color in Flutter?

To change the Outlined Button color in Flutter, simply set the style property of Outlined Button from the OutlinedButton. styleFrom() static method and set the backgroundColor property to change background color and primary property to change the text color.

How do you change the color of a container border?

To change the color and width of Container's border, use its decoration property. Set decoration property with BoxDecoration() object. Set the border property of BoxDecoration() object, with required color and width as per your applications specifications.

How do you give border a color in Flutter?

Step 1: Go to the Container in which you want to add a border. Step 2: Add the decoration parameter and assign the BoxDecoration class. Inside the BoxDecoration add the parameter border and set it to Border. all().


2 Answers

I don't think you can do for that in OutlineButton. So, what i can suggest you is to create a global theme data in a global file.

var borderData=BorderSide(
    color: Colors.white, //Color of the border
    style: BorderStyle.solid, //Style of the border
    width: 1, //width of the border
  )

Then you can easily use it any class just by importing that global file.

OutlineButton(
  child: Text('SIGN IN'),
  padding: EdgeInsets.all(8.0),
  onPressed: handleSignIn,
  borderSide: borderData,
)

This will reduce your boiler plate code in your case.

like image 83
Jay Mungara Avatar answered Oct 16 '22 16:10

Jay Mungara


Unless borderSide is explicitly added to the OutlineButton, it will default to a width of 1.0, a style of solid and this color:

Theme.of(context).colorScheme.onSurface.withOpacity(0.12)

You have the option of changing onSurface of your app theme, but that will affect other widgets as well.

You can do what Jay suggested in the other answer, but my preference would be to create a different widget. This way, if you ever want to change anything else to all of your OutlineButtons in the future, you only need to change your custom widget:

import 'package:flutter/material.dart';

class OutlineButton2 extends OutlineButton {
  const OutlineButton2({
    Key key,
    @required VoidCallback onPressed,
    Widget child,
  }) : super(
    key: key,
    onPressed: onPressed,
    child: child,
    borderSide: const BorderSide(
      color: Colors.white,
      style: BorderStyle.solid,
      width: 1,
    ),
  );
}
like image 2
Ovidiu Avatar answered Oct 16 '22 18:10

Ovidiu