0

There are two ways in my mind to return widget which is repeating again and again. lets see with the example for better understanding. if there is a container which is repeating multiple times with only text changing to if we apply OOP concepts we can refactor the code by extracting container widget and call it wherever we need but there are two ways (in my knowledge) to do this task both works fine but what would be the best practice?

Widget returnContainer(String text){
    return Container(....);
}

or creating stateless widget and return container

class ReturnContainer extends StatelessWidget {
  final String text;
  ReturnContainer(this.text);
  @override
  Widget build(BuildContext context) {
    return Container(.....);
  }
}
Arslan Kaleem
  • 1,410
  • 11
  • 25

1 Answers1

1

They're both valid solutions, but apply to different situations. You would choose the return function if your widget needs to be called only in the dart file where you are implementing it. You would otherwise choose a stateless widget if your code needs to be used many times in manu different files.

Ale TheFe
  • 1,540
  • 15
  • 43