我找不到任何显示如何创建类似于floatingActionButton的圆形图标按钮的示例。任何人都可以建议如何/什么需要创建自定义按钮,如floatingActionButton。感谢。
答案 0 :(得分:41)
我认为RawMaterialButton更适合。
new RawMaterialButton(
onPressed: () {},
child: new Icon(
Icons.pause,
color: Colors.blue,
size: 35.0,
),
shape: new CircleBorder(),
elevation: 2.0,
fillColor: Colors.white,
padding: const EdgeInsets.all(15.0),
),
答案 1 :(得分:32)
您只需要使用以下形状:CircleBorder()
MaterialButton(
onPressed: () {},
color: Colors.blue,
textColor: Colors.white,
child: Icon(
Icons.camera_alt,
size: 24,
),
padding: EdgeInsets.all(16),
shape: CircleBorder(),
)
答案 2 :(得分:16)
颤动伴随着FloatingActionButton
示例:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'msc',
home: new Scaffold(
body: new Center(
child: new FloatingActionButton(
backgroundColor: Colors.redAccent,
onPressed: () => {},
),
)));
}
}
答案 3 :(得分:9)
您可以使用InkWell执行此操作:
材质的矩形区域,响应触摸。
下面的示例演示了如何使用InkWell
。 注意:您不需要StatefulWidget
来执行此操作。我用它来改变计数的状态。
示例:
import 'package:flutter/material.dart';
class SettingPage extends StatefulWidget {
@override
_SettingPageState createState() => new _SettingPageState();
}
class _SettingPageState extends State<SettingPage> {
int _count = 0;
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new InkWell(// this is the one you are looking for..........
onTap: () => setState(() => _count++),
child: new Container(
//width: 50.0,
//height: 50.0,
padding: const EdgeInsets.all(20.0),//I used some padding without fixed width and height
decoration: new BoxDecoration(
shape: BoxShape.circle,// You can use like this way or like the below line
//borderRadius: new BorderRadius.circular(30.0),
color: Colors.green,
),
child: new Text(_count.toString(), style: new TextStyle(color: Colors.white, fontSize: 50.0)),// You can add a Icon instead of text also, like below.
//child: new Icon(Icons.arrow_forward, size: 50.0, color: Colors.black38)),
),//............
),
),
);
}
}
如果您希望获得splashColor
,highlightColor
的好处,请使用带有素材类型圈的InkWell
小部件包装Material
小部件。然后移除decoration
小部件中的Container
。
结果:
答案 4 :(得分:9)
如果需要背景图像,则可以将CircleAvatar与IconButton一起使用。设置backgroundImage属性。
CircleAvatar(
backgroundImage: NetworkImage(userAvatarUrl),
)
带有按钮的示例:
CircleAvatar(
backgroundColor: Colors.blue,
radius: 20,
child: IconButton(
padding: EdgeInsets.zero,
icon: Icon(Icons.add),
color: Colors.white,
onPressed: () {},
),
),
答案 5 :(得分:8)
RawMaterialButton(
onPressed: () {},
constraints: BoxConstraints(),
elevation: 2.0,
fillColor: Colors.white,
child: Icon(
Icons.pause,
size: 35.0,
),
padding: EdgeInsets.all(15.0),
shape: CircleBorder(),
)
请注意constraints: BoxConstraints()
,这是因为不允许在其中填充。
扑扑快乐!
答案 6 :(得分:7)
使用 ElevatedButton:
ElevatedButton(
onPressed: () {},
child: Icon(
Icons.add,
color: Colors.white,
size: 60.0,
),
style: ElevatedButton.styleFrom(
shape: CircleBorder(), primary: Colors.green),
)
答案 7 :(得分:4)
实际上有一个示例,该示例如何创建类似于FloatingActionButton的圆形IconButton。
Ink(
decoration: const ShapeDecoration(
color: Colors.lightBlue,
shape: CircleBorder(),
),
child: IconButton(
icon: Icon(Icons.home),
onPressed: () {},
),
)
要使用此代码示例创建本地项目,请运行:
flutter create --sample=material.IconButton.2 mysample
答案 8 :(得分:2)
我的贡献:
import 'package:flutter/material.dart';
///
/// Create a circle button with an icon.
///
/// The [icon] argument must not be null.
///
class CircleButton extends StatelessWidget {
const CircleButton({
Key key,
@required this.icon,
this.padding = const EdgeInsets.all(8.0),
this.color,
this.onPressed,
this.splashColor,
}) : assert(icon != null),
super(key: key);
/// The [Icon] contained ny the circle button.
final Icon icon;
/// Empty space to inscribe inside the circle button. The [icon] is
/// placed inside this padding.
final EdgeInsetsGeometry padding;
/// The color to fill in the background of the circle button.
///
/// The [color] is drawn under the [icon].
final Color color;
/// The callback that is called when the button is tapped or otherwise activated.
///
/// If this callback is null, then the button will be disabled.
final void Function() onPressed;
/// The splash color of the button's [InkWell].
///
/// The ink splash indicates that the button has been touched. It
/// appears on top of the button's child and spreads in an expanding
/// circle beginning where the touch occurred.
///
/// The default splash color is the current theme's splash color,
/// [ThemeData.splashColor].
final Color splashColor;
@override
Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context);
return ClipOval(
child: Material(
type: MaterialType.button,
color: color ?? theme.buttonColor,
child: InkWell(
splashColor: splashColor ?? theme.splashColor,
child: Padding(
padding: padding,
child: icon,
),
onTap: onPressed,
),
),
);
}
}
答案 9 :(得分:2)
此代码将帮助您添加按钮,而不会出现不必要的填充,
RawMaterialButton(
elevation: 0.0,
child: Icon(Icons.add),
onPressed: (){},
constraints: BoxConstraints.tightFor(
width: 56.0,
height: 56.0,
),
shape: CircleBorder(),
fillColor: Color(0xFF4C4F5E),
),
答案 10 :(得分:2)
之所以使用这一点,是因为我喜欢自定义边框半径和大小。
Material( // pause button (round)
borderRadius: BorderRadius.circular(50), // change radius size
color: Colors.blue, //button colour
child: InkWell(
splashColor: Colors.blue[900], // inkwell onPress colour
child: SizedBox(
width: 35,height: 35, //customisable size of 'button'
child: Icon(Icons.pause,color: Colors.white,size: 16,),
),
onTap: () {}, // or use onPressed: () {}
),
),
Material( // eye button (customised radius)
borderRadius: BorderRadius.only(
topRight: Radius.circular(10.0),
bottomLeft: Radius.circular(50.0),),
color: Colors.blue,
child: InkWell(
splashColor: Colors.blue[900], // inkwell onPress colour
child: SizedBox(
width: 40, height: 40, //customisable size of 'button'
child: Icon(Icons.remove_red_eye,color: Colors.white,size: 16,),),
onTap: () {}, // or use onPressed: () {}
),
),
答案 11 :(得分:2)
我创建了一个具有正确剪裁,高程和边框的版本。随时对其进行自定义。
Material(
elevation: 2.0,
clipBehavior: Clip.hardEdge,
borderRadius: BorderRadius.circular(50),
color: Colors.white,
child: InkWell(
onTap: () => null,
child: Container(
padding: EdgeInsets.all(9.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(color: Colors.blue, width: 1.4)),
child: Icon(
Icons.menu,
size: 22,
color: Colors.red,
),
),
),
)),
答案 12 :(得分:2)
不是重大解决方案:
final double floatingButtonSize = 60;
final IconData floatingButtonIcon;
TouchableOpacity(
onTap: () {
/// Do something...
},
activeOpacity: 0.7,
child: Container(
height: floatingButtonSize,
width: floatingButtonSize,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(floatingButtonSize / 2),
color: Theme.of(context).primaryColor,
boxShadow: [
BoxShadow(
blurRadius: 25,
color: Colors.black.withOpacity(0.2),
offset: Offset(0, 10),
)
],
),
child: Icon(
floatingButtonIcon ?? Icons.add,
color: Colors.white,
),
),
)
您可以使用GestureDetector代替TouchableOpacity库。
答案 13 :(得分:1)
ClipOval(
child: MaterialButton(
color: Colors.purple,
padding: EdgeInsets.all(25.0),
onPressed: () {},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0)),
child: Text(
'1',
style: TextStyle(fontSize: 30.0),
),
),
),
答案 14 :(得分:0)
您可以尝试一下,它是完全可定制的。
ClipOval(
child: Material(
color: Colors.blue, // button color
child: InkWell(
splashColor: Colors.red, // inkwell color
child: SizedBox(width: 56, height: 56, child: Icon(Icons.menu)),
onTap: () {},
),
),
)
输出:
答案 15 :(得分:0)
试用此卡
04-16 18:11:25.699 : Error / CAPI_TELEPHONY ( 5724 : 5724 ) : telephony_common.c: telephony_init(473) > telephony feature is disabled
04-16 18:11:40.459 : Error / CAPI_TELEPHONY ( 5925 : 5925 ) : telephony_common.c: telephony_init(473) > telephony feature is disabled
04-16 18:11:40.611 : Error / CAPI_TELEPHONY ( 5925 : 5928 ) : telephony_common.c: telephony_init(473) > telephony feature is disabled
04-16 18:11:40.627 : Error / CAPI_TELEPHONY ( 5925 : 5928 ) : telephony_common.c: telephony_init(473) > telephony feature is disabled
答案 16 :(得分:0)
您可以轻松地执行以下操作:
FlatButton(
onPressed: () {
},
child: new Icon(
Icons.arrow_forward,
color: Colors.white,
size: 20.0,
),
shape: new CircleBorder(),
color: Colors.black12,
)
答案 17 :(得分:0)
您还可以使用带有内部图像的RaisedButton(例如,用于社交登录)(需要使用带有fittebox的sizebox来将图像限制为指定大小):
FittedBox(
fit: BoxFit.scaleDown,
child: SizedBox(
height: 60,
width: 60,
child: RaisedButton(
child: Image.asset(
'assets/images/google_logo.png'),
shape: StadiumBorder(),
color: Colors.white,
onPressed: () {},
),
),
),
答案 18 :(得分:0)
下面的代码将创建一个半径为 25 的圆,并在其中添加白色图标。如果用户还想拥有可以通过将 Container 小部件包装到 GestureDetector() 或 InkWell() 中来简单实现的点击方法。
Container(
height: 50,
width: 50,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(50 / 2),
),
child: Center(
child: Icon(
Icons.add,
color: Colors.white,
),
),
),
答案 19 :(得分:0)
如果您需要平面(无高度),因为 FlatButton 现在已弃用。
TextButton(
onPressed: (){},
child: Icon(Icons.arrow_back),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.black26),
shape: MaterialStateProperty.all(const CircleBorder())),
);