Comenzamos la nueva semana con otra interpretación de la documentación oficial de Flutter en un formato de preguntas y respuestas. La Parte 4 cubre el estilo comparativo de Flutter para desarrolladores web. Está completamente dedicado al diseño y no ha salido tan voluminoso como los anteriores. Tradicionalmente, recomiendo a todos los desarrolladores web interesados en Flutter que miren debajo del gato para ver si vale la pena probar este marco y cuánto esfuerzo tomará.
Si no hay suficiente información aquí o si tiene experiencia en desarrollo nativo para una plataforma específica, le recomiendo buscar otras partes:Flutter. Parte 1. Para desarrolladores de AndroidFlutter. Parte 2. Para desarrolladores de iOSFlutter. Parte 3. Para los desarrolladores de React NativeFlutter. Parte 4. Para desarrolladores web deFlutter. Parte 5. Para desarrolladores de Xamarin.FormsContenido:
- Diseño básico
- Posiciones y Tamaños
- La forma
- Texto
Diseño básico
Pregunta:
¿Cómo dar estilo y alinear texto?Responder:
Usando TextStyle .Ejemplo:
HTML / CSS<div class="greybox">
Lorem ipsum
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Georgia;
}
Aleteovar container = Container(
child: Text(
"Lorem ipsum",
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.w900,
fontFamily: "Georgia",
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
Pregunta:
¿Cómo se establece el color de fondo?Responder
Usando la clase BoxDecoration .Las diferencias
Una propiedad background-color
en CSS solo es responsable del color de fondo. BoxDecoration es responsable de una gama más amplia de propiedades, como redondear esquinas, bordes, etc.Ejemplo
HTML / CSS<div class="greybox">
Lorem ipsum
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
}
Aleteovar container = Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
),
width: 320,
height: 240,
decoration: BoxDecoration(
color: Colors.grey[300],
),
);
Pregunta:
¿Cómo centrar los componentes?Responder
Usando el widget del Centro .Ejemplo
HTML / CSS<div class="greybox">
Lorem ipsum
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
Aleteovar container = Container(
child: Center(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
Pregunta:
¿Cómo configurar el ancho del contenedor?Responder
Usando la propiedad width
.Las diferencias
Los widgets Flutter tienen una propiedad width
fija. Para configurar maxWidth
o minWidth
, use el widget BoxConstraints .Ejemplo:
HTML / CSS<div class="greybox">
<div class="redbox">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
width: 100%;
max-width: 240px;
}
Aleteovar container = Container(
child: Center(
child: Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
),
decoration: BoxDecoration(
color: Colors.red[400],
),
padding: EdgeInsets.all(16),
width: 240,
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
Posiciones y Tamaños
Pregunta
¿Cómo establecer una posición absoluta?Responder
Uso del widget Posicionado dentro del widget Pila .Información Adicional
Por defecto, los widgets se colocan dentro de los widgets principales.Ejemplo
HTML / CSS<div class="greybox">
<div class="redbox">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
position: relative;
}
.redbox {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
position: absolute;
top: 24px;
left: 24px;
}
Aleteovar container = Container(
child: Stack(
children: [
Positioned(
child: Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
),
decoration: BoxDecoration(
color: Colors.red[400],
),
padding: EdgeInsets.all(16),
),
left: 24,
top: 24,
),
],
),
width: 320,
height: 240,
color: Colors.grey[300],
);
Pregunta
¿Cómo configurar la rotación de los componentes?Responder
Usando el widget Transformar .Ejemplo
HTML / CSS<div class="greybox">
<div class="redbox">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
transform: rotate(15deg);
}
Aleteovar container = Container(
child: Center(
child: Transform(
child: Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
textAlign: TextAlign.center,
),
decoration: BoxDecoration(
color: Colors.red[400],
),
padding: EdgeInsets.all(16),
),
alignment: Alignment.center,
transform: Matrix4.identity()
..rotateZ(15 * 3.1415927 / 180),
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
Pregunta
¿Cómo escalar componentes?Responder
Usando el widget Transformar .Ejemplo
HTML / CSS<div class="greybox">
<div class="redbox">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
transform: scale(1.5);
}
Aleteovar container = Container(
child: Center(
child: Transform(
child: Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
textAlign: TextAlign.center,
),
decoration: BoxDecoration(
color: Colors.red[400],
),
padding: EdgeInsets.all(16),
),
alignment: Alignment.center,
transform: Matrix4.identity()
..scale(1.5),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
Pregunta
¿Cómo aplicar un gradiente?Responder
Usando la clase BoxDecoration y sus propiedades gradient
.Ejemplo
Gradiente lineal verticalHTML / CSS<div class="greybox">
<div class="redbox">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
padding: 16px;
color: #ffffff;
background: linear-gradient(180deg, #ef5350, rgba(0, 0, 0, 0) 80%);
}
Aleteovar container = Container(
child: Center(
child: Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: const Alignment(0.0, -1.0),
end: const Alignment(0.0, 0.6),
colors: <Color>[
const Color(0xffef5350),
const Color(0x00ef5350)
],
),
),
padding: EdgeInsets.all(16),
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
Gradiente lineal horizontalHTML / CSS<div class="greybox">
<div class="redbox">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
padding: 16px;
color: #ffffff;
background: linear-gradient(90deg, #ef5350, rgba(0, 0, 0, 0) 80%);
}
Aleteovar container = Container(
child: Center(
child: Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: const Alignment(-1.0, 0.0),
end: const Alignment(0.6, 0.0),
colors: <Color>[
const Color(0xffef5350),
const Color(0x00ef5350)
],
),
),
padding: EdgeInsets.all(16),
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
La forma
Pregunta
¿Cómo redondear esquinas?Responder
Usando la clase BoxDecoration y sus propiedades borderRadius
.Ejemplo
HTML / CSS<div class="greybox">
<div class="redbox">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
border-radius: 8px;
}
Aleteovar container = Container(
child: Center(
child: Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
),
decoration: BoxDecoration(
color: Colors.red[400],
borderRadius: BorderRadius.all(
const Radius.circular(8),
),
),
padding: EdgeInsets.all(16),
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
Pregunta
¿Cómo agregar una sombra?Responder
Usando la clase BoxShadow .Información Adicional
BoxShadow se usa como parte boxShadow
de la propiedad de clase BoxDecoration .Ejemplo
HTML / CSS<div class="greybox">
<div class="redbox">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.8),
0 6px 20px rgba(0, 0, 0, 0.5);
}
Aleteovar container = Container(
child: Center(
child: Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
),
decoration: BoxDecoration(
color: Colors.red[400],
boxShadow: [
BoxShadow (
color: const Color(0xcc000000),
offset: Offset(0, 2),
blurRadius: 4,
),
BoxShadow (
color: const Color(0x80000000),
offset: Offset(0, 6),
blurRadius: 20,
),
],
),
padding: EdgeInsets.all(16),
),
),
width: 320,
height: 240,
decoration: BoxDecoration(
color: Colors.grey[300],
),
margin: EdgeInsets.only(bottom: 16),
);
Pregunta
¿Cómo hacer formas redondas y elípticas?Responder
Usando la enum
clase BoxShape .Información Adicional
BoxShape se usa como parte de la propiedad de forma de la clase BoxDecoration .Ejemplo
HTML / CSS<div class="greybox">
<div class="redcircle">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redcircle {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
text-align: center;
width: 160px;
height: 160px;
border-radius: 50%;
}
Aleteovar container = Container(
child: Center(
child: Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
textAlign: TextAlign.center,
),
decoration: BoxDecoration(
color: Colors.red[400],
shape: BoxShape.circle,
),
padding: EdgeInsets.all(16),
width: 160,
height: 160,
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
Texto
Pregunta
¿Cómo ajustar la distancia entre el texto?Responder
Usando la clase TextStyle y sus propiedades letterSpacing
y wordSpacing
.Ejemplo
HTML / CSS<div class="greybox">
<div class="redbox">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
letter-spacing: 4px;
}
Aleteovar container = Container(
child: Center(
child: Container(
child: Text(
"Lorem ipsum",
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontWeight: FontWeight.w900,
letterSpacing: 4,
),
),
decoration: BoxDecoration(
color: Colors.red[400],
),
padding: EdgeInsets.all(16),
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
Pregunta
¿Cómo formatear parte del texto?Responder
Usando el widget RichText y la clase TextSpan .Ejemplo
HTML / CSS<div class="greybox">
<div class="redbox">
Lorem <em>ipsum</em>
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
}
.redbox em {
font: 300 48px Roboto;
font-style: italic;
}
Aleteovar container = Container(
child: Center(
child: Container(
child: RichText(
text: TextSpan(
style: bold24Roboto,
children: <TextSpan>[
TextSpan(text: "Lorem "),
TextSpan(
text: "ipsum",
style: TextStyle(
fontWeight: FontWeight.w300,
fontStyle: FontStyle.italic,
fontSize: 48,
),
),
],
),
),
decoration: BoxDecoration(
color: Colors.red[400],
),
padding: EdgeInsets.all(16),
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
Pregunta
¿Cómo limitar la visualización de texto largo?Responder
Con la ayuda de los widgets maxLines
y el overflow
widget de texto .Ejemplo
HTML / CSS<div class="greybox">
<div class="redbox">
Lorem ipsum dolor sit amet, consec etur
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
Aleteovar container = Container(
child: Center(
child: Container(
child: Text(
"Lorem ipsum dolor sit amet, consec etur",
style: bold24Roboto,
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
decoration: BoxDecoration(
color: Colors.red[400],
),
padding: EdgeInsets.all(16),
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
¡A fines de diciembre de 2019, con el lanzamiento de Flutter 1.12, el soporte web entró en beta! Y esta es una gran noticia, lo que significa que en un futuro próximo veremos cada vez más sitios web escritos en Flutter. Por lo tanto, si todavía está pensando, le recomiendo probar este marco y espero que mi artículo sea útil. Y eso es todo por hoy. ¡Que Google no rompa tu Chrome!