默认情况下,浮动操作按钮位于屏幕右侧。我知道
floatingActionButtonLocation
及其属性,如 endDocked
、startDocked
、centerDocked
,但它们都没有帮助我将其移动到屏幕左侧。
您只需将命名构造函数添加到脚手架中即可:
floatingActionButtonLocation: FloatingActionButtonLocation.startFloat
或
floatingActionButtonLocation: FloatingActionButtonLocation.startDocked
以入门应用程序为例:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
//##########################################################################
floatingActionButtonLocation: FloatingActionButtonLocation.startFloat,
//##########################################################################
/*
or
`floatingActionButtonLocation: FloatingActionButtonLocation.startDocked`
*/
);
}
}
我相信之前提供的答案为您的问题提供了最接近的“易于实施”选项集。
https://stackoverflow.com/a/49761700/10768398
答案基本上是说在 Widget 树的 Scaffold 组件上使用类似于以下内容的内容:
, floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat
根据选项的描述,看起来没有向左对齐。
另请查看此处的可用选项:
https://api.flutter.dev/flutter/material/FloatingActionButtonLocation-class.html
这会将您的 FAB 放置在左下角:
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButtonLocation: FloatingActionButtonLocation.startDocked,
floatingActionButton:
FloatingActionButton(heroTag: null, onPressed: () {}),
);
参见 https://api.flutter.dev/flutter/material/FloatingActionButtonLocation/startDocked-constant.html
我使用以下代码通过使用 FAB 右侧的填充来在左侧制作浮动操作按钮。
floatingActionButton: Padding(
padding: const EdgeInsets.only(right: 275.0),
child: FloatingActionButton(
child: Icon(
Icons.add,
color: Colors.black,
),
backgroundColor: Colors.orange,
onPressed: (){},
),
),
您可以用
floatingActionButton
包裹您的 Row()
并将 crossAxisAlignment
放在中心:
floatingActionButton: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(left:25.0),
child: FloatingActionButton(
onPressed: () {
// Add your onPressed code here!
},
child: Icon(Icons.message,color: Colors.white,),
backgroundColor: Colors.green,
),
),
],
),
您可以使用此代码
floatingActionButton: Align(
alignment: Alignment.centerLeft,
child: FloatingActionButton(
onPressed: (){},
),
),