gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:46:41 32 4
gpt4 key购买 nike

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(),
),
),
);
}
}

enter image description here

上面的代码给出了一个透明按钮。如何获得橙色 OutlineButton?

最佳答案

OutlineButton 已被弃用,并已替换为 OutlinedButton。 (注意“d”。)

OutlinedButtondocumentation帮助我了解如何使用它。以下是这些状态的示例:禁用(灰色)--> 正常(蓝色)--> 按下(红色)

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 用新的按钮类型(TextButton 替换了前 3 种按钮类型(FlatButtonRaisedButtonOutlineButton) >、ElevatedButtonOutlinedButton) 与 Material design 保持同步,而且因为使用 MaterialStateProperty 提供了终极灵 active 来实现任何特定状态- UI 需要。您可以阅读更多信息 here .

关于flutter - 如何设置 Flutter OutlineButton 的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55370952/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com