Untitled
unknown
plain_text
3 years ago
4.5 kB
17
Indexable
import 'package:flutter/material.dart';
import 'package:sqflite/sqflite.dart' as sql;
import 'package:path/path.dart' as path;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
TextEditingController titleInput = TextEditingController();
List<Map<String, dynamic>> dataInput = [];
void _refreshData() async {
final data = await Sqlite.getItems();
setState(() {
dataInput = data;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: TextField(controller: titleInput),
),
body: ListView.builder(
itemCount: dataInput.length,
itemBuilder: (context, index) {
if (dataInput.isEmpty) {
Text("No Data");
} else if (dataInput.isNotEmpty) {
return ListTile(
title: Text(dataInput[index]['title']),
onLongPress: () async {
await Sqlite.deleteLocation(dataInput[index]['id']);
_refreshData();
print(dataInput);
},
onTap: () async {
await Sqlite.updateItem(
dataInput[index]['id'], titleInput.text);
_refreshData();
print(dataInput);
});
}
},
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
await Sqlite.createItem(titleInput.text);
_refreshData();
print(dataInput);
},
tooltip: 'Increment',
child: const Icon(Icons.save),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class Sqlite {
static Future<sql.Database> db() async {
return sql.openDatabase(
'kindacode.db',
version: 1,
onCreate: (sql.Database database, int version) async {
await createTables(database);
},
);
}
static Future<void> createTables(sql.Database database) async {
await database.execute("""CREATE TABLE items(
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
title TEXT,
createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)
""");
}
static Future<List<Map<String, dynamic>>> getItems() async {
final db = await Sqlite.db();
return db.query('items', orderBy: "id");
}
static Future<int> createItem(String title) async {
final db = await Sqlite.db();
final data = {'title': title};
final id = await db.insert('items', data,
conflictAlgorithm: sql.ConflictAlgorithm.replace);
return id;
}
static Future<int> updateItem(int id, String title) async {
final db = await Sqlite.db();
final data = {'title': title, 'createdAt': DateTime.now().toString()};
final result =
await db.update('items', data, where: "id = ?", whereArgs: [id]);
return result;
}
static Future<void> deleteLocation(int id) async {
// Get a reference to the database.
final db = await Sqlite.db();
await db.delete(
'items',
where: "id = ?",
whereArgs: [id],
);
}
}
Editor is loading...