Writing main

Let’s start from the simplest and least specific part of our app: the main function.

Using a Wrapper Class

In the previous chapter we defined a MyApp class and used main simply to run it, in the following way:

 void​ ​main​() =>
  runApp(MyApp());
 
 class​ MyApp ​extends​ StatelessWidget {
  @override
  Widget build (BuildContext context) {
 return​ MaterialApp(
 // Actual app content and data
  );
  }
 }

Skipping the Wrapper Class

The same effect would be achieved by simply writing:

 void​ ​main​() {
  runApp(MaterialApp(
 // other app content and data
  ));
 }

This looks a lot shorter, but in a larger app it isn’t really much by comparison.

If your app is entirely contained in one view it doesn’t compromise app functionality, but if you need more views and need to have shared data you should consider writing a wrapper class for your app to keep things organized and clear.

And, in any case, you should only use the second form if you really need to keep your code compact or to save time to quickly write a small app.

So we will write the following:

 void​ ​main​() => runApp(MyApp());
 
 class​ MyApp ​extends​ StatelessWidget {
  @override
  Widget build(BuildContext context) {
 return​ MaterialApp(
  title: ​"Flutter Calculator"​,
  theme: ThemeData(
  primarySwatch: Colors.blue,
  backgroundColor: Colors.black26
  ),
  home: CalculatorHomePage(title: ​"Flutter Calculator"​, ),
  );
  }
 }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset