颤动 - 中心的FloatingActionButton

raf*_*b21 7 center-align dart floating-action-button flutter

是否可以离开FloatingActionButton中心而不是右侧?

import 'package:flutter/material.dart';
import 'number.dart';
import 'keyboard.dart';

class ContaPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) => new Scaffold(
    body: new Column(
      children: <Widget>[
        new Number(),
        new Keyboard(),
      ],
    ),
    floatingActionButton: new FloatingActionButton(
      elevation: 0.0,
      child: new Icon(Icons.check),
      backgroundColor: new Color(0xFFE57373),
      onPressed: (){}
    )
  );
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Bri*_*ung 40

我不知道这是否加入,因为这个问题,首先回答了,但现在有floatingActionButtonLocation产权的Scaffold类.

在原始问题中它会像这样工作:

class ContaPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) => new Scaffold(
    // ...

    floatingActionButton: new FloatingActionButton(
      // ...FloatingActionButton properties...
    ),

    // Here's the new attribute:

    floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
  );
}
Run Code Online (Sandbox Code Playgroud)

另见文档:


Rao*_*che 16

使用新的flutter API,您只需floatingActionButtonLocation将Scaffold中的属性更改为

FloatingActionButtonLocation.centerFloat
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

范例:

return new Scaffold(
  floatingActionButton: new FloatingActionButton(
    child: const Icon(Icons.add),
  ),
  floatingActionButtonLocation:    
      FloatingActionButtonLocation.centerFloat,
  bottomNavigationBar: new BottomAppBar(
    color: Colors.white,
    child: new Row(...),
  ),
);
Run Code Online (Sandbox Code Playgroud)


Ash*_*ful 16

使用属性floatingActionButtonLocation脚手架类。

floatActionButtonLocation: FloatingActionButtonLocation.centerFloat,

完整示例:

import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: HomePage()
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Container(child: Center(child: Text('Hello World')),),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        child: Icon(Icons.camera, color: Colors.white, size: 29,),
        backgroundColor: Colors.black,
        tooltip: 'Capture Picture',
        elevation: 5,
        splashColor: Colors.grey,
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
    );
  }
}
Run Code Online (Sandbox Code Playgroud)


Col*_*son 9

尝试在其包装Center部件或使用crossAxisAlignmentCrossAxisAlignment.centerColumn

您应该选择将您的一部分Column包裹在一个Flexible会折叠以避免溢出的部分中,或者用 a 替换部分或全部,ListView以便用户可以滚动查看隐藏的部分。


Ath*_*ddy 9

浮动操作按钮中心

floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
Run Code Online (Sandbox Code Playgroud)

将此属性与 Scaffold 中的 floatingActionButtonLocation 属性一起使用。

FloatingActionButton Flutter - 更多细节


Dul*_*era 7

Align(
                      alignment: Alignment.center,
                      child: Container(
                        child: FloatingActionButton(
                          hoverColor: Colors.black,
                          elevation: 10,
                          onPressed: () {},
                          backgroundColor: Colors.pink,
                          child: Icon(Icons.add,),
                          shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.all(Radius.circular(20.0))),
                        ),
                      ),
                    ),
Run Code Online (Sandbox Code Playgroud)

在这里,我使用“对齐”小部件使 FloatingActionButton 居中。你可以在这里看到它。


Tus*_*kam 6

浮动操作按钮小部件结束后,您可以使用floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,

例如

   import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  File _image;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark(),
      title: "Camera App",
      home: Scaffold(
        appBar: AppBar(
          title: Text("Camera App"),
        ),
        body: Center(
          child: Center(
            child: _image == null
                ? Text('No image selected.')
                : Image.file(_image,
                alignment: Alignment.topLeft,
                ),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          elevation: 50,
          hoverColor: Colors.red,
          autofocus: true,
          onPressed: () {
            imagepicker();
          },
          child: Icon(Icons.camera_alt),
          tooltip: 'Pick Image',
        ),
         floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      ),
    );
  }

  Future imagepicker() async {
    var image = await ImagePicker.pickImage(source: ImageSource.gallery);
    setState(() {
      _image = image;
    });
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

您可以使用 Container 和 Align 小部件,如下所示:

@override
Widget build(BuildContext context) {
    return new Scaffold(
      body: Center(

      ),
      floatingActionButton: Container(
        padding: EdgeInsets.only(bottom: 100.0),
        child: Align(
          alignment: Alignment.bottomCenter,
          child: FloatingActionButton.extended(
            onPressed: _getPhoneAuthResult,
            icon: Icon(Icons.phone_android),
            label: Text("Authenticate using Phone"),
          ),
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
    );
  }
Run Code Online (Sandbox Code Playgroud)


小智 5

上面的例子很棒,但是如果您想完全控制浮动操作按钮的确切位置,您应该使用 Align 小部件包装您的 FloatingActionButton 小部件,并使用 Alignment(x axis, y axis) 来设置确切位置。

Align(
  alignment: Alignment(0.0, 0.8), 
//control the location by changing the numbers here to anything between 1 and -1
  child: FloatingActionButton()
)
Run Code Online (Sandbox Code Playgroud)