https://github.com/ducafecat/Flutter-Responsive-Admin-Panel-or-Dashboard
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KzIkPJb1-1620778176102)(https://ducafecat.tech/2021/05/12/flutter-opensource/flutter-responsive-admin-panel-or-dashboard/2021-05-12-07-33-10.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hNRg3PVS-1620778176104)(https://ducafecat.tech/2021/05/12/flutter-opensource/flutter-responsive-admin-panel-or-dashboard/2021-05-12-07-33-28.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZhZVIB0t-1620778176105)(https://ducafecat.tech/2021/05/12/flutter-opensource/flutter-responsive-admin-panel-or-dashboard/2021-05-12-07-33-50.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9M2E0WhJ-1620778176106)(https://ducafecat.tech/2021/05/12/flutter-opensource/flutter-responsive-admin-panel-or-dashboard/2021-05-12-07-34-24.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-W59NMgaO-1620778176107)(https://ducafecat.tech/2021/05/12/flutter-opensource/flutter-responsive-admin-panel-or-dashboard/2021-05-12-07-38-08.png)]
通过 MediaQuery
查询界面宽高尺寸,来决定你是那个布局方案。
import 'package:flutter/material.dart';
class Responsive extends StatelessWidget {
final Widget mobile;
final Widget tablet;
final Widget desktop;
const Responsive({
Key key,
@required this.mobile,
this.tablet,
@required this.desktop,
}) : super(key: key);
// This size work fine on my design, maybe you need some customization depends on your design
// This isMobile, isTablet, isDesktop helep us later
static bool isMobile(BuildContext context) =>
MediaQuery.of(context).size.width < 850;
static bool isTablet(BuildContext context) =>
MediaQuery.of(context).size.width < 1100 &&
MediaQuery.of(context).size.width >= 850;
static bool isDesktop(BuildContext context) =>
MediaQuery.of(context).size.width >= 1100;
@override
Widget build(BuildContext context) {
final Size _size = MediaQuery.of(context).size;
// If our width is more than 1100 then we consider it a desktop
if (_size.width >= 1100) {
return desktop;
}
// If width it less then 1100 and more then 850 we consider it as tablet
else if (_size.width >= 850 && tablet != null) {
return tablet;
}
// Or less then that we called it mobile
else {
return mobile;
}
}
}
通过 if (!Responsive.isMobile(context))
这样的方式去判断执行
lib/screens/main/main_screen.dart
class MainScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
key: context.read().scaffoldKey,
drawer: SideMenu(),
body: SafeArea(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// We want this side menu only for large screen
if (Responsive.isDesktop(context))
Expanded(
// default flex = 1
// and it takes 1/6 part of the screen
child: SideMenu(),
),
Expanded(
// It takes 5/6 part of the screen
flex: 5,
child: DashboardScreen(),
),
],
),
),
);
}
}
class DashboardScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SafeArea(
child: SingleChildScrollView(
padding: EdgeInsets.all(defaultPadding),
child: Column(
children: [
Header(),
SizedBox(height: defaultPadding),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
flex: 5,
child: Column(
children: [
MyFiels(),
SizedBox(height: defaultPadding),
RecentFiles(),
if (Responsive.isMobile(context))
SizedBox(height: defaultPadding),
if (Responsive.isMobile(context)) StarageDetails(),
],
),
),
if (!Responsive.isMobile(context))
SizedBox(width: defaultPadding),
// On Mobile means if the screen is less than 850 we dont want to show it
if (!Responsive.isMobile(context))
Expanded(
flex: 2,
child: StarageDetails(),
),
],
)
],
),
),
);
}
}
© 猫哥
https://ducafecat.tech/
https://github.com/ducafecat
https://github.com/ducafecat/getx_quick_start
https://github.com/ducafecat/flutter_learn_news
https://getstrapi.cn
https://ducafecat.tech/categories/%E8%AF%91%E6%96%87/
https://space.bilibili.com/404904528/channel/detail?cid=111585
https://space.bilibili.com/404904528/channel/detail?cid=123470
https://space.bilibili.com/404904528/channel/detail?cid=106755
https://space.bilibili.com/404904528/channel/detail?cid=144262
https://space.bilibili.com/404904528/channel/detail?cid=177519
https://space.bilibili.com/404904528/channel/detail?cid=177514
https://space.bilibili.com/404904528/channel/detail?cid=130578