{"id":9090,"date":"2024-05-14T09:01:01","date_gmt":"2024-05-14T01:01:01","guid":{"rendered":""},"modified":"2024-05-14T09:01:01","modified_gmt":"2024-05-14T01:01:01","slug":"Flutter\u5e93\u63a8\u8350sizer\u53ef\u5e2e\u52a9\u60a8\u8f7b\u677e\u521b\u5efa\u54cd\u5e94\u5f0fUI","status":"publish","type":"post","link":"https:\/\/mushiming.com\/9090.html","title":{"rendered":"Flutter\u5e93\u63a8\u8350sizer\u53ef\u5e2e\u52a9\u60a8\u8f7b\u677e\u521b\u5efa\u54cd\u5e94\u5f0fUI"},"content":{"rendered":"
\u4e00\u4e2a Flutter \u63d2\u4ef6\uff0c\u7528\u4e8e\u8f7b\u677e\u4f7f Flutter \u5e94\u7528\u7a0b\u5e8f\u54cd\u5e94\u3002\u81ea\u52a8\u4f7f UI \u9002\u5e94\u4e0d\u540c\u7684\u5c4f\u5e55\u5c3a\u5bf8\u3002\u54cd\u5e94\u53d8\u5f97\u7b80\u5355\u3002<\/p>\n<\/p>\n
<\/p>\n<\/p>\n
<\/p>\n
\u6dfb\u52a0\u5230 pubspec.yaml\u3002<\/p>\n<\/p>\n
dependencies:<\/code> ...<\/code> sizer: ^2.0.15<\/code>\n\n<\/pre>\n\u53c2\u6570\u2699\ufe0f<\/h3>\n\n- \n
.h<\/code>- \u8fd4\u56de\u57fa\u4e8e\u8bbe\u5907\u8ba1\u7b97\u7684\u9ad8\u5ea6<\/p>\n<\/li>\n- \n
.w<\/code>- \u6839\u636e\u8bbe\u5907\u8fd4\u56de\u8ba1\u7b97\u51fa\u7684\u5bbd\u5ea6<\/p>\n<\/li>\n- \n
.sp<\/code>- \u8fd4\u56de\u57fa\u4e8e\u8bbe\u5907\u8ba1\u7b97\u7684 sp<\/p>\n<\/li>\n- \n
SizerUtil.orientation<\/code>- \u7528\u4e8e\u5c4f\u5e55\u65b9\u5411\u7eb5\u5411\u6216\u6a2a\u5411<\/p>\n<\/li>\n- \n
SizerUtil.deviceType<\/code>- \u9002\u7528\u4e8e\u8bbe\u5907\u7c7b\u578b\u624b\u673a\u6216\u5e73\u677f\u7535\u8111<\/p>\n<\/li>\n<\/ul>\n\u7528\u6cd5\ud83d\udcbb<\/h3>\n\u5c06\u4ee5\u4e0b\u5bfc\u5165\u6dfb\u52a0\u5230\u60a8\u7684 Dart \u4ee3\u7801\u4e2d\uff1a<\/h4>\nimport 'package:sizer\/sizer.dart';<\/code>\n<\/pre>\n<\/p>\n\u7528 ResponsiveSizer \u5c0f\u90e8\u4ef6\u5305\u88c5 MaterialApp<\/h4>\nSizer(<\/code> builder: (context, orientation, deviceType) {\n \n <\/code> return MaterialApp(<\/code> debugShowCheckedModeBanner: false,<\/code> title: 'Sizer',<\/code> theme: ThemeData.light(),<\/code> home: homePage(),<\/code> );<\/code> },<\/code> )<\/code>\n\n<\/pre>\n\u6bcf\u5f53\u60a8\u4f7f\u7528\u9ad8\u5ea6\u548c\u5bbd\u5ea6\u65f6\uff0c\u9996\u5148\u5bfc\u5165 sizer \u5305\u3002<\/p>\n<\/p>\n
import 'package:sizer\/sizer.dart';<\/code>\n<\/pre>\n<\/p>\n\u5c0f\u90e8\u4ef6\u5927\u5c0f\ud83d\udd53<\/h4>\n Container(<\/code> width: 20.w, \/\/It will take a 20% of screen width<\/code> height:30.h \/\/It will take a 30% of screen height<\/code> )<\/code>\n\n<\/pre>\n\u586b\u5145\u23f9<\/h4>\n Padding(<\/code> padding: EdgeInsets.symmetric(vertical: 5.h, horizontal: 3.h),<\/code> child: Container(),<\/code> );<\/code>\n\n<\/pre>\n\u5b57\u4f53\u5927\u5c0f\ud83c\udd8e<\/h4>\n Text(<\/code> 'Sizer',style: TextStyle(fontSize: 15.sp),<\/code> );<\/code>\n\n<\/pre>\n\u65b9\u5f62\u5c0f\u90e8\u4ef6\ud83d\udfe9<\/h4>\n
\u5982\u679c\u4f60\u60f3\u5236\u4f5c\u65b9\u5f62\u5927\u5c0f\u7684\u5c0f\u90e8\u4ef6\uff0c\u90a3\u4e48\u5728\u9ad8\u5ea6\u548c\u5bbd\u5ea6\u4e2d\u7ed9\u51fa\u9ad8\u5ea6\u6216\u5bbd\u5ea6\u3002<\/p>\n<\/p>\n
Container(<\/code> width: 30.h, \/\/It will take a 30% of screen height<\/code> height: 30.h, \/\/It will take a 30% of screen height<\/code> );<\/code>\n<\/pre>\n\u65b9\u5411\ud83d\udd04<\/h4>\n
\u5982\u679c\u60a8\u60f3\u540c\u65f6\u652f\u6301\u7eb5\u5411\u548c\u6a2a\u5411<\/p>\n
Device.orientation == Orientation.portrait<\/code> ? Container( \/\/ Widget for Portrait<\/code> width: 100.w,<\/code> height: 20.5.h,<\/code> )<\/code> : Container( \/\/ Widget for Landscape<\/code> width: 100.w,<\/code> height: 12.5.h,<\/code> )<\/code>\n\n<\/pre>\n\u8bbe\u5907\u7c7b\u578b\ud83d\udcf1<\/h4>\n
\u5982\u679c\u60a8\u5e0c\u671b\u76f8\u540c\u7684\u5e03\u5c40\u5728\u5e73\u677f\u7535\u8111\u548c\u79fb\u52a8\u8bbe\u5907\u4e2d\u770b\u8d77\u6765\u4e0d\u540c\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0bSizerUtil.deviceType<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\nSizerUtil.deviceType == DeviceType.mobile<\/code> ? Container( \/\/ Widget for Mobile<\/code> width: 100.w,<\/code> height: 20.5.h,<\/code> )<\/code> : Container( \/\/ Widget for Tablet<\/code> width: 100.w,<\/code> height: 12.5.h,<\/code> )<\/code>\n\n<\/pre>\n\u5efa\u8bae<\/h3>\n
\u65b9\u5411<\/strong><\/p>\n\u5982\u679c\u60a8\u60f3\u540c\u65f6\u652f\u6301\u7eb5\u5411\u548c\u6a2a\u5411\uff0c\u8bf7\u4e3a\u4e24\u4e2a\u7c7b\u4f3c\u65b9\u5411\u793a\u4f8b\u5236\u4f5c\u5355\u72ec\u7684\u5c0f\u90e8\u4ef6\u3002<\/p>\n
\u8bbe\u5907\u7c7b\u578b<\/strong><\/p>\n\u5982\u679c\u60a8\u60f3\u540c\u65f6\u652f\u6301\u79fb\u52a8\u8bbe\u5907\u548c\u5e73\u677f\u7535\u8111\uff0c\u8bf7\u4e3a\u4e24\u8005\u5236\u4f5c\u5355\u72ec\u7684\u5c0f\u90e8\u4ef6\uff0c\u4f8b\u5982 deviceType \u793a\u4f8b\u3002<\/p>\n
\u6ce8\u610f<\/h3>\n
\u60a8\u9700\u8981\u5bfc\u5165sizer<\/code>\u5305\u624d\u80fd\u8bbf\u95eenumber.h<\/code>\u3001number.w<\/code>\u548cnumber.sp<\/code><\/p>\n**VSCode \u548c Android Studio \u4e2d\u7684\u81ea\u52a8\u5bfc\u5165\u4e0d\u9002\u7528\u4e8e dart \u6269\u5c55\u65b9\u6cd5\u3002**\u952e\u516510.h<\/code>\u4e0d\u4f1a\u663e\u793a\u6b64\u5305\u7684\u81ea\u52a8\u5bfc\u5165\u5efa\u8bae<\/p>\n\u4e00\u79cd\u89e3\u51b3\u65b9\u6cd5\u662f\u952e\u5165Device<\/code>\u4ee5\u4fbf\u663e\u793a\u81ea\u52a8\u5bfc\u5165\u5efa\u8bae\uff1a<\/p>\n<\/p>\nimport 'package:sizer\/sizer.dart';<\/code>\n<\/pre>\n\u8fd9\u4e5f\u662f\u4e00\u4e2a\u5f88\u4e0d\u9519\u7684\u5305\uff0c\u5e0c\u671b\u5927\u5bb6\u559c\u6b22\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Flutter\u5e93\u63a8\u8350sizer\u53ef\u5e2e\u52a9\u60a8\u8f7b\u677e\u521b\u5efa\u54cd\u5e94\u5f0fUI\u4e00\u4e2aFlutter\u63d2\u4ef6\uff0c\u7528\u4e8e\u8f7b\u677e\u4f7fFlutter\u5e94\u7528\u7a0b\u5e8f\u54cd\u5e94","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/9090"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=9090"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/9090\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=9090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=9090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=9090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}