Flutter 将浮动操作按钮居中
我真的被一个问题困住了,我试图将一个浮动动作按钮放在身体外的中央。当前代码如下所示:Flutter 将浮动操作按钮居中,flutter,dart,alignment,floating-action-button,Flutter,Dart,Alignment,Floating Action Button,我真的被一个问题困住了,我试图将一个浮动动作按钮放在身体外的中央。当前代码如下所示: @override Widget build(BuildContext context) { return Scaffold( body: FutureBuilder( future: _initializeCameraControllerFuture, builder: (context, snapshot) { if (snapsh
@override Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: _initializeCameraControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return CameraPreview(_cameraController);
} else {
return Center(child: CircularProgressIndicator());
}
},
),
floatingActionButton: Container(
height: buttonsize,
width: buttonsize,
child: FittedBox(
child: FloatingActionButton(
onPressed: () {
},
child: Icon(Icons.camera_alt),
backgroundColor: Colors.transparent,
elevation: 0.0,
shape: CircleBorder(side: BorderSide(color: Colors.white.withOpacity(0.25),width: 2.0)),
),
),
),
);
}
FutureBuilder正在创建相机视图,而FloatingAction按钮应该是用来拍照的按钮
每次我尝试使用alignment:alignment.center将其居中,在容器中,该容器还包含按钮的高度和宽度,但按钮没有居中,并且没有应有的高度和大小
顶部的代码工作正常,但按钮位于右下角
提前感谢您的回答 在Sage的评论中添加以下内容:
Scaffold(
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
body:...
),
您可以创建一个自定义的圆形按钮,如FAB button,并将其对齐到您想要的任何位置
body: Container(
child: MaterialButton(
onPressed: () {},
color: Colors.pink,
textColor: Colors.white,
child: Icon(
Icons.camera_alt,
size: 24,
),
padding: EdgeInsets.all(16),
shape: CircleBorder(),
),
),
您可以通过使用floatingActionButtonLocation属性并将其设置为floatingActionButtonLocation.centerFloat来实现这一点 检查下面的代码,它工作正常:
@override Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: _initializeCameraControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return CameraPreview(_cameraController);
} else {
return Center(child: CircularProgressIndicator());
}
},
),
// use this property to center the floating action button
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButton: Container(
height: buttonsize,
width: buttonsize,
child: FittedBox(
child: FloatingActionButton(
onPressed: () {
},
child: Icon(Icons.camera_alt),
backgroundColor: Colors.transparent,
elevation: 0.0,
shape: CircleBorder(side: BorderSide(color: Colors.white.withOpacity(0.25),width: 2.0)),
),
),
),
);
}
输出如下:
使用floatingActionButtonLocation并将其设置为居中@Mo711@T.TSage我不能使用它,只要我没有创建像BottomNavigationBar这样的东西是的,你可以@Mo711,我只是在DartPad中尝试过,它可以@Mo711。好的,我想用另一种方式,但这很有帮助,谢谢!