Flutter Angela
Flutter Angela
import 'package:flutter/material.dart';
void main() {
runApp(
Center(
child: Text(
'Hello, world!',
textDirection: TextDirection.ltr,
),
),
);
}
When writing an app, you’ll commonly author new widgets that are subclasses of
either StatelessWidget or StatefulWidget, depending on whether your
widget manages any state. A widget’s main job is to implement a build() function,
which describes the widget in terms of other, lower-level widgets. The framework
builds those widgets in turn until the process bottoms out in widgets that represent
the underlying RenderObject, which computes and describes the geometry of the
widget.
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
title: 'GeeksforGeeks',
theme: ThemeData(
primarySwatch: Colors.green
),
home: Scaffold(
appBar: AppBar(
title:Text(
'GeeksforGeeks'
)
),
),
));
}
void main() {
runApp(
MaterialApp(
home: Scaffold(
backgroundColor: Colors.blueGrey,
body: Center(
child: Image(
image: AssetImage('images/alishaw2.jpg'),
),
),
appBar: AppBar(
title: Center(
child: Text('This is my First App'),
),
backgroundColor: Colors.blueGrey[900],
),
),
),
);
}
An application that uses material design. A convenience widget that wraps a number
of widgets that are commonly required for material design applications. It builds
upon a WidgetsApp by adding material-design specific functionality, such as
AnimatedTheme and GridPaper.
The MaterialApp class itself has a long list of named parameters conveying its
influence and importance in making such apps in Flutter. It’s the MaterialApp widget
that defines the theme (the ‘look and feel’) of your app. It’s to the MaterialApp object
where you specify the list of ‘routes’ to any and all the separate screens that will
make up your app. You can also tell the MaterialApp object, which will be the first
screen (the ‘home’ screen) when your app first starts up. It’s to the MaterialApp
object where you give a title to your app. Further, it’s to the Material object where
you specify what locale to use to for your app. In other words, how to display values
that are location-specific etc etc.
As a quick aside, note the MaterialApp widget is a StatefulWidget. That tells you
things can change in such an object. It retains a state, but that state is free to
change.
To ‘move about’ to more than one screen in a Flutter app, the concept of ‘routes’ is
utilized. The MaterialApp requires the named parameter, routes, not be null and so
it’s assigned, by default, what’s simply an empty Map object. The key apparently is a
String object, and its value is of the function-type (see typedef) called,
WidgetBuilder.
Your Initial Route
Note, in the example an ‘initial route’ is specified with the named parameter, initialRoute. This will
indicate what the first screen will be when the app first starts up.
As it happens, this example is a little misleading, you need not explicitly specify the forward slash,
`/` as it’s the default value even if the named parameter, initialRoute, was not provided. And so,
commenting it out will bring about the same results. Anyway, for our endeavours, the real interest
in this example is the routes parameter. It’s a Map object listing all the screens one can navigate
to in this app. Again, the `/` signifies the ‘home screen.’ In this example, the home screen is
named, `/`, and the other screen is named, `/second.`
For example, the assigning of a default value to the routes parameter implies, routes need not be
specified. And so, what happens then if you don’t provide the parameter, routes, a Map of named
WidgetBuilders?? It defaults to an empty Map object, I know, but then what? Now, the app has got
to start somewhere. A ‘first screen’ must be specified. Now in this example, there’s no ‘routes’
parameter. However, there is the named parameter called, home, being used instead. What does
that do? Judging from the name, ‘home’, you’d guess correctly it specifies the first screen to
display.
What is Scaffold ?
In the documentation, it stipulates that at least one of the following named
parameters must be passed to the MaterialApp object and must not be null: home,
routes, onGeneratedRoute, or builder. Further, as we’ve already seen, if only
‘routes’ is provided, it must include an entry for the `/` so to present a screen when
the app is first launched.
In fact, there is an order to things in this regard. If the parameter, home, is not null,
then it provides the ‘home screen’. Simple enough. Otherwise, if the parameter,
routes, was provided and there’s a forward slash entry (`/`), then that’s your ‘home
screen.’ However, if such an entry doesn’t exist, and no parameter, home, the
Flutter framework looks to the fourth of the seven parameters indicated above. The
callback function, onGenerateRoute. If provided, it’s called upon to return the first
screen. Finally, if the parameter, onGenerateRoute, is not provided and the
parameter, builder, is not provided, then the fifth parameter, onUnknownRoute, is
called upon — if provided. Note, the parameter, onUnknownRoute, is called if at any
time there’s an ‘unknown route’ event in your app. Now, after that, if it’s not there
either…well, you’re then typically presented with a blank screen at startup. Ta-da!
What is Scaffold ?
Scaffold is a class in flutter which provides many widgets or we can say APIs like
AppBar, Drawer, SnackBar, BottomNavigationBar, FloatingActionButton etc.
Scaffold will expand or occupy in the whole device screen. It will occupy the
available space. Scaffold will provide a framework to implement the basic material
design layout of the application.
Class Hierarchy:
Object
↳ Diagnosticable
↳ Diagnosticable Tree
↳ Widget
↳ StateFul Widget
↳ Scaffold
const Scaffold({
Key key,
this.appBar,
this.body,
this.floatingActionButton,
this.floatingActionButtonLocation,
this.floatingActionButtonAnimator,
this.persistentFooterButtons,
this.drawer,
this.endDrawer,
this.bottomNavigationBar,
this.bottomSheet,
this.backgroundColor,
this.resizeToAvoidBottomPadding,
this.resizeToAvoidBottomInset,
this.primary = true,
this.drawerDragStartBehavior
= DragStartBehavior.start,
this.extendBody = false,
this.drawerScrimColor,
})
1. appBar: It displays a horizontal bar which mainly placed at the top of the
Scaffold. appBar uses the widget AppBar which has its own properties like
elevation, title, brightness etc.
Widget build(BuildContext context)
{
return Scaffold(
appBar: AppBar(
title: Text('GeeksforGeeks'),
),
2. body: It will display the main or primary content in the Scaffold. It is below the
appBar and under the floatingActionButton. The widgets inside the the body
are at left-corner by default.
Widget build(BuildContext context)
{
return Scaffold(
appBar: AppBar(
title:
Text('GeeksforGeeks'),
),
body: Center(
child: Text("Welcome
to GeeksforGeeks!!!",
style: TextStyle(
color:
Colors.black,
fontSize: 40.0,
),
),
),
Here the elevation property is used to give a shadow effect to the button. Icon
is used to put the icon of the button using some of the preloaded icons in
flutter SDK. The onPressed() is function which will be called when the button is
pressed and the statements inside the function will be executed.
ListTile(
title
: Text('Item 1'),
leading
: Icon(Icons.people), ),
ListTile(
title
: Text('Item 2'),
leading
: Icon(Icons.mail), ),
We use BottomNavigationBar widget to display the bar. For the color of active
icon we use the fixedColor property. To add items in the bar we use
BottomNavigationBarItems widget, inside which we give text and icon. For the
action performed on the tapping on the items, we have onTap(int indexOfItem)
function which works according to the index position of the item.
STATELESS Widgets
In simple words, Stateless widgets cannot change their state during the runtime of
the app, which means the widgets cannot be redrawn while the app is in action.
In Stateless widget, The “build” method can be called only ONCE while the app is in
action, which is responsible for drawing the widgets on to the device screen.