Created with <3 with dartpad.dev.
Last active
October 12, 2023 15:36
-
-
Save leonino/73f6924b343c752541ab20fbcde58afb to your computer and use it in GitHub Desktop.
Future Builder Example
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
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file | |
// for details. All rights reserved. Use of this source code is governed by a | |
// BSD-style license that can be found in the LICENSE file. | |
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const FutureBuilderExample(), | |
); | |
} | |
} | |
class FutureBuilderExample extends StatefulWidget { | |
const FutureBuilderExample({super.key}); | |
@override | |
State<FutureBuilderExample> createState() => _FutureBuilderExampleState(); | |
} | |
class _FutureBuilderExampleState extends State<FutureBuilderExample> { | |
Future<bool>? _future; | |
@override | |
void initState() { | |
super.initState(); | |
_future = myFuture(); | |
} | |
Future<bool> myFuture() async { | |
await Future.delayed(const Duration( | |
seconds: 2)); //Aguarda 2 segundos para prosseguir com o resto do código | |
var currentSeccond = DateTime.now().second; //Salva o segundo da hora atual | |
debugPrint( | |
'currentSeccond = $currentSeccond'); //Printa o número no DEBUG CONSOLE | |
return currentSeccond | |
.isEven; //Retorna TRUE quando o número for par, FALSE quando ímpar | |
} | |
@override | |
Widget build(BuildContext context) { | |
debugPrint('setState executado'); | |
return Material( | |
child: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
FutureBuilder<bool>( | |
future: _future, | |
builder: (context, snapshot) { | |
if (snapshot.connectionState == ConnectionState.waiting) { | |
return Container( | |
width: 100, | |
height: 100, | |
color: Colors.transparent, | |
child: const Center(child: CircularProgressIndicator()), | |
); | |
} else { | |
final isEven = snapshot.data ?? false; | |
return Container( | |
width: 100, | |
height: 100, | |
color: isEven ? Colors.green : Colors.red, | |
child: Center(child: Text(isEven ? "Impar" : "Par")), | |
); | |
} | |
}, | |
), | |
const SizedBox(height: 10), | |
ElevatedButton( | |
child: const Text('setState'), | |
onPressed: () { | |
setState( | |
() {}); //Apenas um setState para mostrar que o método não será chamado novamente. | |
}, | |
), | |
const SizedBox(height: 10), | |
ElevatedButton( | |
child: const Text('Reload function'), | |
onPressed: () { | |
setState(() { | |
_future = myFuture(); //Recarrega a função | |
}); | |
}, | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment