Created
April 9, 2024 11:31
-
-
Save DavBfr/ab8a608d3638b8e0f7c7ca88993d02f4 to your computer and use it in GitHub Desktop.
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 'dart:async'; | |
import 'package:flutter/material.dart'; | |
void main() => runApp(const MyApp()); | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
const MyHomePage({ | |
super.key, | |
required this.title, | |
}); | |
final String title; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(title), | |
), | |
body: Center( | |
child: OutlinedButton( | |
onPressed: () async { | |
final result = await progress( | |
context: context, | |
title: Text('Processing...'), | |
result: _process(), | |
); | |
print('Result is $result'); | |
}, | |
child: const Text('Process'), | |
), | |
), | |
); | |
} | |
Future<int> _process() async { | |
await Future.delayed(const Duration(seconds: 2)); | |
return 42; | |
} | |
} | |
Future<T?> progress<T>({ | |
required BuildContext context, | |
required Future<T> result, | |
required Widget title, | |
}) async { | |
print('Processing...'); | |
final answer = await result; | |
print('done.'); | |
return answer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment