简体   繁体   English

如何设置 Flutter OutlineButton 的背景颜色?

[英]How to set the background color of a Flutter OutlineButton?

class _HomePageState extends State<HomePage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("..."),
      ),
      body: Container(
        color: Colors.green,
        child: OutlineButton(
          onPressed: () { },
          color: Colors.orange,
          highlightColor: Colors.pink,
          child: Container(
            color: Colors.yellow,
            child: Text("A"),
          ),
          shape: CircleBorder(),
        ),
      ),
    );
  }
}

在此处输入图片说明

The above code gives a transparent button.上面的代码给出了一个透明的按钮。 How can I get an orange OutlineButton?我怎样才能得到一个橙色的 OutlineButton?

To modify the backgroundColor of a OutlineButton you can use aDecoratedBox and a Theme widget.要修改OutlineButton的 backgroundColor,您可以使用DecoratedBoxTheme小部件。 At the end of this answer you'll find a quick example.在这个答案的末尾,你会找到一个简单的例子。

Anyway I'd still recommend simply using the FlatButton with its color attribute instead.无论如何,我仍然建议简单地使用带有color属性的FlatButton

Wrap your OutlinedButton inside a DecoratedBox .将您的OutlinedButton包裹在DecoratedBox Set the shape of your DecoratedBox to the same shape your OutlinedButton .DecoratedBoxshape设置为与OutlinedButton相同的形状。 Now you can use the color attribute of your DecoratedBox to change the color.现在您可以使用DecoratedBoxcolor属性来更改颜色。 The result will still have a small padding around the OutlinedButton .结果仍然会在OutlinedButton周围有一个小的填充。 To remove this you can wrap the DecoratedBox inside a Theme in which you adjust the ButtonTheme .要删除它,您可以将DecoratedBox包装在一个Theme中,您可以在其中调整ButtonTheme Inside the ButtonTheme you want to set materialTapTargetSize: MaterialTapTargetSize.shrinkWrap .ButtonTheme您要设置materialTapTargetSize: MaterialTapTargetSize.shrinkWrap

The padding is added inside Flutter, to increase the tap area around the button to a minimum size of 48x48 (source) .在 Flutter 内部添加了填充,以将按钮周围的点击区域增加到最小尺寸 48x48 (source) Setting materialTapTargetSize to MaterialTapTargetSize.shrinkWrap removes this minimum size.materialTapTargetSize设置为MaterialTapTargetSize.shrinkWrap会移除这个最小尺寸。


FlatButton example: FlatButton示例:

演示

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: FlatButton(
            color: Colors.pinkAccent,
            shape: CircleBorder(),
            onPressed: () => {},
            child: Text('A'),
          ),
        ),
      ),
    );
  }
}

OutlinedButton example: OutlinedButton示例:

演示

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: MyButton(),
        ),
      ),
    );
  }
}

class MyButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return DecoratedBox(
      decoration:
          ShapeDecoration(shape: CircleBorder(), color: Colors.pinkAccent),
      child: Theme(
        data: Theme.of(context).copyWith(
            buttonTheme: ButtonTheme.of(context).copyWith(
                materialTapTargetSize: MaterialTapTargetSize.shrinkWrap)),
        child: OutlineButton(
          shape: CircleBorder(),
          child: Text('A'),
          onPressed: () => {},
        ),
      ),
    );
  }
}

OutlineButton has been deprecated and has been replaced with OutlinedButton . OutlineButton已被弃用并已替换为OutlinedButton (Notice the "d".) (注意“d”。)

OutlinedButton 's documentation helped me understand how to use it. OutlinedButton文档帮助我了解如何使用它。 Here's an example with these states: Disabled (Grey) --> Normal (Blue) --> Pressed (Red)以下是这些状态的示例:禁用(灰色)--> 正常(蓝色)--> 按下(红色)

Outlined-Button-Disabled-Normal-Pressed

   return Container(
      width: double.infinity,
      height: 48,
      child: OutlinedButton(
        child: Text(
          "This is an Outline\"d\"Button. (Not OutlineButton)",
          style: TextStyle(color: Colors.white),
        ),
        onPressed: () => print("Tapped"),
        //onPressed: null, //Uncomment this statement to check disabled state.
        style: ButtonStyle(
          backgroundColor: MaterialStateProperty.resolveWith<Color>((states) {
            if (states.contains(MaterialState.disabled)) {
              return Colors.grey[100];
            }
            return Colors.blue;
          }),
          overlayColor: MaterialStateProperty.resolveWith<Color>((states) {
            if (states.contains(MaterialState.pressed)) {
              return Colors.red;
            }
            return Colors.transparent;
          }),
          side: MaterialStateProperty.resolveWith((states) {
            Color _borderColor;

            if (states.contains(MaterialState.disabled)) {
              _borderColor = Colors.greenAccent;
            } else if (states.contains(MaterialState.pressed)) {
              _borderColor = Colors.yellow;
            } else {
              _borderColor = Colors.pinkAccent;
            }

            return BorderSide(color: _borderColor, width: 5);
          }),
          shape: MaterialStateProperty.resolveWith<OutlinedBorder>((_) {
            return RoundedRectangleBorder(borderRadius: BorderRadius.circular(16));
          }),
        ),
      ),
    );

Flutter replaced the former 3 button types ( FlatButton , RaisedButton and OutlineButton ) with new buttons types ( TextButton , ElevatedButton and OutlinedButton ) to remain in sync with Material design and also because using MaterialStateProperty provides the ultimate flexibility to achieve whatever state-specific-UI one needs. Flutter 用新的按钮类型( TextButtonElevatedButtonOutlinedButton )替换了前 3 种按钮类型( FlatButtonRaisedButtonOutlineButton )以与 Material Design 保持同步,并且还因为使用MaterialStateProperty提供了实现任何特定状态的 UI 的终极灵活性需要。 You can read more about it here .您可以在此处阅读更多相关信息。

If you need to Change Colour of Outline Border如果您需要更改轮廓边框的颜色

 OutlineButton(
borderSide: BorderSide(color: kPrimaryColor, width: 1),)

这非常简单,只需使用

backgroundColor: MaterialStateProperty.all(Colors.colorname),

Here's way that you can check the background color of the button.您可以通过以下方式检查按钮的背景颜色。 Remove hightlightColor, and try give some value to highlightElevation property of OutlineButton, then press it, you could see initially it loads orange color.去掉hightlightColor,尝试给OutlineButton的highlightElevation属性赋值,然后按下它,你可以看到它最初加载了橙色。

您可以使用FlatButton,并在那里设置颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM