1

so i have this code:

import 'package:flutter/material.dart';
void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Home()
    );
  }
}

Widget Home() {
  return Container(child: Text('aa'),);
}


class Home2 extends StatelessWidget {
  const Home2({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(child: Text('aa'));
  }
}

what is the difference between Home() and Home2()? will they work the same or they have something special?

dx dx
  • 43
  • 1
  • 4
  • 1
    `in details`(https://stackoverflow.com/questions/53234825/what-is-the-difference-between-functions-and-classes-to-create-reusable-widgets) – Zaid Alazwi Apr 27 '22 at 03:56
  • This is the same as what i was asking for. thank you so much! – dx dx Apr 27 '22 at 04:03

1 Answers1

1

Usually 'Home' function is called 'Helper method'.

Here is a official flutter video that exlain difference between helper method and widget.
https://www.youtube.com/watch?v=IOyq-eTRhvo&ab_channel=Flutter

  • The widgets in helper method is not detected in widget tree structure
  • performance: Helper method is all rebuilded when it needs refresh.
  • When the code is changed in Helper method, hot reload is not worked
    ...and so on..
KuKu
  • 6,654
  • 1
  • 13
  • 25
  • ah this is actually the same explanation as the one above, sorry im dumb, yes it is a helper method, thank you for you help! – dx dx Apr 27 '22 at 04:49