Flutter嵌套侧边栏插件nested_side_sheet的使用
Flutter嵌套侧边栏插件nested_side_sheet的使用
🕵️ 动机
这个仓库包含一个插件,该插件允许在顶部显示多个侧边栏,并且能够执行典型的导航操作,如push
、replace
、pop
和popUntil
。我们在构建一个Web仪表板应用时开发了这个插件,发现标准的Material导航抽屉和现有的插件无法提供我们所需的特性。
我们的插件设计简单且灵活,允许您自定义侧边栏的外观和过渡动画而没有任何限制。通过我们的插件,您可以创建满足特定需求的无缝用户体验。
⚙ 特性
- 显示多个堆叠在一起的侧边栏
- 执行典型的导航操作,如
push
、replace
、pop
和popUntil
- 自定义侧边栏的外观和感觉
- 创建无缝的过渡动画
💻 安装
dependencies:
nested_side_sheet: ^1.1.0
🕹️ 使用
import 'package:flutter/material.dart';
import 'package:nested_side_sheet/nested_side_sheet.dart';
const kSideSheetWidth = 304.0;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) =>
MaterialApp(
// Side sheets will be placed above the NestedSideSheet child
home: NestedSideSheet(
child: HomePage(),
),
);
}
class HomePage extends StatelessWidget {
constHomePage({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) =>
Scaffold(
appBar: AppBar(
leading: IconButton(
onPressed: () async {
// Call this anywhere in the app within the NestedSideSheet context
final result = await NestedSideSheet.of(context).push(
Container(
color: Colors.white,
width: kSideSheetWidth,
),
transitionBuilder: leftSideSheetTransition,
position: SideSheetPosition.left,
);
},
icon: Icon(Icons.add),
),
),
);
}
示例代码
import 'package:flutter/material.dart';
import 'package:nested_side_sheet/nested_side_sheet.dart';
const kSideSheetWidth = 304.0;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) => MaterialApp(
title: 'Nested Side Sheet',
theme: lightTheme(),
debugShowCheckedModeBanner: false,
home: Material(
color: accentColor,
// Side sheets will be placed above the NestedSideSheet's child
child: NestedSideSheet(
child: MyHomePage(title: 'Nested Side Sheet'),
),
),
);
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
[@override](/user/override)
Widget build(BuildContext context) => HomeCard(
shadows: kElevationToShadow[8],
child: Scaffold(
body: Stack(
children: [
buildInfoCard(),
...buildButtons(),
],
),
),
);
Widget buildInfoCard() => Center(
child: Container(
width: 300,
height: 150,
decoration: ShapeDecoration(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
side: BorderSide(color: accentColor),
),
),
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GestureDetector(
onTap: () => launchUrl(Uri.https('krootl.com')),
child: MouseRegion(
cursor: SystemMouseCursors.click,
child: ClipRRect(
borderRadius: BorderRadius.circular(2),
child: SvgPicture.asset(
'assets/krootl_logo.svg',
height: 28,
),
),
),
),
const SizedBox(width: 8),
Text(
widget.title,
style: Theme.of(context).appBarTheme.titleTextStyle,
),
],
),
),
),
);
List<Widget> buildButtons() => [
Align(
alignment: Alignment.center,
child: Padding(
padding: const EdgeInsets.only(right: 300),
child: FloatingActionButton(
onPressed: openLeftSheet,
child: Icon(Icons.arrow_back_rounded),
),
),
),
Align(
alignment: Alignment.center,
child: Padding(
padding: const EdgeInsets.only(bottom: 150),
child: FloatingActionButton(
onPressed: openTopSheet,
child: Icon(Icons.arrow_upward_rounded),
),
),
),
Align(
alignment: Alignment.center,
child: Padding(
padding: const EdgeInsets.only(left: 300),
child: FloatingActionButton(
onPressed: openRightSheet,
child: Icon(Icons.arrow_forward_rounded),
),
),
),
Align(
alignment: Alignment.center,
child: Padding(
padding: const EdgeInsets.only(top: 150),
child: FloatingActionButton(
onPressed: openBottomSheet,
child: Icon(Icons.arrow_downward_rounded),
),
),
),
];
void openLeftSheet() async {
ScaffoldMessenger.of(context).clearSnackBars();
final result = await NestedSideSheet.of(context).push(
Sheet(
size: Size(376, MediaQuery.of(context).size.height),
),
transitionBuilder: leftSideSheetTransition,
position: SideSheetPosition.left,
);
if (result is String) showSnackBar(result);
}
void openTopSheet() async {
ScaffoldMessenger.of(context).clearSnackBars();
final decorationBuilder = (child) => HomeCard(child: child);
final result = await NestedSideSheet.of(context).push(
Sheet(
size: Size(MediaQuery.of(context).size.width, 400),
),
transitionBuilder: topSideSheetTransition,
position: SideSheetPosition.top,
decorationBuilder: decorationBuilder,
);
if (result is String) showSnackBar(result);
}
void openRightSheet() async {
ScaffoldMessenger.of(context).clearSnackBars();
final decorationBuilder = (child) => HomeCard(child: child);
final result = await NestedSideSheet.of(context).push(
Sheet(
size: Size(
MediaQuery.of(context).size.width * (1 / 3),
MediaQuery.of(context).size.height,
),
),
transitionBuilder: rightSideSheetTransition,
position: SideSheetPosition.right,
decorationBuilder: decorationBuilder,
dismissible: true,
);
if (result is String) showSnackBar(result);
}
void openBottomSheet() async {
ScaffoldMessenger.of(context).clearSnackBars();
final result = await NestedSideSheet.of(context).push(
Sheet(
size: Size(MediaQuery.of(context).size.width, 400),
),
transitionBuilder: bottomSideSheetTransition,
position: SideSheetPosition.bottom,
);
if (result is String) showSnackBar(result);
}
void showSnackBar(String result) =>
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(result, textAlign: TextAlign.center),
width: 300,
behavior: SnackBarBehavior.floating,
),
);
}
更多关于Flutter嵌套侧边栏插件nested_side_sheet的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter嵌套侧边栏插件nested_side_sheet的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何在Flutter中使用nested_side_sheet
插件来实现嵌套侧边栏的示例代码。nested_side_sheet
是一个强大的Flutter插件,允许你在应用中嵌套多个侧边栏。
首先,确保你已经在pubspec.yaml
文件中添加了nested_side_sheet
的依赖:
dependencies:
flutter:
sdk: flutter
nested_side_sheet: ^x.y.z # 请替换为最新版本号
然后,运行flutter pub get
来安装依赖。
接下来,在你的Dart文件中,你可以按照以下方式使用nested_side_sheet
插件:
import 'package:flutter/material.dart';
import 'package:nested_side_sheet/nested_side_sheet.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Nested Side Sheet Example'),
),
body: NestedSideSheetScaffold(
sheetBuilder: (context, state) {
return NestedSideSheet(
// 设置侧边栏的宽度
sheetWidth: 250.0,
// 侧边栏的标题
header: Padding(
padding: EdgeInsets.all(16.0),
child: Text('Side Sheet Header'),
),
// 侧边栏的内容
body: ListView(
children: <Widget>[
ListTile(
title: Text('Option 1'),
onTap: () {
state.closeSheet();
// 处理选项1的点击事件
},
),
ListTile(
title: Text('Option 2'),
onTap: () {
state.closeSheet();
// 处理选项2的点击事件
},
),
// 可以添加更多的选项
],
),
);
},
// 主内容区域
contentBuilder: (context) {
return Center(
child: ElevatedButton(
onPressed: () {
// 打开侧边栏
NestedSideSheetScaffold.of(context).openSheet();
},
child: Text('Open Side Sheet'),
),
);
},
),
),
);
}
}
在这个示例中:
- 我们使用
NestedSideSheetScaffold
作为主框架,它包含了两个主要的构建器:sheetBuilder
和contentBuilder
。 sheetBuilder
用于构建侧边栏的内容。在这个例子中,我们创建了一个NestedSideSheet
,并设置了宽度、标题和主体内容。contentBuilder
用于构建主内容区域,在这个例子中,我们放了一个按钮,点击按钮时会打开侧边栏。- 使用
NestedSideSheetScaffold.of(context).openSheet();
来打开侧边栏。
你可以根据需求自定义侧边栏和主内容区域的内容。nested_side_sheet
插件还提供了更多高级功能,比如嵌套多个侧边栏、动画效果等,你可以查阅其官方文档获取更多信息。