hello everyone i am creating a flutter app and i need to do a simple if statement to decide the device type i can do it inside the original class like this and it will work fine of course
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(....
but instead of putting this code in every page i have, i want to make a class that will do this if statement and it will return font , categoriesFont , navHeight and navIcons variables so that i can use them as much as i need in other pages
i want to do something like this
//this class decides device type and use its corresponding percentages
class SetDeviceType{
final context;
SetDeviceType(this.context);
double font;
double categoriesFont;
double navHeight;
double navIcons;
void deviceType(){
if(DeviceInformation(context).width > 600){
font = fontTablet;
categoriesFont = categoriesTabletFont;
navHeight = navTabletHeight;
navIcons = navTabletIcons;
}else{
font = fontPhone;
categoriesFont = categoriesPhoneFont;
navHeight = navPhoneHeight;
navIcons = navPhoneIcons;
}
}
double get responsiveFont => font;
double get responsiveCategoriesFont => categoriesFont;
double get responsiveNavHeight => navHeight;
double get responsiveNavIcons => navIcons;
}
and then simply i can do
DeviceType(context).font
to get the font variable after deciding the device screen type and use it, but i didn't find a way around it.
the main idea here is to make this if statement reusable and to make the code cleaner.
any idea how i can make this happen ? it doesn't have to be a class anything will do the job is welcome i.e function, etc
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire