Flutter 如何在颤振中创建圆形图标按钮?
如何创建类似于浮动操作按钮的东西?您可以使用它: 对触摸作出反应的材料的矩形区域 下面的示例演示如何使用Flutter 如何在颤振中创建圆形图标按钮?,flutter,flutter-layout,Flutter,Flutter Layout,如何创建类似于浮动操作按钮的东西?您可以使用它: 对触摸作出反应的材料的矩形区域 下面的示例演示如何使用InkWell注意:您不需要StatefulWidget来执行此操作。我用它来改变计数的状态 例如: import 'package:flutter/material.dart'; class SettingPage extends StatefulWidget { @override _SettingPageState createState() => new _Settin
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)),
),//............
),
),
);
}
}
导入“包装:颤振/材料.省道”;
类设置页扩展StatefulWidget{
@凌驾
_SettingPageState createState()=>new_SettingPageState();
}
类_设置页面状态扩展状态{
int _计数=0;
@凌驾
小部件构建(构建上下文){
归还新脚手架(
正文:新中心(
孩子:新的墨水池(//这就是你要找的。。。。。。。。。。
onTap:()=>设置状态(()=>_count++),
子容器:新容器(
//宽度:50.0,
//身高:50.0,
padding:const EdgeInsets.all(20.0),//我使用了一些没有固定宽度和高度的填充
装饰:新盒子装饰(
shape:BoxShape.circle,//您可以像这样使用或像下面的线条一样使用
//边界半径:新边界半径。圆形(30.0),
颜色:颜色。绿色,
),
child:new Text(_count.toString(),style:new TextStyle(color:Colors.white,fontSize:50.0)),//您也可以添加图标而不是文本,如下所示。
//子项:新图标(Icons.arrow_forward,大小:50.0,颜色:Colors.black38)),
),//............
),
),
);
}
}
如果您想从splashColor
,highlightColor
中获益,请使用Material
小部件包装InkWell
小部件,并使用带有材质类型圆圈的Material
小部件。然后移除容器
小部件中的装饰
结果:
我认为RawMaterialButton更合适
RawMaterialButton(
onPressed: () {},
elevation: 2.0,
fillColor: Colors.white,
child: Icon(
Icons.pause,
size: 35.0,
),
padding: EdgeInsets.all(15.0),
shape: CircleBorder(),
)
您还可以使用内置图像的RaisedButton(例如用于社交登录),如下所示(需要带有fittebox的sizedbox将图像压缩到指定大小):
您可以轻松地执行以下操作:
FlatButton(
onPressed: () {
},
child: new Icon(
Icons.arrow_forward,
color: Colors.white,
size: 20.0,
),
shape: new CircleBorder(),
color: Colors.black12,
)
结果是更新(使用新的提升按钮
(定制较少)提升按钮
ElevatedButton( 按下:(){}, 子:图标(Icons.menu,颜色:Colors.white), 样式:ElevatedButton.styleFrom( 形状:CircleBorder(), 填充:边缘设置。全部(20),
primary:Colors.blue,//您只需要使用形状:
我的贡献:CircleBorder()
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, ), ), ); } }
此代码将帮助您添加按钮,而无需任何不必要的填充
试试这张卡RawMaterialButton( elevation: 0.0, child: Icon(Icons.add), onPressed: (){}, constraints: BoxConstraints.tightFor( width: 56.0, height: 56.0, ), shape: CircleBorder(), fillColor: Color(0xFF4C4F5E), ),
记下Card( elevation: 10, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(25.0), // half of height and width of Image ), child: Image.asset( "assets/images/home.png", width: 50, height: 50, ), )
,用于不允许在左侧填充约束:BoxConstraints()
愉快的飘动!!如果您需要背景图像,可以使用CircleAvatar和图标按钮。设置backgroundImage属性
按钮示例:CircleAvatar( backgroundImage: NetworkImage(userAvatarUrl), )
CircleAvatar( backgroundColor: Colors.blue, radius: 20, child: IconButton( padding: EdgeInsets.zero, icon: Icon(Icons.add), color: Colors.white, onPressed: () {}, ), ),
我之所以使用这个,是因为我喜欢自定义边界半径和大小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: () {} ), ),
我创建了一个具有正确剪裁、标高和边框的版本。请随意定制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, ), ), ), )),
实际上,有一个示例如何创建类似于FloatingActionButton的圆形图标按钮
要使用此代码示例创建本地项目,请运行:Ink( decoration: const ShapeDecoration( color: Colors.lightBlue, shape: CircleBorder(), ), child: IconButton( icon: Icon(Icons.home), onPressed: () {}, ), )
非物质解决方案:flutter create --sample=material.IconButton.2 mysample
您可以使用GestureDetector而不是TouchableOpacity库。使用ElevatedButton: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, ), ), )
ElevatedButton( onPressed: () {}, child: Icon( Icons.add, color: Colors.white, size: 60.0, ), style: ElevatedButton.styleFrom( shape: CircleBorder(), primary: Colors.green), )
下面的代码将创建一个半径为25的圆,并在其中添加白色的添加图标。如果用户还想使用click方法,只需将容器小部件包装到GestureDetector()或InkWell()中即可 2021 如果您需要平面(无标高),因为FlatButton现在不推荐使用
Blassanka,谢谢您提供的信息。我最终使用了FloatingAction按钮。但是您的解决方案在将来的其他场景中会派上用场。此代码不再创建“圆圈”按钮.我用这种方法得到了一个很大的水平填充,无论我怎么做都无法删除它。有什么想法吗?我使用RawMaterialButton constraints的constraints属性解决了这个问题:BoxConstraints(minWidth:36.0,maxWidth:36.0,minHeight:36.0,maxHeight:36.0这可能不是最好的解决方案,但效果很好。若要完全删除按钮周围的填充,请添加text按钮( 按下:(){}, 子:图标(图标。箭头返回), 样式:钮扣样式( 背景色:MaterialStateProperty.all(Colors.black26), shape:MaterialStateProperty.all(const CircleBorder()), );
,若要删除我添加的填充,请添加:MaterialTargetSize:MaterialTargetSize:MaterialTargetSize.shrinkWrap
对我来说,最好的方法是组合,constraints:BoxConstraints.expand(宽度:42,高度:42),
,MaterialTargetSize:MaterialTargetSize.shrinkWrap
和填充:EdgeInsets.all(8)
如果使用填充,请确保设置minWidth。约束:BoxConstraints(minWidth:0)
flutter create --sample=material.IconButton.2 mysample
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), ), ), ),
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, ), ), )
ElevatedButton( onPressed: () {}, child: Icon( Icons.add, color: Colors.white, size: 60.0, ), style: ElevatedButton.styleFrom( shape: CircleBorder(), primary: Colors.green), )
Container( height: 50, width: 50, decoration: BoxDecoration( color: Colors.blue, borderRadius: BorderRadius.circular(50 / 2), ), child: Center( child: Icon( Icons.add, color: Colors.white, ), ), ),