1

I have some confusion between Builder Function and Widget Class , I would to know why , when I should use one of them ,

like in this code , this code not the problem but I will write to explain more .

import 'package:flutter/material.dart';

class AOneScreen extends StatelessWidget {

  //Builder Function
  Widget builderFunction(){
    return Container(
      // any code .....
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Column(
          children: <Widget>[
            builderFunction(), ///use builder function
            WidgetBuilder()   /// use Widget Class
          ],
        ),
      ),
    );
  }
}

/////Widget Class
class WidgetBuilder extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      /// any code ....
    );
  }
}
Mahmoud Niypoo
  • 1,598
  • 5
  • 24
  • 41
  • Is that a duplicate of: https://stackoverflow.com/questions/53234825/what-is-the-difference-between-functions-and-classes-to-create-widgets/53234826#53234826 ? This question being about static function vs yours about methods, but that should be the same – Rémi Rousselet Nov 10 '19 at 14:37
  • No it's not a duplicate of that question. The one that you linked is about using global functions. – Filip P. Nov 10 '19 at 15:05
  • The answer is strictly the same though. – Rémi Rousselet Nov 10 '19 at 15:05
  • The answer in the question is "Never ever use functions over classes to make reusable widget-tree" I think that phrase "reusable" is key here. I don't see anything wrong in using widget helper methods for composing layout when the method is not intended for reusability. – Filip P. Nov 10 '19 at 15:10
  • @FilipP. so I can separate my widget class into small methods/functions builder if I don't user them ever out side that class !! but If maybe use a method/function builder outside or with another widget make it in Widget class !! – Mahmoud Niypoo Nov 10 '19 at 15:14

0 Answers0