如何更改 OutlinedButton 边框颜色?

Joh*_*ohn 6 dart flutter flutter-layout

Flutter 小部件,我尝试使用 BorderSide(color : Colors.blue) 更改 OutlineButton 边框颜色。无论设置哪种颜色,OutlineButton 始终带有灰色边框,但宽度更改适用。如何更改 OutlineButton 边框线条颜色?

class OutlineButtonWidget extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Center(
        child: OutlineButton(
            onPressed: null,
            borderSide: BorderSide(
                width: 5.0,
                color: Colors.blue,
                style: BorderStyle.solid,
            ),
            child: Text('outline button')
            ),
        ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

lav*_*ava 25

使用样式属性

   style: ButtonStyle(
                    side: MaterialStateProperty.all(BorderSide(
                        color: Colors.blue,
                        width: 1.0,
                        style: BorderStyle.solid)))
Run Code Online (Sandbox Code Playgroud)

或者尝试这两个工作

style: OutlinedButton.styleFrom(
                          side: BorderSide(
                            width: 5.0,
                            color: Colors.blue,
                            style: BorderStyle.solid,
                          ),
                        ),
            child: Text('outline button')
            )
Run Code Online (Sandbox Code Playgroud)
OutlinedButton(
                style: ButtonStyle(
                    side: MaterialStateProperty.all(BorderSide(
                        color: Colors.blue,
                        width: 1.0,
                        style: BorderStyle.solid))),
                onPressed: () {},
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Align(
                      child: Padding(
                        padding:
                            const EdgeInsets.symmetric(horizontal: 12.0),
                        child: Icon(
                          Icons.clear,
                          size: 24,
                        ),
                      ),
                    ),
                    Text("Clear")
                  ],
                ))
Run Code Online (Sandbox Code Playgroud)

结果可能是这样的 在此输入图像描述


Cop*_*oad 14

使用style财产

OutlinedButton(
  onPressed: () {},
  child: Text('Outlined button'),
  style: OutlinedButton.styleFrom(
    side: BorderSide(width: 5.0, color: Colors.blue),
  ),
)
Run Code Online (Sandbox Code Playgroud)


Alo*_*lok 10

我收到“OutlineButton”已弃用,不应使用。请改用 OutlinedButton。请参阅 flutter.dev/go/material-button-migration-guide 中的迁移指南)。

迁移前代码:

child: OutlineButton(
    onPressed: onPressed,
    child: CustomText(
      text,
      style: TextStyle(
          fontWeight: FontWeight.w600,
          fontSize: 14,
          color: Colors.black),
    ),
    color: Colors.orange,
    borderSide: BorderSide(color: Colors.amber),
  ),
Run Code Online (Sandbox Code Playgroud)

迁移后代码:

child: OutlinedButton(
    onPressed: onPressed,
    child: CustomText(
      text,
      style: TextStyle(
          fontWeight: FontWeight.w600,
          fontSize: 14,
          color: Colors.black),
    ),
    style: OutlinedButton.styleFrom(backgroundColor: Colors.orange, side: BorderSide(color: Colors.amber)),
  ),
Run Code Online (Sandbox Code Playgroud)

这是backgroundColor和颜色属性的官方参考: https://api.flutter.dev/flutter/material/ButtonStyle/backgroundColor.html

https://api.flutter.dev/flutter/material/MaterialButton/color.html


小智 9

样式属性将起作用

OutlineButton(
            onPressed: (){},
            style: OutlinedButton.styleFrom(
                          side: BorderSide(
                            width: 5.0,
                            color: Colors.blue,
                            style: BorderStyle.solid,
                          ),
                        ),
            child: Text('outline button')
            ),
        ),

Run Code Online (Sandbox Code Playgroud)