سلام وقت بخیر استاد
من یه پروژه دفترچه یادداشت رو دارم پیاده میکنم که خطا نمیده بهم ولی تو دیتابیس ذخیره نمیکنه برام.
کدهای صفحه دیتابیس
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import 'package:vklnote/models/note_model.dart';
class DbProvider {
DbProvider._();
static final DbProvider db = DbProvider._();
static late Database _database;
Future<Database> get database async {
_database = await initDb();
return _database;
}
initDb() async {
return await openDatabase(join(await getDatabasesPath(), 'note.db'),
onCreate: (db, version) async {
var query = 'create table tbl_note(id Integer PRIMARY KEY AUTOINCREMENT,'
'title varchar(100), description Text, dateNote Text , timeNote Text)';
db.execute(query);
}, version: 1);
}
addNote(NoteModel note) async {
final db = await database;
int result = await db.rawInsert(
'insert into tbl_note(title,description,dateNote,timeNote)values(?,?,?,?)',
[note.title, note.description, note.date, note.time]);
return result;
}
}
کدهای صفحه note_model
class NoteModel {
int? id;
String? title;
String? description;
String? date;
String? time;
NoteModel({this.id, required this.title, required this.description, required this.date, required this.time});
}
کدهای پروژه
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:vklnote/customview/persiantext.dart';
import 'package:vklnote/db/database.dart';
import 'package:vklnote/models/note_model.dart';
class AddNotePage extends StatefulWidget {
const AddNotePage({Key? key}) : super(key: key);
@override
State createState() => _AddNotePageState();
}
class _AddNotePageState extends State {
String title = '';
String description = '';
String dateNote = '';
String timeNote = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.green,
title: PersianText(
title: 'دفترچه یادداشت', fontSize: 14, fontColor: Colors.white),
centerTitle: true,
),
body: Container(
height: double.infinity - 60,
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Container(
margin: EdgeInsets.only(right: 16,bottom: 16,top: 16),
child: PersianText(
title: ': عنوان یادداشت',
fontSize: 14.0,
fontColor: Colors.black)),
Container(
margin: EdgeInsets.only(right: 16, left: 16),
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
),
onChanged: (value) {
setState(() {
title = value;
});
},
),
),
Container(
margin: EdgeInsets.only(right: 16,bottom: 16,top: 16),
child: PersianText(
title: ': توضیحات یادداشت',
fontSize: 14.0,
fontColor: Colors.black)),
Container(
margin: EdgeInsets.only(right: 16, left: 16, bottom: 16),
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
),
onChanged: (value) {
setState(() {
description = value;
});
},
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
DatePicker.showDatePicker(context,
showTitleActions: true,
minTime: DateTime(2000, 1, 1),
maxTime: DateTime(2099, 12, 29),
onChanged: (date) {
print('change $date');
}, onConfirm: (date) {
print('confirm $date');
this.dateNote =
'${date.year}/${date.month}/${date.day}';
},
currentTime: DateTime.now(),
locale: LocaleType.fa);
},
child: Text('تاریخ'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
fixedSize: Size(80, 40))),
SizedBox(width: 16),
ElevatedButton(
onPressed: () {
DatePicker.showTimePicker(context,
showTitleActions: true, onChanged: (date) {
print('change $date in time zone ' +
date.timeZoneOffset.inHours.toString());
}, onConfirm: (date) {
print('confirm $date');
timeNote =
'${date.hour}/${date.minute}/${date.second}';
}, currentTime: DateTime.now());
},
child: Text('زمان'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
fixedSize: Size(80, 40))),
],
),
],
),
),
Container(
margin: EdgeInsets.only(left: 16, right: 16, bottom: 16),
width: double.infinity,
child: ElevatedButton(
onPressed: () async {
NoteModel note = NoteModel(
title: title,
description: description,
date: dateNote,
time: timeNote);
int result = await DbProvider.db.addNote(note);
if (result > 0) {
SnackBar snackBar = SnackBar(
content: PersianText(
title: 'یادداشت افزوده شد',
fontSize: 14.0,
fontColor: Colors.white),
backgroundColor: Colors.green,
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
} else {
SnackBar snackBar = SnackBar(
content: PersianText(
title: 'حطا در عملیات',
fontSize: 14.0,
fontColor: Colors.white),
backgroundColor: Colors.black,
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
},
child: PersianText(title: 'ذخیره یادداشت', fontSize: 14, fontColor: Colors.white),
style: ElevatedButton.styleFrom(backgroundColor: Colors.green)),
),
],
),
)
);
}
}