-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFuture Builder In Flutter.dart
60 lines (45 loc) · 1.16 KB
/
Future Builder In Flutter.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: Animal().getName(),
builder:(context, animalName){
if(animalName.connectionState == ConnectionState.done){
return Text('${animalName.data}', style: Theme.of(context).textTheme.headline4);
}else if(animalName.hasError){
return Text("Ran into some error..") ;
}
else{
return CircularProgressIndicator();
}
}
);
}
}
class Animal{
String name ="Elephant";
int legs =4;
Future<String> getName()async {
await Future.delayed(Duration(seconds: 3));
return name;
}
}