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

时间:2019-07-25 17:38:05

标签: flutter flutter-layout floating-action-button

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

6 个答案:

答案 0 :(得分:2)

您可以用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,
      ),
    ),
  ],
),

答案 1 :(得分:1)

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

floatingActionButton: Padding(
    padding: const EdgeInsets.only(right: 275.0),
    child: FloatingActionButton(
      child: Icon(
        Icons.add,
        color: Colors.black,
      ),
      backgroundColor: Colors.orange,
      onPressed: (){},
    ),
  ),

答案 2 :(得分:1)

基本上由@ E.Bradford在上面的评论中建议,但我想在需要时提供一些代码。通过将FloatingActionButton堆叠到定位的小部件中,可以将其放置在几乎任何位置。

output = []
for i in range(len(df)):
    tmp = []
    for k in range(len(df2)):
        d = df2.loc[k].isin(df.loc[i,['a1']])
        f = df2.loc[k].isin(df.loc[i,['b1']])
        d = d.tolist()
        f = f.tolist()
        if sum(d) >= 1 and sum(f) >=1:
            tmp.append(k)
    output.append(tmp)

output
[[0], [], [], [0, 1, 3], [1], [0, 4]]

enter image description here

答案 3 :(得分:1)

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

答案 4 :(得分:1)

您可以将命名的构造函数添加到 Scaffold 中:

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

答案 5 :(得分:0)

我相信以前提供的答案为您的问题提供了最接近的“易于实施”选项。

https://stackoverflow.com/a/49761700/10768398

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

, floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat

根据选项的说明,看起来好像没有向左对齐。

还可以在此处查看可用选项:

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