플러터에 반응
La siguiente clase me ayuda a definir los tamaños.
import 'package:flutter/widgets.dart';
class SizeConfig {
static double screenWidth = 0;
static double screenHeight = 0;
static double _blockSizeHorizontal = 0;
static double _blockSizeVertical = 0;
static double _safeAreaHorizontal = 0;
static double _safeAreaVertical = 0;
static double _safeBlockHorizontal = 0;
static double _safeBlockVertical = 0;
SizeConfig(BuildContext context) {
var mediaQueryContext = MediaQuery.of(context);
screenWidth = mediaQueryContext.size.width;
screenHeight = mediaQueryContext.size.height;
_blockSizeHorizontal = screenWidth / 100;
_blockSizeVertical = screenHeight / 100;
_safeAreaHorizontal =
mediaQueryContext.padding.left + mediaQueryContext.padding.right;
_safeAreaVertical =
mediaQueryContext.padding.top + mediaQueryContext.padding.bottom;
_safeBlockHorizontal = (screenWidth - _safeAreaHorizontal) / 100;
_safeBlockVertical = (screenHeight - _safeAreaVertical) / 100;
}
static double blockSizeHorizontal(double percentage) {
return _blockSizeHorizontal * percentage;
}
static double blockSizeVertical(double percentage) {
return _blockSizeVertical * percentage;
}
static double safeBlockSizeHorizontal(double percentage) {
return _safeBlockHorizontal * percentage;
}
static double safeBlockSizeVertical(double percentage) {
return _safeBlockVertical * percentage;
}
}
초기 설정 솔로 생성자 및 SizeConfig en tu initialRoute o en tu home, 의존적인 유틸리티. Solo Recuerda inicializarlo en el primer Widget 후속 MaterialApp.
class Ushieru extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Ushieru 👨💻',
theme: ThemeData(
primarySwatch: Colors.blue,
),
routes: {
IndexScreen.routeName: (context) => IndexScreen(),
},
initialRoute: IndexScreen.routeName,
home: IndexScreen(),
);
}
}
예:
class IndexScreen extends StatelessWidget {
static final routeName = "IndexScreen";
@override
Widget build(BuildContext context) {
SizeConfig(context);
return Container(
width: SizeConfig.screenWidth, // Toma todo el ancho de la pantalla
height: SizeConfig.blockSizeVertical(50), // Toma el 50% del alto de la pantalla
);
}
}
크기 구성에 따라 변수를 사용할 수 있으며 다른 위젯과 함께 사용할 수 있습니다.
class SecondScreen extends StatelessWidget {
static final routeName = "SecondScreen";
@override
Widget build(BuildContext context) {
// SizeConfig(context); No se necesita inicializar otra vez
return Container(
width: SizeConfig.screenWidth,
height: SizeConfig.blockSizeVertical(50),
);
}
}
¿Y que pasa si utilizo un SafeArea para mi aplicación?
블록 크기 수직 및 안전 블록 크기 수직, 단순함을 사전에 설정하세요.
class ThirdScreen extends StatelessWidget {
static final routeName = "ThirdScreen";
@override
Widget build(BuildContext context) {
return SafeArea(
child: Container(
width: SizeConfig.screenWidth,
height: SizeConfig.safeBlockSizeVertical(50), // Usamos el metodo safeBlockSizeVertical
),
);
}
}
Reference
이 문제에 관하여(플러터에 반응), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/ushieru/responsive-in-flutter-5dil텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)