Model
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
final String tableName = "todom";
final String Column_id = "id";
final String Column_name = "name";
class TaskModel{
final String name;
int id;
TaskModel({this.name, this.id});
Map<String, dynamic> toMap(){
return {
Column_name : this.name
};
}
}
class TodoHelper{
Database db;
TodoHelper(){
initDatabase();
}
Future<void> initDatabase() async{
db = await openDatabase(
join(await getDatabasesPath(), "databse.db"),
onCreate: (db, version){
return db.execute("CREATE TABLE $tableName($Column_id INTEGER PRIMARY KEY AUTOINCREMENT, $Column_name TEXT)");
},
version: 1
);
}
Future<void> insertTask(TaskModel task) async{
try{
db.insert(tableName, task.toMap(), conflictAlgorithm: ConflictAlgorithm.replace);
}catch(_){
print(_);
}
}
Future<List<TaskModel>> getAllTask () async{
final List<Map<String, dynamic>> tasks = await db.query(tableName);
return List.generate(tasks.length, (i){
return TaskModel(name: tasks[i][Column_name], id: tasks[i][Column_id]);
});
}
}
main.dart
import 'package:flutter/material.dart';
import 'TaskModel.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final textController = TextEditingController();
List<TaskModel> tasks = [];
TaskModel currentTask;
@override
Widget build(BuildContext context) {
final TodoHelper _todoHelper = TodoHelper();
return Scaffold(
body: Container(
padding:EdgeInsets.all(32),
child: Column(
children: <Widget>[
TextField(
controller: textController,
),
FlatButton(
child: Text("Insert"),
onPressed: (){
currentTask = TaskModel(name: textController.text);
_todoHelper.insertTask(currentTask);
},
color: Colors.blue,
textColor: Colors.white,
),
FlatButton(
child: Text("Show All Task"),
onPressed: () async{
List<TaskModel> list = await _todoHelper.getAllTask();
setState(() {
tasks = list;
});
},
color: Colors.red,
textColor: Colors.white,
),
Expanded(
child: ListView.separated(
itemBuilder: (context, index){
return ListTile(
leading: Text("${tasks[index].id}"),
title: Text("${tasks[index].name}"),
);
},
separatorBuilder: (context, index) => Divider(),
itemCount: tasks.length,
),
)
],
),
),
);
}
}