class MyWidget extends StatelessWidget {
final String name = 'Default';
final bool hasThing = true;
MyWidget({this.name});
MyWidget.withoutThing({this.name}) : hasThing = false;
@override
Widget build(BuildContext context) {
//define widgets they have in common here (as many as possible)
if (hasThing) {
return Widget(child: Thing(this.name));
} else {
return Widget(child: WithoutThing(this.name));
}
}
}
使用它:
Center(child: MyWidget.withoutThing(name: 'Foo'),)//don't show thing
Column(
children: [
Text('Title'),
name != ''
? Text(name) //show name
: null // just pass a null we will filter it in next line!
].where((e) => e != null).toList()// Filter list and make it List again!
)