Flutter 封装组件

tech2025-04-23  6

封装组件

class MyBody extends StatelessWidget { const MyBody({Key key}) : super(key: key); @override Widget build(BuildContext context) { return Container( child: Wrap( children: [ RaisedButton( onPressed: () {}, child: Text('第一季'), textColor: Theme.of(context).accentColor, elevation: 15.0, ), ], ), ); } }

1.先写好组件 然后准备抽出来封装RaisedButton

class MyButton extends StatelessWidget { final String text; const MyButton(this.text, {Key key}) : super(key: key); @override Widget build(BuildContext context) { return RaisedButton( onPressed: () {}, child: Text(this.text), textColor: Theme.of(context).accentColor, elevation: 15.0, ); } }

2.这样就可以封装一个组件了

最新回复(0)