Skip to content

Instantly share code, notes, and snippets.

@abner
Created May 7, 2020 14:05
Show Gist options
  • Save abner/dc4fa9265fb43175dfdeef6bc407d7c5 to your computer and use it in GitHub Desktop.
Save abner/dc4fa9265fb43175dfdeef6bc407d7c5 to your computer and use it in GitHub Desktop.
Exemplo indicador de progresso
// 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';
import 'dart:async';
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: MyHomePage(title: 'Exemplo de indicador de progresso'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Future<List<int>> result;
void _updateList() {
setState(() {
result =
Future.delayed(Duration(milliseconds: 800), () => [1, 2, 3, 4, 5]);
});
}
@override
void initState() {
super.initState();
result = Future.delayed(Duration(milliseconds: 800), () => [1, 2, 3, 4, 5]);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: FutureLoadingProgress(
future: result,
onFinishedBuilder: (ctx, data) => ListView.builder(
itemCount: data.length,
itemBuilder: (BuildContext ctxt, int index) {
return new Padding(
padding: EdgeInsets.all(20),
child: Text('Elemento ${data[index]}'));
},
),
onErrorBuilder: (ctx, error) => Text('Erro na obtenção dos dados: $error')
),
floatingActionButton: FloatingActionButton(
onPressed: _updateList,
tooltip: 'Refresh',
child: Icon(Icons.refresh),
),
);
}
}
class FutureLoadingProgress extends StatelessWidget {
final Future<dynamic> future;
final Widget Function(BuildContext ctx, dynamic data) onFinishedBuilder;
final Widget Function(BuildContext ctx, dynamic error) onErrorBuilder;
FutureLoadingProgress({
@required this.future,
@required this.onFinishedBuilder,
@required this.onErrorBuilder,
});
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: future,
builder: (ctx, asyncSnapshot) {
if (asyncSnapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
} else if (asyncSnapshot.error != null) {
return onErrorBuilder(ctx, asyncSnapshot.error);
} else {
return onFinishedBuilder(ctx, asyncSnapshot.data);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment