Flutter响应式侧边栏插件responsive_sidebar的使用
Flutter响应式侧边栏插件responsive_sidebar的使用
特性
- 响应式设计,可以根据屏幕大小自动调整侧边栏的布局。
- 支持不同的设备类型(手机、平板、桌面)。
- 可以自定义侧边栏的菜单项、标题、图标等。
开始使用
要使用responsive_sidebar
插件,首先需要将其添加到你的pubspec.yaml
文件中。然后,你可以替换普通的Scaffold
为ResponsiveScaffold
并添加菜单。
使用示例
以下是一个完整的示例,展示了如何在Flutter应用中使用responsive_sidebar
插件。
import 'package:flutter/material.dart';
import 'package:responsive_sidebar/responsive_sidebar.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
themeMode: ThemeMode.dark,
theme: ThemeData(
primarySwatch: Colors.cyan,
),
darkTheme:
ThemeData(primarySwatch: Colors.cyan, brightness: Brightness.light),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
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> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return ResponsiveScaffold(
appBar: AppBar(
title: const Text("Example Sidebar Scaffold"),
actions: const [],
elevation: 0,
),
menuTitle: const Text("Example Sidebar Scaffold"),
// 当在平板或桌面使用时,使侧边栏宽度更大。
railWidth: 66,
breakpointShowFullMenu: 900,
extendBodyBehindAppBar: false,
extendBody: true,
menuItems: [
MenuItemInfo(
id: "Dashboard",
icon: Icons.dashboard,
label: "Dashboard",
),
MenuItemInfo(id: "Pacientes", icon: Icons.people, label: "Pacientes"),
MenuItemInfo(
id: "Estudios", icon: Icons.medical_services, label: "Estudios"),
],
menuHeader: const _UserProfile(railWidth: 52),
menuFooter: const Padding(
padding: EdgeInsets.all(8.0),
child: Text("Version 1.0"),
),
onSelect: (id) {
debugPrint(id);
},
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
/// 一个用户头像小部件,我们在菜单中用它作为前导小部件。
///
/// 演示应用的模拟UI,它不执行任何实际功能。
class _UserProfile extends StatefulWidget {
const _UserProfile({
Key? key,
required this.railWidth,
}) : super(key: key);
final double railWidth;
[@override](/user/override)
_UserProfileState createState() => _UserProfileState();
}
class _UserProfileState extends State<_UserProfile> {
bool _collapsed = true;
[@override](/user/override)
Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context);
final TextTheme textTheme = theme.textTheme;
final TextTheme primaryTextTheme = theme.primaryTextTheme;
const double hPadding = 5;
return LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Material(
color: theme.colorScheme.surface,
child: Column(
children: [
Row(
children: [
Padding(
padding: const EdgeInsets.all(10.0),
child: CircleAvatar(
backgroundColor: theme.colorScheme.primary,
radius: widget.railWidth / 2 - hPadding,
child: Text(
'MR',
style: primaryTextTheme.titleMedium!.copyWith(
color: theme.colorScheme.onPrimary,
fontWeight: FontWeight.w600),
),
),
),
if (constraints.maxWidth > widget.railWidth * 2) ...[
Expanded(
child: Text(
'Marcos Rodriguez',
maxLines: 1,
style: textTheme.titleMedium!
.copyWith(fontWeight: FontWeight.w600),
),
),
ExpandIcon(
isExpanded: !_collapsed,
size: 24,
padding: EdgeInsets.zero,
onPressed: (_) {
setState(() {
_collapsed = !_collapsed;
});
},
),
]
],
),
AnimatedSwitcher(
duration: const Duration(milliseconds: 200),
transitionBuilder: (Widget child, Animation<double> animation) {
return SizeTransition(
sizeFactor: animation,
child: child,
);
},
child: _collapsed
? const SizedBox.shrink()
: Padding(
padding: const EdgeInsets.only(bottom: 8),
child: Row(
children: [
const Spacer(),
TextButton(
onPressed: () {},
child: Column(
children: [
const Icon(Icons.person),
Text('Profile', style: textTheme.labelSmall),
],
),
),
const SizedBox(width: 10),
TextButton(
onPressed: () {},
child: Column(
children: [
const Icon(Icons.logout),
Text('Sign out', style: textTheme.labelSmall),
],
),
),
const SizedBox(width: 8),
],
),
),
),
],
),
);
},
);
}
}
更多关于Flutter响应式侧边栏插件responsive_sidebar的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter响应式侧边栏插件responsive_sidebar的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
responsive_sidebar
是一个 Flutter 插件,用于创建响应式侧边栏。它可以根据屏幕宽度自动调整侧边栏的显示方式,例如在宽屏上显示为侧边栏,在窄屏上显示为抽屉式菜单。以下是如何使用 responsive_sidebar
插件的基本步骤。
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 responsive_sidebar
插件的依赖:
dependencies:
flutter:
sdk: flutter
responsive_sidebar: ^0.1.0 # 请检查最新版本
然后运行 flutter pub get
来获取依赖。
2. 导入包
在你的 Dart 文件中导入 responsive_sidebar
包:
import 'package:responsive_sidebar/responsive_sidebar.dart';
3. 使用 ResponsiveSidebar
ResponsiveSidebar
是一个小部件,你可以将其添加到你的应用布局中。以下是一个简单的示例:
import 'package:flutter/material.dart';
import 'package:responsive_sidebar/responsive_sidebar.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Responsive Sidebar Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Responsive Sidebar Example'),
),
body: ResponsiveSidebar(
drawerWidth: 200, // 抽屉的宽度
sidebarWidth: 250, // 侧边栏的宽度
breakpoint: 600, // 切换抽屉和侧边栏的断点
sidebar: SidebarContent(), // 侧边栏内容
body: MainContent(), // 主内容
),
);
}
}
class SidebarContent extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
decoration: BoxDecoration(
color: Colors.blue,
),
child: Text(
'Sidebar Header',
style: TextStyle(
color: Colors.white,
fontSize: 24,
),
),
),
ListTile(
leading: Icon(Icons.home),
title: Text('Home'),
onTap: () {
// 处理点击事件
},
),
ListTile(
leading: Icon(Icons.settings),
title: Text('Settings'),
onTap: () {
// 处理点击事件
},
),
],
);
}
}
class MainContent extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Center(
child: Text('Main Content'),
);
}
}