import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Petri Dish App'),
),
body: ClassWithStateFull(),
),
);
}
}
class ClassWithStateFull extends StatefulWidget {
@override
_ClassWithStateFullState createState() => _ClassWithStateFullState();
}
class _ClassWithStateFullState extends State<ClassWithStateFull> {
int _cont = 0;
//function with documentation recomendated way
addCountInsideState() {
setState(() {
_cont++;
});
print("I add 1 on count, now it is $_cont");
}
//function add count and render
addCountAndThenCallState() {
_cont++;
setState(() {});
print("I add 1 on count, now it is $_cont");
}
//function add count and render in another function
addCountAndCallStateJustToRender() {
_cont++;
reRenderThePage();
print("I add 1 on count, now it is $_cont");
}
//function to render the page
reRenderThePage() {
setState(() {});
print("runing setState to rerender page");
}
@override
Widget build(BuildContext context) {
return Column(
children: [
Text('new count value is: $_cont'),
RaisedButton(
onPressed: addCountInsideState,
child: Text("addCountInsideState"),
),
RaisedButton(
onPressed: addCountAndThenCallState,
child: Text("addCountAndThenCallState"),
),
RaisedButton(
onPressed: addCountAndCallStateJustToRender,
child: Text("addCountAndCallStateJustToRender"),
)
],
);
}
}