flutter - 如何设置 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(),
),
),
);
}
}
上面的代码给出了一个透明按钮。我怎样才能得到一个橙色的大纲按钮?
解决方案
要修改OutlineButton的 backgroundColor,您可以使用DecoratedBox和Theme小部件。在这个答案的最后,你会找到一个简单的例子。
无论如何,我仍然建议简单地使用FlatButton及其color
属性。
将您的OutlinedButton
内部包裹起来DecoratedBox
。将您的设置为shape
与您DecoratedBox
的相同的形状OutlinedButton
。现在您可以使用您的color
属性DecoratedBox
来更改颜色。结果仍然会在OutlinedButton
. 要删除它,您可以将DecoratedBox
调整. 在你要设置的里面。Theme
ButtonTheme
ButtonTheme
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap
在 Flutter 中添加了填充,以将按钮周围的点击区域增加到最小尺寸 48x48 (source)。设置materialTapTargetSize
为MaterialTapTargetSize.shrinkWrap
删除此最小尺寸。
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
例子:
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: () => {},
),
),
);
}
}
推荐阅读
- clojure - Clojure - 让第一个 + 过滤器变得懒惰
- sql - COALESCE 中的 CASE
- r - gganimate中许多(> 50)状态的问题
- python - 循环列表-列表的长度但不需要创建的项目,pythonic?
- c# - 如何在 UWP 中添加 mediaplayerelements 控件
- c# - ASP.Net 子应用程序抛出实体框架错误。
- javascript - 堆叠 UIkit.modal 内置对话框
- javascript - AngularJs 文本框验证,使用正则表达式负前瞻限制字符
- node.js - Nodejs应用程序在本地主机上工作但在heroku上崩溃
- django - urlpatterns += (静态(settings.STATIC_URL,ducoment_root=settings.STATIC_ROOT))