Flutter 如何在颤振中将浮动操作按钮移动到屏幕左侧?
默认情况下,浮动操作按钮位于屏幕右侧。我知道floatingActionButtonLocation及其属性,如endDocked、startDocked、centerDocked,但它们都没有帮助我将其移动到屏幕左侧。我相信前面提供的答案提供了最接近您的问题的易于实现的选项集 答案基本上是在小部件树的脚手架组件上使用类似于以下内容的内容:Flutter 如何在颤振中将浮动操作按钮移动到屏幕左侧?,flutter,flutter-layout,floating-action-button,Flutter,Flutter Layout,Floating Action Button,默认情况下,浮动操作按钮位于屏幕右侧。我知道floatingActionButtonLocation及其属性,如endDocked、startDocked、centerDocked,但它们都没有帮助我将其移动到屏幕左侧。我相信前面提供的答案提供了最接近您的问题的易于实现的选项集 答案基本上是在小部件树的脚手架组件上使用类似于以下内容的内容: , floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat 根据选项
, floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat
根据选项的描述,它看起来不像是左对齐
还可以在此处查看可用选项:
我相信前面提供的答案提供了最接近您的问题的易于实现的选项集 答案基本上是在小部件树的脚手架组件上使用类似于以下内容的内容:
, floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat
根据选项的描述,它看起来不像是左对齐
还可以在此处查看可用选项:
我使用下面的代码通过在FAB的右侧使用填充来制作左侧的floatingAction按钮
floatingActionButton: Padding(
padding: const EdgeInsets.only(right: 275.0),
child: FloatingActionButton(
child: Icon(
Icons.add,
color: Colors.black,
),
backgroundColor: Colors.orange,
onPressed: (){},
),
),
我使用下面的代码通过在FAB的右侧使用填充来制作左侧的floatingAction按钮
floatingActionButton: Padding(
padding: const EdgeInsets.only(right: 275.0),
child: FloatingActionButton(
child: Icon(
Icons.add,
color: Colors.black,
),
backgroundColor: Colors.orange,
onPressed: (){},
),
),
基本上是由@E.Bradford在上面的评论中建议的,但是如果需要的话,我想提供一些代码。通过将FloatingActionButton堆叠到一个定位的小部件中,您可以将其放置在几乎任何您想要的地方
Stack(
children: [
Placeholder(),
Positioned(
left: 40,
bottom: 40,
child: FloatingActionButton(
onPressed: () {},
),
),
],
)
基本上是由@E.Bradford在上面的评论中建议的,但是如果需要的话,我想提供一些代码。通过将FloatingActionButton堆叠到一个定位的小部件中,您可以将其放置在几乎任何您想要的地方
Stack(
children: [
Placeholder(),
Positioned(
left: 40,
bottom: 40,
child: FloatingActionButton(
onPressed: () {},
),
),
],
)
您可以将floatingActionButton换行,并将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换行,并将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,
),
),
],
),
这将使您的晶圆厂位于左下角:
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButtonLocation: FloatingActionButtonLocation.startDocked,
floatingActionButton:
FloatingActionButton(heroTag: null, onPressed: () {}),
);
请参见这将把晶圆厂放在左下角:
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButtonLocation: FloatingActionButtonLocation.startDocked,
floatingActionButton:
FloatingActionButton(heroTag: null, onPressed: () {}),
);
请参见您可以将命名构造函数添加到脚手架中: 浮动ActionButtonLocation:浮动ActionButtonLocation.startFloat 或 浮动ActionButtonLocation:浮动ActionButtonLocation.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`
*/
);
}
}
您只需将命名构造函数添加到脚手架中: 浮动ActionButtonLocation:浮动ActionButtonLocation.startFloat 或 浮动ActionButtonLocation:浮动ActionButtonLocation.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`
*/
);
}
}
是的,我看到了。但它为如何在中心实现浮动操作按钮提供了建议。我想把它放在屏幕的左边。但是谢谢你的回复。我在答案中加了一点,我不认为有左对齐选项,先生,我只是注意到你的编辑。我完全同意默认情况下没有左对齐选项。我只是想一想,但是在堆栈中有一个小部件数组的堆栈对象,并且其中一个孩子有一个定位的小部件,在你的情况下,你想要的按钮会起作用吗?我还没有尝试过。但正如你所建议的,我也会试试。是的,我看到了。但它为如何在中心实现浮动操作按钮提供了建议。我想把它放在屏幕的左边。但是谢谢你的回复。我在答案中加了一点,我不认为有左对齐选项,先生,我只是注意到你的编辑。我完全同意默认情况下没有左对齐选项。我只是想一想,但是在堆栈中有一个小部件数组的堆栈对象,并且其中一个孩子有一个定位的小部件,在你的情况下,你想要的按钮会起作用吗?我还没有尝试过。但正如你所建议的,我也会尝试。我建议避免使用这种技术,因为你有不同的屏幕大小,不同的设备会有不同的感觉。我建议避免使用这种技术,因为你有不同的屏幕大小,不同的设备会有不同的感觉。有没有办法在停靠按钮上添加填充物或边距以便它没有靠近屏幕边缘?有没有办法给停靠的按钮添加填充或边距,使其不那么靠近屏幕边缘?