플러터에 반응

12540 단어 uxflutter
La forma más sencilla de tener control en nuestros diseños es imaginar que dividimos nuestra pantalla entre 100 bloques iguales, a lo ancho y a lo alto. 10%에서 35%로 고정된 위젯을 정의할 수 있습니다.

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
      ),
    );
  }
}

좋은 웹페이지 즐겨찾기