Если я правильно понял, вы ищете одинаковые виджеты с разными метками (иногда «вкл / выкл», а иногда «блокировать / разблокировать»).
У вас могут быть две разные переменные для определения этих этикетки. Я сделал это так:
import 'package:flutter/material.dart';
void main() {
runApp(Home());
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
List<bool> cardsValue = [false, false, false, false];
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CustomCard(
activeLabel: 'Lock',
inActiveLabel: 'Unlock',
iconData: cardsValue[0] ? Icons.lock : Icons.lock_open,
text: 'Front\nDoor Lock',
isActive: cardsValue[0],
onTap: () {
setState(() {
cardsValue[0] = !cardsValue[0];
});
},
),
SizedBox(width: 30.0),
CustomCard(
activeLabel: 'On',
inActiveLabel: 'Off',
iconData: Icons.lightbulb_outline,
text: 'Lâmpada 2 Schuma',
isActive: cardsValue[1],
onTap: () {
setState(() {
cardsValue[1] = !cardsValue[1];
});
},
),
],
),
),
);
}
}
class CustomCard extends StatelessWidget {
final bool isActive;
final String text;
final String activeLabel;
final String inActiveLabel;
final IconData iconData;
final VoidCallback onTap;
const CustomCard({
this.isActive,
this.text,
this.iconData,
this.onTap,
this.activeLabel,
this.inActiveLabel,
});
@override
Widget build(BuildContext context) {
return InkWell(
onTap: onTap,
child: Container(
height: 100,
width: 120,
child: Card(
color: isActive ? Colors.white : Colors.grey[800],
semanticContainer: true,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
margin: new EdgeInsets.all(0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Row(
children: <Widget>[
Padding(
padding:
EdgeInsets.only(left: 4, top: 7, right: 30, bottom: 10),
child: Icon(
iconData,
color: isActive ? Colors.black : Colors.white,
size: 35,
),
),
Padding(
padding:
EdgeInsets.only(top: 0, right: 0, bottom: 20, left: 0),
child: new Text(
isActive ? activeLabel : inActiveLabel,
style: TextStyle(
color: isActive ? Colors.black : Colors.white),
),
),
],
),
Align(
alignment: Alignment.bottomLeft,
child: Padding(
padding: EdgeInsets.only(top: 8, left: 5),
child: Text(
text,
style: TextStyle(
color: isActive ? Colors.black : Colors.white,
fontSize: 13),
),
),
),
],
),
),
),
);
}
}
Изменения на основе комментария:
Если вам нужно только изменить отступ вокруг метки, вы также можете определить параметр заполнения для виджета следующим образом :
import 'package:flutter/material.dart';
void main() {
runApp(Home());
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
List<bool> cardsValue = [false, false, false, false];
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CustomCard(
padding: EdgeInsets.only(top: 0, right: 0, bottom: 10, left: 0),
activeLabel: 'Lock',
inActiveLabel: 'Unlock',
iconData: cardsValue[0] ? Icons.lock : Icons.lock_open,
text: 'Front\nDoor Lock',
isActive: cardsValue[0],
onTap: () {
setState(() {
cardsValue[0] = !cardsValue[0];
});
},
),
SizedBox(width: 30.0),
CustomCard(
padding: EdgeInsets.only(top: 0, right: 0, bottom: 20, left: 0),
activeLabel: 'On',
inActiveLabel: 'Off',
iconData: Icons.lightbulb_outline,
text: 'Lâmpada 2 Schuma',
isActive: cardsValue[1],
onTap: () {
setState(() {
cardsValue[1] = !cardsValue[1];
});
},
),
],
),
),
);
}
}
class CustomCard extends StatelessWidget {
final bool isActive;
final String text;
final String activeLabel;
final String inActiveLabel;
final IconData iconData;
final EdgeInsetsGeometry padding;
final VoidCallback onTap;
const CustomCard({
this.isActive,
this.text,
this.padding,
this.iconData,
this.onTap,
this.activeLabel,
this.inActiveLabel,
});
@override
Widget build(BuildContext context) {
return InkWell(
onTap: onTap,
child: Container(
height: 100,
width: 120,
child: Card(
color: isActive ? Colors.white : Colors.grey[800],
semanticContainer: true,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
margin: new EdgeInsets.all(0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Row(
children: <Widget>[
Padding(
padding:
EdgeInsets.only(left: 4, top: 7, right: 30, bottom: 10),
child: Icon(
iconData,
color: isActive ? Colors.black : Colors.white,
size: 35,
),
),
Padding(
padding: padding,
child: new Text(
isActive ? activeLabel : inActiveLabel,
style: TextStyle(
color: isActive ? Colors.black : Colors.white),
),
),
],
),
Align(
alignment: Alignment.bottomLeft,
child: Padding(
padding: EdgeInsets.only(top: 8, left: 5),
child: Text(
text,
style: TextStyle(
color: isActive ? Colors.black : Colors.white,
fontSize: 13),
),
),
),
],
),
),
),
);
}
}
Но вы также можете определить дочерний элемент и значок как widget
, поэтому для каждого из этих виджетов вы можете делать любую конфигурацию, которую хотите.
import 'package:flutter/material.dart';
void main() {
runApp(Home());
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
List<bool> cardsValue = [false, false, false, false];
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CustomCard(
icon: Padding(
padding: EdgeInsets.only(
left: 4,
top: 7,
right: 30,
bottom: 10,
),
child: Icon(
cardsValue[0] ? Icons.lock : Icons.lock_open,
color: cardsValue[0] ? Colors.black : Colors.white,
size: 35,
),
),
child: Padding(
padding: EdgeInsets.only(
left: 0,
top: 7,
right: 0,
bottom: 10,
),
child: new Text(
cardsValue[0] ? 'Lock' : 'Unlock',
style: TextStyle(
color: cardsValue[0] ? Colors.black : Colors.white,
),
),
),
label: 'Front\nDoor Lock',
isActive: cardsValue[0],
onTap: () {
setState(() {
cardsValue[0] = !cardsValue[0];
});
},
),
SizedBox(width: 30.0),
CustomCard(
icon: Padding(
padding: EdgeInsets.only(
left: 4,
top: 7,
right: 30,
bottom: 10,
),
child: Icon(
Icons.lightbulb_outline,
color: cardsValue[1] ? Colors.black : Colors.white,
size: 35,
),
),
child: Padding(
padding: EdgeInsets.only(
left: 4,
top: 7,
right: 20,
bottom: 10,
),
child: new Text(
cardsValue[1] ? 'On' : 'Off',
style: TextStyle(
color: cardsValue[1] ? Colors.black : Colors.white,
),
),
),
label: 'Lâmpada 2 Schuma',
isActive: cardsValue[1],
onTap: () {
setState(() {
cardsValue[1] = !cardsValue[1];
});
},
),
],
),
),
);
}
}
class CustomCard extends StatelessWidget {
final bool isActive;
final Widget child;
final String label;
final Widget icon;
final VoidCallback onTap;
const CustomCard({
this.isActive,
this.child,
this.label,
this.icon,
this.onTap,
});
@override
Widget build(BuildContext context) {
return InkWell(
onTap: onTap,
child: Container(
height: 100,
width: 120,
child: Card(
color: isActive ? Colors.white : Colors.grey[800],
semanticContainer: true,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
margin: new EdgeInsets.all(0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Row(
children: <Widget>[icon, child],
),
Align(
alignment: Alignment.bottomLeft,
child: Padding(
padding: EdgeInsets.only(top: 8, left: 5),
child: Text(
label,
style: TextStyle(
color: isActive ? Colors.black : Colors.white,
fontSize: 13),
),
),
),
],
),
),
),
);
}
}