Flutter曲线抽屉导航插件curved_drawer_fork的使用

Flutter曲线抽屉导航插件curved_drawer_fork的使用

curved_drawer_fork 是一个 Flutter 小部件,它提供了一个易于使用的动画抽屉。

添加依赖

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

dependencies:
  curved_drawer_fork: ^0.0.1 #最新版本

简单实现

以下是一个简单的实现示例:

Scaffold(
  drawer: CurvedDrawer(
    color: Colors.white,
    labelColor: Colors.black54,
    width: 75.0,
    items: [
      DrawerItem(icon: personIcon),
      // 可选标签文本
      DrawerItem(icon: messageIcon, label: "Messages")
    ],
    onTap: (index) {
      // 处理按钮点击事件
    },
  ),
  body: Container(),
)

属性

  • items: DrawerItem 列表。
  • index: 选中的项索引 - 可以用于更改当前索引或设置初始索引。
  • color: 抽屉的颜色,默认为白色。
  • buttonBackgroundColor: 浮动按钮的背景颜色,默认与 color 属性相同。
  • backgroundColor: 导航栏背景颜色,默认为透明。
  • onTap: 处理对项目的点击事件的函数。
  • animationCurve: 插值器,用于按钮更改动画,默认为 Curves.easeOutCubic
  • animationDuration: 动画持续时间,默认为 Duration(milliseconds: 600)
  • width: 抽屉的宽度,默认最小值为 50.0,最大值为 100.0。
  • isEndDrawer: 如果用作结束抽屉,则设为 true,默认为 false

完整示例

以下是一个完整的示例代码,展示了如何使用 curved_drawer_fork 插件来创建一个带有滑块的抽屉导航:

import 'package:curved_drawer_fork/curved_drawer_fork.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

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

class MyApp extends StatelessWidget {
  // 这个小部件是你的应用的根
  [@override](/user/override)
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
    return MaterialApp(
      title: '动画演示',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Playground(title: '曲线抽屉演示'),
    );
  }
}

class Playground extends StatefulWidget {
  Playground({Key? key, required this.title}) : super(key: key);

  final String title;

  [@override](/user/override)
  _PlaygroundState createState() => _PlaygroundState();
}

class _PlaygroundState extends State<Playground> {
  int index = 0;
  double leftWidth = 75.0;
  int leftTextColor = 1;
  int leftBackgroundColor = 0;
  double rightWidth = 75.0;
  int rightTextColor = 1;
  int rightBackgroundColor = 0;
  final List<Color> colorPallete = [
    Colors.white,
    Colors.black,
    Colors.blue,
    Colors.blueAccent,
    Colors.red,
    Colors.redAccent,
    Colors.teal,
    Colors.orange,
    Colors.pink,
    Colors.purple,
    Colors.lime,
    Colors.green
  ];

  List<DrawerItem> _drawerItems = [
    DrawerItem(icon: Icon(Icons.people), label: "People"),
    DrawerItem(icon: Icon(Icons.trending_up), label: "Trending"),
    DrawerItem(icon: Icon(Icons.tv)),
    DrawerItem(icon: Icon(Icons.work), label: "Work"),
    DrawerItem(icon: Icon(Icons.web)),
    DrawerItem(icon: Icon(Icons.videogame_asset)),
    DrawerItem(icon: Icon(Icons.book), label: "Book"),
    DrawerItem(icon: Icon(Icons.call), label: "Telephone")
  ];

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      drawer: CurvedDrawer(
        index: index,
        width: leftWidth,
        color: colorPallete[leftBackgroundColor],
        buttonBackgroundColor: colorPallete[leftBackgroundColor],
        labelColor: colorPallete[leftTextColor],
        items: _drawerItems,
        onTap: (newIndex) {
          setState(() {
            index = newIndex;
          });
        },
      ),
      endDrawer: CurvedDrawer(
        index: index,
        width: rightWidth,
        color: colorPallete[rightBackgroundColor],
        buttonBackgroundColor: colorPallete[rightBackgroundColor],
        labelColor: colorPallete[rightTextColor],
        isEndDrawer: true,
        items: _drawerItems,
        onTap: (newIndex) {
          setState(() {
            index = newIndex;
          });
        },
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              '当前索引是 $index',
            ),
            SizedBox(
              height: 15,
            ),
            Text("左侧宽度 = $leftWidth"),
            Slider(
              label: "左侧抽屉宽度",
              activeColor: Colors.blue,
              min: 50.0,
              max: 100.0,
              divisions: 50,
              value: leftWidth,
              onChanged: (value) => setState(() => leftWidth = value),
            ),
            SizedBox(
              height: 15,
            ),
            SliderTheme(
                child: Slider(
                  label: "左侧抽屉背景色",
                  activeColor: colorPallete[leftBackgroundColor],
                  min: 0,
                  max: colorPallete.length.toDouble() - 1.0,
                  divisions: colorPallete.length,
                  value: leftBackgroundColor.toDouble(),
                  onChanged: (value) => setState(() {
                    leftBackgroundColor = value.toInt();
                  }),
                ),
                data: SliderThemeData(
                    valueIndicatorTextStyle: TextStyle(color: Colors.grey))),
            SizedBox(
              height: 15,
            ),
            SliderTheme(
                child: Slider(
                  label: "左侧抽屉标签颜色",
                  activeColor: colorPallete[leftTextColor],
                  min: 0,
                  max: colorPallete.length.toDouble() - 1.0,
                  divisions: colorPallete.length,
                  value: leftTextColor.toDouble(),
                  onChanged: (value) => setState(() {
                    leftTextColor = value.toInt();
                  }),
                ),
                data: SliderThemeData(
                    valueIndicatorTextStyle: TextStyle(color: Colors.grey))),
            SizedBox(
              height: 15,
            ),
            Text("右侧宽度 = $rightWidth"),
            Slider(
              label: "右侧抽屉宽度",
              activeColor: Colors.blue,
              min: 50.0,
              max: 100.0,
              divisions: 50,
              value: rightWidth,
              onChanged: (value) => setState(() => rightWidth = value),
            ),
            SizedBox(
              height: 15,
            ),
            SliderTheme(
                child: Slider(
                  label: "右侧抽屉背景色",
                  activeColor: colorPallete[rightBackgroundColor],
                  min: 0,
                  max: colorPallete.length.toDouble() - 1.0,
                  divisions: colorPallete.length,
                  value: rightBackgroundColor.toDouble(),
                  onChanged: (value) => setState(() {
                    rightBackgroundColor = value.toInt();
                  }),
                ),
                data: SliderThemeData(
                    valueIndicatorTextStyle: TextStyle(color: Colors.grey))),
            SizedBox(
              height: 15,
            ),
            SliderTheme(
                child: Slider(
                  label: "右侧抽屉标签颜色",
                  activeColor: colorPallete[rightTextColor],
                  min: 0,
                  max: colorPallete.length.toDouble() - 1.0,
                  divisions: colorPallete.length,
                  value: rightTextColor.toDouble(),
                  onChanged: (value) => setState(() {
                    rightTextColor = value.toInt();
                  }),
                ),
                data: SliderThemeData(
                    valueIndicatorTextStyle: TextStyle(color: Colors.grey))),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter曲线抽屉导航插件curved_drawer_fork的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter曲线抽屉导航插件curved_drawer_fork的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用curved_drawer_fork插件来实现曲线抽屉导航的示例代码。这个插件提供了一个具有曲线动画效果的抽屉导航菜单。

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

dependencies:
  flutter:
    sdk: flutter
  curved_drawer_fork: ^最新版本号  # 请替换为实际最新版本号

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

接下来,你可以按照以下步骤在你的Flutter应用中实现曲线抽屉导航:

1. 导入必要的包

import 'package:flutter/material.dart';
import 'package:curved_drawer_fork/curved_drawer.dart';

2. 创建主应用

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

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

3. 创建主页

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

class _MyHomePageState extends State<MyHomePage> {
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        title: Text('Curved Drawer Demo'),
        leading: IconButton(
          icon: Icon(Icons.menu),
          onPressed: () {
            _scaffoldKey.currentState?.openDrawer();
          },
        ),
      ),
      drawer: CurvedDrawer(
        title: "Menu",
        subTitle: "Options",
        curveSize: 20,
        overlayColor: Colors.black45,
        borderRadius: 20,
        menuList: [
          DrawerItem(
            icon: Icons.home,
            text: "Home",
            onTap: () {
              Navigator.pop(context); // 关闭抽屉
              // 你可以在这里添加跳转到Home页面的逻辑
            },
          ),
          DrawerItem(
            icon: Icons.settings,
            text: "Settings",
            onTap: () {
              Navigator.pop(context); // 关闭抽屉
              // 你可以在这里添加跳转到Settings页面的逻辑
            },
          ),
          // 添加更多菜单项
        ],
      ),
      body: Center(
        child: Text('Hello, Flutter!'),
      ),
    );
  }
}

4. 定义DrawerItem组件(如果需要自定义)

curved_drawer_fork插件可能已经自带了DrawerItem组件,但如果你需要自定义,可以按照如下方式实现:

class DrawerItem extends StatelessWidget {
  final IconData icon;
  final String text;
  final VoidCallback onTap;

  const DrawerItem({Key? key, required this.icon, required this.text, required this.onTap}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: Icon(icon),
      title: Text(text),
      onTap: onTap,
    );
  }
}

5. 运行应用

确保你的开发环境配置正确,然后运行应用。你应该会看到一个带有曲线动画效果的抽屉导航菜单。

这个示例展示了如何使用curved_drawer_fork插件来实现一个基本的曲线抽屉导航。你可以根据需求进一步自定义和扩展这个示例。

回到顶部