[英]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,您可以使用DecoratedBox和Theme小部件。 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
.将
DecoratedBox
的shape
设置为与OutlinedButton
相同的形状。 Now you can use the color
attribute of your DecoratedBox
to change the color.现在您可以使用
DecoratedBox
的color
属性来更改颜色。 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)以下是这些状态的示例:禁用(灰色)--> 正常(蓝色)--> 按下(红色)
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 用新的按钮类型(
TextButton
、 ElevatedButton
和OutlinedButton
)替换了前 3 种按钮类型( FlatButton
、 RaisedButton
和OutlineButton
)以与 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.