Created
December 5, 2019 15:46
-
-
Save fabiojansenbr/2805427f1ba1175ddb5d717589b00031 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 'package:api_to_sqlite_flutter/src/providers/db_provider.dart'; | |
import 'package:api_to_sqlite_flutter/src/providers/employee_api_provider.dart'; | |
import 'package:flutter/material.dart'; | |
class HomePage extends StatefulWidget { | |
const HomePage({Key key}) : super(key: key); | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
var isLoading = false; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Api to sqlite'), | |
centerTitle: true, | |
actions: <Widget>[ | |
Container( | |
padding: EdgeInsets.only(right: 10.0), | |
child: IconButton( | |
icon: Icon(Icons.settings_input_antenna), | |
onPressed: () async { | |
await _loadFromApi(); | |
}, | |
), | |
), | |
Container( | |
padding: EdgeInsets.only(right: 10.0), | |
child: IconButton( | |
icon: Icon(Icons.delete), | |
onPressed: () async { | |
await _deleteData(); | |
}, | |
), | |
), | |
], | |
), | |
body: isLoading | |
? Center( | |
child: CircularProgressIndicator(), | |
) | |
: _buildEmployeeListView(), | |
); | |
} | |
_loadFromApi() async { | |
setState(() { | |
isLoading = true; | |
}); | |
var apiProvider = EmployeeApiProvider(); | |
await apiProvider.getAllEmployees(); | |
// wait for 2 seconds to simulate loading of data | |
await Future.delayed(const Duration(seconds: 2)); | |
setState(() { | |
isLoading = false; | |
}); | |
} | |
_deleteData() async { | |
setState(() { | |
isLoading = true; | |
}); | |
await DBProvider.db.deleteAllEmployees(); | |
// wait for 1 second to simulate loading of data | |
await Future.delayed(const Duration(seconds: 1)); | |
setState(() { | |
isLoading = false; | |
}); | |
print('All employees deleted'); | |
} | |
_buildEmployeeListView() { | |
return FutureBuilder( | |
future: DBProvider.db.getAllEmployees(), | |
builder: (BuildContext context, AsyncSnapshot snapshot) { | |
if (!snapshot.hasData) { | |
return Center( | |
child: CircularProgressIndicator(), | |
); | |
} else { | |
return ListView.separated( | |
separatorBuilder: (context, index) => Divider( | |
color: Colors.black12, | |
), | |
itemCount: snapshot.data.length, | |
itemBuilder: (BuildContext context, int index) { | |
return ListTile( | |
leading: Text( | |
"${index + 1}", | |
style: TextStyle(fontSize: 20.0), | |
), | |
title: Text( | |
"Name: ${snapshot.data[index].firstName} ${snapshot.data[index].lastName} "), | |
subtitle: Text('EMAIL: ${snapshot.data[index].email}'), | |
); | |
}, | |
); | |
} | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment