如何在 Flutter 中的新 OutlinedButton 小部件上实现圆角?

Ada*_*aka 24 dart flutter flutter-layout

在 Flutter 1.22 中,我们收到了一个OutlinedButton用于替换的新小部件,OutlineButton但我们如何才能真正使其边框变圆?borderSide并且shape属性不再可用。

Mat*_*ias 38

您可以使用OutlinedButton.styleFrom属性:

OutlinedButton(
   style: OutlinedButton.styleFrom(
      shape: RoundedRectangleBorder(
         borderRadius: BorderRadius.circular(18.0),
      ),
      side: BorderSide(width: 2, color: Colors.green),
   ),
   onPressed: () {},
   child: Text('Button'),
)
Run Code Online (Sandbox Code Playgroud)

从源代码

 /// All parameters default to null, by default this method returns
 /// a [ButtonStyle] that doesn't override anything.
 ///
 /// For example, to override the default shape and outline for an
 /// [OutlinedButton], one could write:
 ///
 /// ```dart
 /// OutlinedButton(
 ///   style: OutlinedButton.styleFrom(
 ///      shape: StadiumBorder(),
 ///      side: BorderSide(width: 2, color: Colors.green),
 ///   ),
 /// )
 /// ```
Run Code Online (Sandbox Code Playgroud)


Cop*_*oad 6

截屏:

在此输入图像描述

如果您需要对样式进行一些额外的控制,例如按下按钮时边框应为 ABC,悬停时应为 DEF...未交互时为 XYZ,您可以使用ButtonStyle.

OutlinedButton(
  onPressed: () {},
  style: ButtonStyle(
    shape: MaterialStateProperty.resolveWith<OutlinedBorder?>((states) {
      // Rounded button (when the button is pressed)
      if (states.contains(MaterialState.pressed)) {
        return RoundedRectangleBorder(borderRadius: BorderRadius.circular(20));
      }
    }),
  ),
  child: Text('OutlinedButton'),
)
Run Code Online (Sandbox Code Playgroud)