Flutter自定义侧边栏插件side_bar_custom的使用

Flutter自定义侧边栏插件side_bar_custom的使用

描述

此插件允许你创建一个简单且快速的“侧边栏”组件,并提供了许多自定义选项。

如何安装

pubspec.yaml文件中添加以下依赖:

dependencies:
  flutter:
    sdk: flutter
  side_bar_custom: ^1.0.3

如何使用

以下是基本用法示例:

SideBar(
    children: [
        Center(
            child: Text("Dashboard"),
        ),
        Center(
            child: Text("Add User"),
        ),
    ],
    items: [
        SideBarItem(
            text: "Dashboard", 
            icon: Icons.home, 
            tooltipText: "Dashboard page",
        ),
        SideBarItem(
            text: "Add User", 
            icon: Icons.add,
        ),
    ],
),

截图

配置项

SideBarConfig

属性名 描述 是否必填 默认值
backgroundColor 用于更改侧边栏背景颜色 Color(0xff303F9F)
bottomIconColor 用于更改侧边栏底部图标颜色 Color(0xff448AFF)
dividerColor 用于更改侧边栏分隔线颜色 Color(0xff448AFF)
selectedBoxColor 用于更改单个选中项目的背景颜色 Color(0xff3F51B5)
selectedIconColor 用于更改单个选中项目的图标颜色 Color(0xffC5CAE9)
unselectedBoxColor 用于更改单个未选中项目的背景颜色 Color(0xff303F9F)
unselectedIconColor 用于更改单个未选中项目的图标颜色 Color(0xff448AFF)
enableDivider 用于启用侧边栏分隔线 true
enableFloating 用于启用浮动侧边栏 false
enablePageView 用于启用侧边栏子项动画 true
enableResizeBody 用于启用侧边栏主体的大小调整,如果设为false,则子项将有固定大小 true
collapseWidth 用于确定侧边栏折叠时的宽度 60.0
dividerIndent 用于确定分隔线在左右边缘的间距 10.0
dividerThickness 用于确定侧边栏内部分隔线的厚度 0.6
fontSize 用于设置侧边栏项目文本大小(可选)
iconSize 用于设置侧边栏项目图标的大小(可选)
maxWidth 用于确定侧边栏展开时的宽度 260.0
tooltipDecoration 用于设置启用时工具提示的样式(可选)
tooltipTextStyle 用于设置启用时工具提示内文本的样式(可选)
selectedTextStyle 用于设置选中状态下的侧边栏项目文本样式 TextStyle(color: Color(0xffC5CAE9))
unselectedTextStyle 用于设置未选中状态下的侧边栏项目文本样式 TextStyle(color: Color(0xff448AFF))
borderRadius 用于当enableFloating设为true时设置侧边栏的圆角半径 BorderRadius.all(Radius.circular(6))
sideBarCurve 用于指示所有侧边栏动画的曲线 Curves.ease
sideBarAnimationDuration 用于指示所有侧边栏动画的持续时间 Duration(milliseconds: 300)
floatingPadding 用于确定侧边栏周围的间距,仅在enableFloating为true时可用 EdgeInsets.all(8)

SideBarItem

属性名 描述 是否必填
text 用于设置侧边栏项目的文本
tooltipText 用于设置侧边栏项目的工具提示文本(可选)
icon 用于设置侧边栏项目的图标

下一步功能

如果你需要新的功能,请在GitHub问题页面中告诉我。如果它们被批准,我会尽快实现它们。

完整示例

以下是一个完整的示例,展示了如何使用side_bar_custom插件创建一个简单的侧边栏应用。

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

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

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final pages = const [
    Center(
      child: Text("Dashboard"),
    ),
    Center(
      child: Text("Add User"),
    ),
  ];

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('SideBar Custom'),
          backgroundColor: const Color(0xff303F9F),
          elevation: 0,
        ),
        body: SideBar(
          children: pages,
          items: [
            SideBarItem(
              text: "Dashboard",
              icon: Icons.home,
              tooltipText: "Dashboard page",
            ),
            SideBarItem(
              text: "Add User",
              icon: Icons.add,
            ),
          ],
          config: SideBarConfig(
            enablePageView: true,
          ),
        ),
      ),
    );
  }
}

更多关于Flutter自定义侧边栏插件side_bar_custom的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter自定义侧边栏插件side_bar_custom的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中自定义侧边栏插件 side_bar_custom 的一个基本示例。假设你已经将 side_bar_custom 插件添加到了你的 pubspec.yaml 文件中,并且已经运行了 flutter pub get

步骤 1: 添加依赖

首先,确保在你的 pubspec.yaml 文件中添加了 side_bar_custom 依赖:

dependencies:
  flutter:
    sdk: flutter
  side_bar_custom: ^最新版本号

步骤 2: 导入插件

在你的 Dart 文件中导入插件:

import 'package:side_bar_custom/side_bar_custom.dart';

步骤 3: 创建自定义侧边栏

以下是一个完整的示例,展示如何使用 side_bar_custom 创建一个简单的侧边栏。

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Side Bar Custom Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

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

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  int _selectedIndex = 0;
  final List<Widget> _widgetOptions = <Widget>[
    Text('首页'),
    Text('发现'),
    Text('朋友'),
    Text('我的'),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('侧边栏示例'),
      ),
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      drawer: Drawer(
        child: Column(
          children: <Widget>[
            UserAccountsDrawerHeader(
              accountName: Text('用户名'),
              accountEmail: Text('user@example.com'),
              currentAccountPicture: CircleAvatar(
                backgroundColor: Colors.grey,
                child: Icon(Icons.person, size: 64,),
              ),
            ),
            Expanded(
              child: SidebarCustom(
                items: [
                  {
                    "icon": Icons.home,
                    "title": "首页",
                    "onPressed": () {
                      _onItemTapped(0);
                      Navigator.pop(context);
                    },
                  },
                  {
                    "icon": Icons.explore,
                    "title": "发现",
                    "onPressed": () {
                      _onItemTapped(1);
                      Navigator.pop(context);
                    },
                  },
                  {
                    "icon": Icons.people,
                    "title": "朋友",
                    "onPressed": () {
                      _onItemTapped(2);
                      Navigator.pop(context);
                    },
                  },
                  {
                    "icon": Icons.person,
                    "title": "我的",
                    "onPressed": () {
                      _onItemTapped(3);
                      Navigator.pop(context);
                    },
                  },
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

代码解释

  1. 导入必要的包

    • package:flutter/material.dart 用于基本的 Flutter 组件。
    • package:side_bar_custom/side_bar_custom.dart 用于自定义侧边栏。
  2. 创建主应用

    • MyApp 是一个 StatelessWidget,它设置了应用的基本主题和主页。
  3. 创建主页

    • MyHomePage 是一个 StatefulWidget,它包含了一个 _selectedIndex 变量来跟踪当前选中的项。
    • _widgetOptions 列表包含了要显示的不同内容。
  4. 侧边栏

    • Drawer 中,我们使用了 SidebarCustom 小部件,它接受一个包含侧边栏项的列表。
    • 每个项都是一个包含 icontitleonPressed 回调的字典。
  5. 导航

    • 当用户点击侧边栏中的项时,_onItemTapped 方法会被调用,更新 _selectedIndex 并关闭 Drawer

这样,你就创建了一个带有自定义侧边栏的 Flutter 应用。你可以根据需要进一步自定义侧边栏的样式和功能。

回到顶部