Today I faced the new code composing style in Flutter. Usually I'm using the next way to declare widget tree :
This is a class that we have after create new flutter project(to save your time I publish only build method) :
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
The difference, that I seen today in some flutter project in github, to declare widgets as getters :
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
textPushed,
counterText,
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
Widget get textPushed {
return Text(
'You have pushed the button this many times:',
);
}
Widget get counterText {
return Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
);
I have never seen this approach of using getters for building widgets and I want to know is it a good practice or not and why?