estevaorada

Flutter: Stateful

Mar 31st, 2021
382
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3.  
  4. main() {
  5.   runApp(WidgetPrincipal(
  6.     titulo: "Faustão",
  7.   ));
  8. }
  9.  
  10. class WidgetPrincipal extends StatelessWidget {
  11.   final String titulo;
  12.  
  13.   const WidgetPrincipal({Key key, this.titulo}) : super(key: key);
  14.   @override
  15.   Widget build(BuildContext context) {
  16.     return MaterialApp(
  17.       theme: ThemeData.light(),
  18.       home: HomePage(),
  19.     );
  20.   }
  21. }
  22.  
  23. // Classe para "Invocar" o widget stateful:
  24. // Estado "desacoplado":
  25. class HomePage extends StatefulWidget {
  26.   @override
  27.   State<HomePage> createState() {
  28.     return HomePageState();
  29.   }
  30. }
  31.  
  32. // Widget Stateful com o devido conteúdo:
  33. class HomePageState extends State<HomePage> {
  34. // Variaveis:
  35.   var contador = 0;
  36.  
  37.   @override
  38.   Widget build(BuildContext context) {
  39.     return MaterialApp(
  40.         theme: ThemeData.light(),
  41.         home: Container(
  42.             child: Center(
  43.           // GestureDetector: Widget para detectar 'movimentos' na tela:
  44.           child: GestureDetector(
  45.             child: Text("Cont: $contador",
  46.                 textDirection: TextDirection.ltr,
  47.                 style: TextStyle(color: Colors.white, fontSize: 50.0)),
  48.             // Evendo a ser detectado por GestureDetector:
  49.             onTap: () {
  50.               // Exibir no console:
  51.               // print('clicado');
  52.               //print(contador);
  53.               // definir o setstate:
  54.               // Boa prática: Colocar os itens que serão alterados dentro:
  55.               setState(() {
  56.                 contador++;
  57.               });
  58.             },
  59.           ),
  60.         )));
  61.   }
  62. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×