We will now create a new app that we'll use throughout this chapter to build a fully functioning measure converter:
- From your favorite editor, create a new app. Name the new app Unit Converter.
- In the main.dart file, remove the example code and write the code given as follows:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Measures Converter',
home: Scaffold(
appBar: AppBar(
title: Text('Measures Converter'),
),
body: Center(
child: Text('Measures Converter'),
),),
);}
}
As you may have noticed, the preceding code makes use of a Stateless widget:
class MyApp extends StatelessWidget {
A Stateless widget is a class that extends a StatelessWidget. Extending a StatelessWidget class requires overriding a build() method.
In the build() method, you describe the widget returned by the method:
@override
Widget build(BuildContext context) {
The build() method that takes a context and returns a widget:
return MaterialApp(…)
So to summarize, in order to have a stateless widget you need to do the following:
- Create a class that extends StatelessWidget.
- Override the build() method.
- Return a widget.
Once built, a Stateless widget never changes.