【问题标题】:How can I move floating action button to the left side of the Screen in Flutter?如何在 Flutter 中将浮动操作按钮移动到屏幕左侧?
【发布时间】:2019-12-04 01:21:40
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    , floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat
    

    根据选项的描述,它看起来不像是向左对齐。

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

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

    【讨论】:

    • 是的,我看到了。但它建议如何在 Center 上实现浮动操作按钮。我希望它在屏幕的左侧。但感谢您的回复。
    • 我在答案中添加了一点,我认为没有左对齐选项
    • 是的,先生,我刚刚注意到您的编辑。我完全同意默认情况下没有左对齐选项。
    • 只是一个想法,但是 Stack 对象(在堆栈中有一组小部件)和其中一个具有定位小部件(您想要的按钮)的孩子是否适用于您的情况?
    • 我还没试过。但正如你所建议的,我也会尝试一下。
    【解决方案2】:

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

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

    【讨论】:

    • 我建议避免使用这种技术,因为您有不同的屏幕尺寸,并且不同设备的感觉会有所不同。
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

        【讨论】:

        • 有没有办法为停靠的按钮添加填充或边距,使其不靠近屏幕边缘?
        【解决方案6】:

        您可以将命名构造函数添加到您的 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` 
        */
            );
          }
        }
        

        【讨论】:

          猜你喜欢
          • 2019-05-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-25
          • 2021-01-27
          • 2023-04-04
          相关资源
          最近更新 更多