· 3 years ago · Feb 04, 2022, 05:30 PM
1
2import 'package:flutter/material.dart';
3
4void main() => runApp(MyApp());
5
6class MyApp extends StatelessWidget {
7 @override
8 Widget build(BuildContext context) {
9 return MaterialApp(
10 title: 'Flutter Demo',
11 debugShowCheckedModeBanner: false,
12 theme: ThemeData(
13 primarySwatch: Colors.blue,
14 ),
15 home: const MyHomePage(title: 'Flutter Demo Home Page'),
16 );
17 }
18}
19
20class MyHomePage extends StatefulWidget {
21 final String title;
22
23 const MyHomePage({
24 Key? key,
25 required this.title,
26 }) : super(key: key);
27
28 @override
29 _MyHomePageState createState() => _MyHomePageState();
30}
31
32class _MyHomePageState extends State<MyHomePage> {
33 bool isLoading = false;
34 late TextEditingController nameController;
35
36 void getUserData() {
37 setState(() {
38 isLoading = true;
39 });
40 Future.delayed(const Duration(seconds: 6), () {
41 // Get data from API
42 const name = 'Flutter';
43 nameController = TextEditingController(text: name);
44 setState(() {
45 isLoading = false;
46 });
47 });
48 }
49
50 @override
51 void initState() {
52 super.initState();
53 getUserData();
54 }
55
56 @override
57 void dispose() {
58 super.dispose();
59 nameController.dispose();
60 }
61
62 @override
63 Widget build(BuildContext context) {
64 return Scaffold(
65 appBar: AppBar(
66 title: Text(widget.title),
67 ),
68 body: Container(
69 alignment: Alignment.center,
70 padding: const EdgeInsets.symmetric(horizontal: 50.0),
71 child: Column(
72 mainAxisAlignment: MainAxisAlignment.center,
73 crossAxisAlignment: CrossAxisAlignment.center,
74 children: [
75 const Text(
76 'User Detail',
77 ),
78 const SizedBox(height: 30.0),
79 if (isLoading) const CircularProgressIndicator(),
80 if (!isLoading) TextField(controller: nameController),
81 ],
82 ),
83 ),
84 );
85 }
86}
87