Home > Enterprise >  what is the difference between creating widget using widget class, and creating class widget that ex
what is the difference between creating widget using widget class, and creating class widget that ex

Time:04-27

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?

CodePudding user response:

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..
  • Related