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

问题描述 投票:0回答:7

默认情况下,浮动操作按钮位于屏幕右侧。我知道

floatingActionButtonLocation
及其属性,如
endDocked
startDocked
centerDocked
,但它们都没有帮助我将其移动到屏幕左侧。

flutter flutter-layout floating-action-button
7个回答
11
投票

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

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

6
投票

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

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

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

, floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat

根据选项的描述,看起来没有向左对齐。

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

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


6
投票

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


3
投票

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

Stack(
  children: [
    Placeholder(),
    Positioned(
      left: 40,
      bottom: 40,
      child: FloatingActionButton(
        onPressed: () {},
      ),
    ),
  ],
)


3
投票

我使用以下代码通过使用 FAB 右侧的填充来在左侧制作浮动操作按钮。

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

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,
      ),
    ),
  ],
),

0
投票

您可以使用此代码

floatingActionButton: Align(
            alignment: Alignment.centerLeft,
            child: FloatingActionButton(

              onPressed: (){},
            ),
          ),
© www.soinside.com 2019 - 2024. All rights reserved.