如何在flutter中为浮动操作按钮设置自定义位置?

时间:2021-06-02 12:41:49

标签: flutter flutter-layout

我正在尝试制作一个 sportify 的克隆版,我想设置一个浮动操作按钮 just like this green play button 有什么帮助吗?

2 个答案:

答案 0 :(得分:1)

您能否通过 Align Widget 将浮动操作按钮置于自定义位置

示例:

 Align(
         alignment: Alignment.center,
         child: FloatingActionButton(),
      ),

而且你可以通过 Chang center 字获得更具体的位置

答案 1 :(得分:0)

试试这个,

class PlayerScreen extends StatelessWidget {
  const PlayerScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        physics: const BouncingScrollPhysics(
            parent: AlwaysScrollableScrollPhysics()),
        slivers: <Widget>[
          SliverAppBar(
            stretch: true,
            onStretchTrigger: () {
              // Function callback for stretch
              return Future<void>.value();
            },
            expandedHeight: 300.0,
            flexibleSpace: FlexibleSpaceBar(
              stretchModes: const <StretchMode>[
                StretchMode.zoomBackground,
                StretchMode.blurBackground,
                StretchMode.fadeTitle,
              ],
              centerTitle: true,
              title: FloatingActionButton(
                child: Icon(
                  Icons.play_arrow,
                  size: 30,
                ),
                onPressed: () {},
                backgroundColor: Colors.greenAccent[400],
              ),
              background: Stack(
                fit: StackFit.expand,
                children: <Widget>[
                  Image.network(
                    'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg',
                    fit: BoxFit.cover,
                  ),
                  const DecoratedBox(
                    decoration: BoxDecoration(
                      gradient: LinearGradient(
                        begin: Alignment(0.0, 0.5),
                        end: Alignment.center,
                        colors: <Color>[
                          Color(0x60000000),
                          Color(0x00000000),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
          SliverList(
            delegate: SliverChildBuilderDelegate((ctx, index) {
              return ListTile(
                leading: Icon(Icons.wb_sunny),
                title: Text('Song $index'),
                subtitle: Text("description"),
              );
            }, childCount: 20),
          ),
        ],
      ),
    );
  }
}