Flutter滑动抽屉增强插件flutter_slider_drawer_enhanced的使用

Flutter滑动抽屉增强插件flutter_slider_drawer_enhanced的使用

标题

Flutter滑动抽屉增强插件flutter_slider_drawer_enhanced的使用

内容

  • 简介

  • 功能

    • 自定义动画时间的滑动效果
    • 提供基本的Appbar,支持颜色、大小和标题的定制
    • 动态滑动打开和关闭偏移量
    • 提供抽屉图标动画
    • 提供主屏幕阴影,支持阴影颜色、模糊半径和扩散半径的定制
    • 支持RTL(右到左)、LTR(左到右)和TTB(上到下)滑动选择
    • 提供自定义Appbar支持,也可以使用插件AppBar与SliderAppBar widget一起使用
    • 如果使用CupertinoApp,请传递isCupertino: true
  • 示例代码

import 'package:flutter/material.dart';
import 'package:flutter_slider_drawer_enhanced/flutter_slider_drawer.dart';

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

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

  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  final GlobalKey<SliderDrawerState> _sliderDrawerKey = GlobalKey<SliderDrawerState>();
  late String title;

  @override
  void initState() {
    title = "Home";
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(fontFamily: 'BalsamiqSans'),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: SliderDrawer(
          appBar: SliderAppBar(
              appBarColor: Colors.white,
              title: Text(title,
                  style: const TextStyle(fontSize: 22, fontWeight: FontWeight.w700))),
          key: _sliderDrawerKey,
          sliderOpenSize: 179,
          slider: _SliderView(
            onItemClick: (title) {
              _sliderDrawerKey.currentState!.closeSlider();
              setState(() {
                this.title = title;
              });
            },
          ),
          child: _AuthorList(),
        ),
      ),
    );
  }
}

class _SliderView extends StatelessWidget {
  final Function(String)? onItemClick;

  const _SliderView({Key? key, this.onItemClick}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      padding: const EdgeInsets.only(top: 30),
      child: ListView(
        children: [
          const SizedBox(height: 30),
          CircleAvatar(
            radius: 65,
            backgroundColor: Colors.grey,
            child: CircleAvatar(
              radius: 60,
              backgroundImage: Image.network(
                      'https://nikhilvadoliya.github.io/assets/images/nikhil_1.webp')
                  .image,
            ),
          ),
          const SizedBox(height: 20),
          const Text(
            'Nick',
            textAlign: TextAlign.center,
            style: TextStyle(
              color: Colors.black,
              fontWeight: FontWeight.bold,
              fontSize: 30,
            ),
          ),
          const SizedBox(height: 20),
          ...[
            Menu(Icons.home, 'Home'),
            Menu(Icons.add_circle, 'Add Post'),
            Menu(Icons.notifications_active, 'Notification'),
            Menu(Icons.favorite, 'Likes'),
            Menu(Icons.settings, 'Setting'),
            Menu(Icons.arrow_back_ios, 'LogOut')
          ]
              .map((menu) =&gt; _SliderMenuItem(
                  title: menu.title,
                  iconData: menu.iconData,
                  onTap: onItemClick))
              .toList(),
        ],
      ),
    );
  }
}

class _SliderMenuItem extends StatelessWidget {
  final String title;
  final IconData iconData;
  final Function(String)? onTap;

  const _SliderMenuItem(
      {Key? key,
      required this.title,
      required this.iconData,
      required this.onTap})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListTile(
        title: Text(title,
            style: const TextStyle(
                color: Colors.black, fontFamily: 'BalsamiqSans_Regular')),
        leading: Icon(iconData, color: Colors.black),
        onTap: () =&gt; onTap?.call(title));
  }
}

class _AuthorList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    List&lt;Quotes&gt; quotesList = [];
    quotesList.add(Quotes(Colors.amber, 'Amelia Brown',
        'Life would be a great deal easier if dead things had the decency to remain dead.'));
    quotesList.add(Quotes(Colors.orange, 'Olivia Smith',
        'That proves you are unusual," returned the Scarecrow'));
    quotesList.add(Quotes(Colors.deepOrange, 'Sophia Jones',
        'Her name badge read: Hello! My name is DIE, DEMIGOD SCUM!'));
    quotesList.add(Quotes(Colors.red, 'Isabella Johnson',
        'I am about as intimidating as a butterfly.'));
    quotesList.add(Quotes(Colors.purple, 'Emily Taylor',
        'Never ask an elf for help; they might decide your better off dead, eh?'));
    quotesList
        .add(Quotes(Colors.green, 'Maya Thomas', 'Act first, explain later'));

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 10),
      child: ListView.separated(
          scrollDirection: Axis.vertical,
          padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
          itemBuilder: (builder, index) {
            return LimitedBox(
              maxHeight: 150,
              child: Container(
                decoration: BoxDecoration(
                    color: quotesList[index].color,
                    borderRadius: const BorderRadius.all(
                      Radius.circular(10.0),
                    )),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Padding(
                      padding: const EdgeInsets.all(12),
                      child: Text(
                        quotesList[index].author,
                        style: const TextStyle(
                            fontFamily: 'BalsamiqSans_Blod',
                            fontSize: 30,
                            color: Colors.white),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(10),
                      child: Text(
                        quotesList[index].quote,
                        style: const TextStyle(
                            fontFamily: 'BalsamiqSans_Regular',
                            fontSize: 15,
                            color: Colors.white),
                      ),
                    )
                  ],
                ),
              ),
            );
          },
          separatorBuilder: (builder, index) {
            return const Divider(
              height: 10,
              thickness: 0,
            );
          },
          itemCount: quotesList.length),
    );
  }
}

class Quotes {
  final MaterialColor color;
  final String author;
  final String quote;

  Quotes(this.color, this.author, this.quote);
}

class Menu {
  final IconData iconData;
  final String title;

  Menu(this.iconData, this.title);
}

示例demo


更多关于Flutter滑动抽屉增强插件flutter_slider_drawer_enhanced的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter滑动抽屉增强插件flutter_slider_drawer_enhanced的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何使用 flutter_slider_drawer_enhanced 插件的示例代码。这个插件提供了一个增强的滑动抽屉(Drawer)组件,具有更丰富的动画效果和自定义选项。

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

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

然后运行 flutter pub get 来获取依赖。

以下是一个简单的示例,展示了如何使用 flutter_slider_drawer_enhanced 插件来创建一个带有滑动抽屉的应用:

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

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

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

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

class _MyHomePageState extends State<MyHomePage> {
  final GlobalKey<SliderDrawerState> _drawerKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Slider Drawer Enhanced Demo'),
        leading: IconButton(
          icon: Icon(Icons.menu),
          onPressed: () => _drawerKey.currentState?.openDrawer(),
        ),
      ),
      body: Center(
        child: Text('Hello, Flutter!'),
      ),
      drawer: SliderDrawer(
        key: _drawerKey,
        controller: SliderDrawerController(),
        child: Container(
          color: Colors.white,
          child: ListView(
            // 重要:在ListView中添加一个padding来避免内容被覆盖
            padding: EdgeInsets.zero,
            children: <Widget>[
              DrawerHeader(
                decoration: BoxDecoration(
                  color: Colors.blue,
                ),
                child: Text('Drawer Header'),
              ),
              ListTile(
                leading: Icon(Icons.home),
                title: Text('Home'),
                onTap: () {
                  Navigator.pop(context); // 关闭抽屉
                },
              ),
              ListTile(
                leading: Icon(Icons.settings),
                title: Text('Settings'),
                onTap: () {
                  Navigator.pop(context); // 关闭抽屉
                },
              ),
            ],
          ),
        ),
        // 你可以在这里配置更多选项,例如抽屉的位置、动画曲线等
        drawerPosition: DrawerPosition.start,
        overlayColor: Colors.black54.withOpacity(0.5),
        borderRadius: 20.0,
        animationCurve: Curves.easeInOutQuad,
        animationDuration: Duration(milliseconds: 300),
      ),
    );
  }
}

在这个示例中,我们做了以下几件事:

  1. 添加依赖:在 pubspec.yaml 文件中添加了 flutter_slider_drawer_enhanced 依赖。
  2. 创建应用:创建了一个简单的 Flutter 应用,包含一个 Scaffold,其中有一个 AppBar 和一个居中的文本。
  3. 添加抽屉:在 Scaffolddrawer 属性中使用了 SliderDrawer 组件。我们传递了一个 GlobalKey 来控制抽屉的打开和关闭,以及一个 SliderDrawerController 来管理抽屉的状态。
  4. 配置抽屉:配置了抽屉的位置、覆盖颜色、边框半径、动画曲线和动画持续时间等属性。

你可以根据需要进一步自定义 SliderDrawer 的其他属性,以满足你的应用需求。

回到顶部