0

I'm in the process of clean up my code in the mobile app.

I don't quite understand what is the difference between this code:

import 'package:flutter/material.dart';

class TestWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Text("test")
      ],
    );
  }
}

and this:

TestWidget(){
  return Row(
    children: [
      Text("test")
    ],
  );
  }

I want to use later this components for example like this:

 return MaterialApp(
      home: TestWidget(),
    );

I understand that it matters if the widget is stateful, but is it enough to create a function for such a component that will return the Widget, or should I wrap it in a stateless widget in some specific cases?

  • To treat it better as a widget you should use: Widget TestWidget(BuildContext context) { return here }. Or extract it to a new file which you define StateLess or Statefull widget. Hope it helps – Fotis Psarris Feb 09 '23 at 10:57

1 Answers1

0

You can write it how ever you want but splitting the code helps you to understand better and clutter will be less