Sunday 7 June 2020

Stafull and Stateless widget in Flutter

In flutter we have 2 type of widget, Statefull and stateless. Difference are the stateless widget will never changes after it rendered. Meantime statefull widget can be change in the future after the widget rendered.


There are difference on how to use it. Because flutter using inheritance of the widget, we will always override some of the function / properties of the widget. There will be a lot of inheritance method in flutter.

Lets go for the stateless widget first. On the stateless widget, the one we override are the build function.
Below is the example code.

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'),
        ),
      ),
    );
  }
}


And for statefull widget, we override the createState method.

import 'package:flutter/material.dart';

void
main() => runApp(MyApp());

class
MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp (
title: 'Measures Converter' ,
home: Scaffold(
appBar: AppBar(
title: Text('Measures Converter'),
),
body: Center(
child: Text('Measures Converter'),
),
),
);
}
}

class MyApp extends StatefulWidget {
@override
MyAppState createState() => MyAppState();


}


0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More