Flutter:自定义小部件不显示元素
Flutter: Custom Widget not showing elements
我创建了自定义小部件 (cardChild),但它没有显示任何元素
class ReuseableCard extends StatelessWidget {
ReuseableCard({required this.colour, required this.cardChild});
Color colour;
final Widget cardChild;
应用程序中未显示图标和文本
Expanded(
child: ReuseableCard(
colour: activeCardColour,
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
FontAwesomeIcons.mars,
size: 80.0,
),
Text(
'Male',
style: TextStyle(
fontSize: 18.0, color: Color(0xFF8D8E98)),
)],
),
),),
flutter没有报错
并请说明为什么widget需要“required”才能构建。
class ReuseableCard extends StatelessWidget {
ReuseableCard({required this.colour, required this.cardChild});
Color colour;
final Widget cardChild;
@override
Widget build(BuildContext context) {
return Container(
child: cardChild, //Here
margin: const EdgeInsets.all(15.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: colour,
),
);}
}
我们需要在Container中添加cardChild。
希望这能解决您的问题..
class SecondRoute extends StatelessWidget {
const SecondRoute({Key? key, required this.argumentWidget}) : super(key: key);
final Widget argumentWidget;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
argumentWidget,/// this widget is come from FirstRoute screen as you can see we are on second route screen
customMyWidget(stringData: 'myNameIsFlutterDev'),//this is from your class
],
),
);
}
// the below widget is your customWidget inside the class
Widget customMyWidget({required String stringData}){
return Text(stringData);
}
}
我创建了自定义小部件 (cardChild),但它没有显示任何元素
class ReuseableCard extends StatelessWidget {
ReuseableCard({required this.colour, required this.cardChild});
Color colour;
final Widget cardChild;
应用程序中未显示图标和文本
Expanded(
child: ReuseableCard(
colour: activeCardColour,
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
FontAwesomeIcons.mars,
size: 80.0,
),
Text(
'Male',
style: TextStyle(
fontSize: 18.0, color: Color(0xFF8D8E98)),
)],
),
),),
flutter没有报错 并请说明为什么widget需要“required”才能构建。
class ReuseableCard extends StatelessWidget {
ReuseableCard({required this.colour, required this.cardChild});
Color colour;
final Widget cardChild;
@override
Widget build(BuildContext context) {
return Container(
child: cardChild, //Here
margin: const EdgeInsets.all(15.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: colour,
),
);}
}
我们需要在Container中添加cardChild。
希望这能解决您的问题..
class SecondRoute extends StatelessWidget {
const SecondRoute({Key? key, required this.argumentWidget}) : super(key: key);
final Widget argumentWidget;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
argumentWidget,/// this widget is come from FirstRoute screen as you can see we are on second route screen
customMyWidget(stringData: 'myNameIsFlutterDev'),//this is from your class
],
),
);
}
// the below widget is your customWidget inside the class
Widget customMyWidget({required String stringData}){
return Text(stringData);
}
}