I am new to Flutter and still don't know how to do things the CORRECT way. I hope the title is clear enough because I don't know the proper keyword to address this. First snippet extends StatelessWidget:
class FloatingActionButtonBuilder extends StatelessWidget {
final Function function;
final String text;
final String toolTip;
final IconData icon;
const FloatingActionButtonBuilder({
Key key,
@required this.function,
@required this.text,
@required this.toolTip,
this.icon,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return FloatingActionButton.extended(
onPressed: function,
foregroundColor: Colors.white,
tooltip: '$toolTip',
icon: Icon(
icon,
),
label: Text(
'$text',
style: TextStyle(
fontSize: 16.0,
),
),
);
}
}
Second snippet a regular class:
class FloatingActionButtonBuilder2 {
final BuildContext context;
final Function function;
final String text;
const FloatingActionButtonBuilder2({
@required this.context,
@required this.function,
@required this.text,
});
Widget buildFAB(String toolTip, IconData icon) {
return FloatingActionButton.extended(
onPressed: function,
foregroundColor: Colors.white,
tooltip: '$toolTip',
icon: Icon(
icon,
),
label: Text(
'$text',
style: TextStyle(
fontSize: 16.0,
),
),
);
}
}
The one I've been using is the regular one. Initially, I made the extends StatelessWidget but then I decided not to because I assumed there's no difference anyway and didn't think much about it. Now, out of nowhere my brain wants to know what the experts think about this particular case, in depth suggestions are deeply appreciated. And I noticed that with override build function I don't need BuildContext as a dependency.
EDIT: Snippet of page using extends StatelessWidget (floatingActionButton property of Scaffold):
class Test extends StatefulWidget {
@override
_TestState createState() => _TestState();
}
class _TestState extends State<Test> {
final String text = 'PUSH';
final IconData icon = Icons.add;
void push() {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => Test3(),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButtonBuilder(
function: push,
text: text,
toolTip: text,
icon: icon,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
'PUSH PAGE',
style: TextStyle(
fontSize: 32.0,
),
),
Text(
'EXTENDS CLASS',
style: TextStyle(
fontSize: 32.0,
),
),
],
),
),
);
}
}
Snippet of page using regular class (floatingActionButton property of Scaffold):
class Test3 extends StatefulWidget {
@override
_Test3State createState() => _Test3State();
}
class _Test3State extends State<Test3> {
final String text = 'POP';
final IconData icon = Icons.remove;
void pop() {
Navigator.of(context).pop();
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButtonBuilder2(
context: context,
function: pop,
text: text,
).buildFAB(text, icon),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
'POP PAGE',
style: TextStyle(
fontSize: 32.0,
),
),
Text(
'REGULAR CLASS',
style: TextStyle(
fontSize: 32.0,
),
),
],
),
),
);
}
}
Container(child: FloatingActionButtonBuilder2(...))
) – Abion47