Class 在课堂外发表if声明
大家好,我正在创建一个颤振应用程序,我需要做一个简单的if语句来决定 设备类型 我可以在原来的课堂上这样做,当然效果会很好Class 在课堂外发表if声明,class,if-statement,flutter,code-reuse,Class,If Statement,Flutter,Code Reuse,大家好,我正在创建一个颤振应用程序,我需要做一个简单的if语句来决定 设备类型 我可以在原来的课堂上这样做,当然效果会很好 class _MyHomePageState extends State<MyHomePage> { @override Widget build(BuildContext context) { double font; double categoriesFont; double navHeight; double n
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
double font;
double categoriesFont;
double navHeight;
double navIcons;
if(DeviceInformation(context).width > 600){
font = fontTablet;
categoriesFont = categoriesTabletFont;
navHeight = navTabletHeight;
navIcons = navTabletIcons;
}else{
font = fontPhone;
categoriesFont = categoriesPhoneFont;
navHeight = navPhoneHeight;
navIcons = navPhoneIcons;
}
return Scaffold(....
然后我就可以做了
DeviceType(context).font
在决定设备屏幕类型后获取字体变量并使用它,但我没有找到解决方法
这里的主要思想是使这个if语句可重用,并使代码更干净
你知道我该怎么做吗?它不必是一个类,任何东西都可以做,工作是受欢迎的,即函数,等等
提前感谢。如果我正确理解了您的问题,您需要一个可以重用的类,根据设备的宽度提供不同的值 我认为你的解决方案非常接近。您可以将构造函数与主体一起使用,并在那里进行条件赋值。像这样:
import 'package:flutter/material.dart';
class DeviceInformation{
//These fields are just some dummy values i used to test this
//You can have these values wherever
static const fontTablet = 2.0;
static const categoriesTabletFont = 2.0;
static const navTabletHeight = 2.0;
static const navTabletIcons = 2.0;
static const fontPhone = 1.0;
static const categoriesPhoneFont = 1.0;
static const navPhoneHeight = 1.0;
static const navPhoneIcons = 1.0;
double _font;
double _categoriesFont;
double _navHeight;
double _navIcons;
double get responsiveFont => _font;
double get responsiveCategoriesFont => _categoriesFont;
double get responsiveNavHeight => _navHeight;
double get responsiveNavIcons => _navIcons;
DeviceInformation(BuildContext context){
//This gives me the display width
//Context.size.width would throw an error because
//the widget's size hasn't been calculated at this point
if(MediaQuery.of(context).size.width > 600){
_font = fontTablet;
_categoriesFont = categoriesTabletFont;
_navHeight = navTabletHeight;
_navIcons = navTabletIcons;
}else{
_font = fontPhone;
_categoriesFont = categoriesPhoneFont;
_navHeight = navPhoneHeight;
_navIcons = navPhoneIcons;
}
}
}
然后您可以从build调用它
class _MyHomePageState extends State<MyHomePage>{
@override
Widget build(BuildContext context) {
DeviceInformation deviceInfo = DeviceInformation(context);
return Text('${deviceInfo.responsiveFont}');
}
}
class\u MyHomePageState扩展状态{
@凌驾
小部件构建(构建上下文){
DeviceInformation deviceInfo=设备信息(上下文);
返回文本(“${deviceInfo.responsiveFont}”);
}
}
我希望这就是你想要的:D非常感谢这正是这个想法缺少的链部分是构造器*\u^非常感谢。我想问你,我知道如何使用类和状态完整小部件来构建应用程序,但我对它们没有深入的了解。请你参考一本书或一个网站,让我了解flifter类的每个部分,状态完整、无状态,“颤振机制”之类的东西吗?@RYOKSecurity老实说,我不知道该把你引荐到哪里。。我对很多事情还没有深入的了解,随着我的进步,一切都变得越来越有意义。也就是说,Flitter团队在本系列的第4-8段视频中重点介绍了小部件。也许它能帮助你。另外,我发现flutrapi文档可以更好地理解许多元素(尽管有时可能有点混乱)。非常感谢兄弟,我真的很感激^_^
class _MyHomePageState extends State<MyHomePage>{
@override
Widget build(BuildContext context) {
DeviceInformation deviceInfo = DeviceInformation(context);
return Text('${deviceInfo.responsiveFont}');
}
}