Flutter底部滑动应用栏插件slidable_bottom_app_bar的使用

发布于 1周前 作者 yibo5220 来自 Flutter

Flutter底部滑动应用栏插件slidable_bottom_app_bar的使用

预览

SlidableBottomAppBar(
    hasCenterButton: true,

SlidableBottomAppBar(
    hasCenterButton: false,

开始使用

在你的Flutter项目的pubspec.yaml文件中添加以下依赖:

dependencies:
  slidable_bottom_app_bar: ^1.0.1

然后运行 $ flutter pub get。在你的库中添加以下导入:

import 'package:slidable_bottom_app_bar/slidable_bottom_app_bar.dart';

使用方法

使用方法是将它放在Scaffold小部件的body参数中,并将页面内容放在pageBody属性中,如下面的示例所示:

return Scaffold(
      body: SlidableBottomAppBar(
        // 外观参数
        shape: SlidableBottomAppBarShape.rounded,
        color: Colors.blue,
        buttonColor: Colors.blue,
        maxHeight: screenSize.height * 0.5,
        allowShadow: true,
        // 主屏幕内容
        pageBody: const SafeArea(
          child: Center(
            child: Text('页面内容'),
          ),
        ),
        // SlidableBottomAppBar 的主体内容
        body: Column(
          children: const [
            Center(
              child: Text('内容'),
            ),
          ],
        ),
        // 中心按钮的内容
        buttonChild: const Icon(
          Icons.refresh,
          color: Colors.white,
        ),
        // 中心按钮的点击事件
        onButtonPressed: () {
          // 做一些事情
        },
        // 底部应用栏的内容
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            const Icon(
              Icons.home,
              color: Colors.white,
            ),
            SizedBox(
              width: screenSize.width * 0.1,
            ),
            const Icon(
              Icons.local_activity,
              color: Colors.white,
            ),
          ],
        ),
      ),
    );

上述示例将产生以下结果:

形状

参数 shape 可以取三个值:

shape: SlidableBottomAppBarShape.rounded,

此值将产生以下效果:

shape: SlidableBottomAppBarShape.wave,

此值将产生以下效果:

shape: SlidableBottomAppBarShape.roundedCurved,

更多关于Flutter底部滑动应用栏插件slidable_bottom_app_bar的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter底部滑动应用栏插件slidable_bottom_app_bar的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何在Flutter应用中使用slidable_bottom_app_bar插件的示例代码。这个插件允许你创建一个可滑动的底部应用栏,通常用于在应用中实现导航和快速操作。

首先,确保你已经在pubspec.yaml文件中添加了slidable_bottom_app_bar依赖:

dependencies:
  flutter:
    sdk: flutter
  slidable_bottom_app_bar: ^x.y.z  # 替换为最新版本号

然后,运行flutter pub get来安装依赖。

接下来,在你的Flutter应用中,你可以这样使用SlidableBottomAppBar

import 'package:flutter/material.dart';
import 'package:slidable_bottom_app_bar/slidable_bottom_app_bar.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Slidable Bottom App Bar Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  int _currentIndex = 0;
  final List<Widget> _pages = [
    Center(child: Text('Page 1')),
    Center(child: Text('Page 2')),
    Center(child: Text('Page 3')),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _pages[_currentIndex],
      bottomNavigationBar: SlidableBottomAppBar(
        color: Colors.blue,
        notchMargin: 5.0,
        child: BottomAppBar(
          color: Colors.blue,
          shape: CircularNotchedRectangle(),
          notchShape: BeveledRectangleBorder(
            borderRadius: BorderRadius.vertical(
              top: Radius.circular(10),
            ),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              IconButton(
                icon: Icon(Icons.home),
                onPressed: () {
                  setState(() {
                    _currentIndex = 0;
                  });
                },
              ),
              IconButton(
                icon: Icon(Icons.star),
                onPressed: () {
                  setState(() {
                    _currentIndex = 1;
                  });
                },
              ),
              IconButton(
                icon: Icon(Icons.settings),
                onPressed: () {
                  setState(() {
                    _currentIndex = 2;
                  });
                },
              ),
            ],
          ),
        ),
        slideActionIcons: [
          SlideActionIcon(
            caption: 'Menu',
            icon: Icons.menu,
            color: Colors.white,
            onPressed: () {
              // 打开菜单的操作
              ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Menu tapped')));
            },
          ),
          SlideActionIcon(
            caption: 'Search',
            icon: Icons.search,
            color: Colors.white,
            onPressed: () {
              // 打开搜索的操作
              ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Search tapped')));
            },
          ),
        ],
      ),
    );
  }
}

在这个示例中,我们创建了一个包含三个页面的简单应用,每个页面显示不同的文本。底部应用栏使用了SlidableBottomAppBar,它包含一个标准的BottomAppBar和一些滑动操作图标。当用户点击滑动操作图标时,会显示一个SnackBar提示。

注意:

  • SlidableBottomAppBarslideActionIcons属性允许你定义滑动操作图标。
  • BottomAppBar用于定义主要的底部导航按钮。
  • 通过setState方法更新_currentIndex来切换页面。

这个示例提供了一个基础框架,你可以根据自己的需求进一步定制和扩展。

回到顶部