You may know there are two classes, actually three, those are
- Stateless Widget
- Stateful Widget
- Inherited Widget
Stateless Widget is immutable and when we want to change something that displaying we need to create new one.
Stateful Widget is mutable and can interact with users. When we want to redraw/ change something that displaying we need to use this widget. To use this widget we need two classes. And to redraw the display we have a method call setState().
Inherited Widget is mix of both of above.
I think now you understand what is the widget that we need to use for this situation. It is stateful widget. You guess it right?
So here is the complete code, and I'll explain it one by one.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
title: 'Converter App', | |
theme: new ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: new Scaffold( | |
appBar: new AppBar( | |
title: new Text('Converter App'), | |
), | |
body: new Center( | |
child: new Converter(), | |
), | |
) | |
); | |
} | |
} | |
class Converter extends StatefulWidget { | |
@override | |
_ConverterWidget createState() => new _ConverterWidget(); | |
} | |
class _ConverterWidget extends State<Converter> { | |
String value = "click Me"; | |
@override | |
Widget build(BuildContext context) { | |
return new Column( | |
children: <Widget>[ | |
new Text(value), | |
new RaisedButton( | |
child: new Text('Click me'), | |
onPressed: () { | |
setState(() { | |
value += ' Me'; | |
}); | |
}, | |
), | |
], | |
); | |
} | |
} |
No comments:
Post a Comment