如何在 Flutter 中将浮动操作按钮移动到屏幕的左侧?

How can I move floating action button to the left side of the Screen in Flutter?

默认情况下,浮动操作按钮位于屏幕右侧。我知道 floatingActionButtonLocation 及其属性,例如 endDockedstartDockedcenterDocked,但其中的 none 帮助我将它移到了左侧屏幕。

我相信之前提供的这个答案提供了最接近您的问题的 "Easy to Implement" 组选项。

答案基本上是说在 Widget 树的 Scaffold 组件上使用类似于以下内容的内容:

, floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat

根据选项的描述,似乎没有左对齐。

另请查看此处的可用选项:

https://api.flutter.dev/flutter/material/FloatingActionButtonLocation-class.html

我使用以下代码通过在 FAB 的右侧使用填充来在左侧制作 floatingActionButton。

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: () {},
      ),
    ),
  ],
)

你可以用 Row() 包裹你的 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,
      ),
    ),
  ],
),

这会将您的 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

您只需将命名构造函数添加到脚手架中即可:

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` 
*/
    );
  }
}