Flutter Build widgets
Building Widgets
Flutter does the job of rendering the widgets on the screen for us (more on change detection & rendering later), but it needs configuration information for the widget: what color is it going to be, what is its border, does it contain other widgets....
Build Method
When it needs to know how to render a widget, Flutter calls the ‘build’ method in your widget. That method returns a Widget object that gives Flutter configuration information about the widget (and any childwidgets that it may be composed of).
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Center(child:Text('Hello World'))
);
}
}
The ‘build’ method takes one argument, the BuildContext (more on that later) and returns a Widget object. That returned Widget object contains configuration data that tells Flutter that it needs to render a Material App widget with a title and some centered text.
Build Context
Earlier we mentioned that a widget can contain other widgets, in a tree structure, a hierarchy. This is often called a Widget Tree. The first argument to the build’ method of your Widget is the BuildContext. This gives your ‘build’ method information about the location of your Widget in the Widget Tree.It may not seem useful at the moment but will come in very handy later on!