Initial commit
This commit is contained in:
88
lib/bups/dbhelper_example.dart
Normal file
88
lib/bups/dbhelper_example.dart
Normal file
@@ -0,0 +1,88 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:path/path.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
class DatabaseHelper {
|
||||
static final _databaseName = "MyDatabase.db";
|
||||
static final _databaseVersion = 1;
|
||||
|
||||
static final table = 'my_table';
|
||||
|
||||
static final columnId = '_id';
|
||||
static final columnName = 'name';
|
||||
static final columnAge = 'age';
|
||||
|
||||
// make this a singleton class
|
||||
DatabaseHelper._privateConstructor();
|
||||
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
|
||||
|
||||
// only have a single app-wide reference to the database
|
||||
static Database _database;
|
||||
Future<Database> get database async {
|
||||
if (_database != null) return _database;
|
||||
// lazily instantiate the db the first time it is accessed
|
||||
_database = await _initDatabase();
|
||||
return _database;
|
||||
}
|
||||
|
||||
// this opens the database (and creates it if it doesn't exist)
|
||||
_initDatabase() async {
|
||||
Directory documentsDirectory = await getApplicationDocumentsDirectory();
|
||||
String path = join(documentsDirectory.path, _databaseName);
|
||||
return await openDatabase(path,
|
||||
version: _databaseVersion, onCreate: _onCreate);
|
||||
}
|
||||
|
||||
// SQL code to create the database table
|
||||
Future _onCreate(Database db, int version) async {
|
||||
await db.execute('''
|
||||
CREATE TABLE $table (
|
||||
$columnId INTEGER PRIMARY KEY,
|
||||
$columnName TEXT NOT NULL,
|
||||
$columnAge INTEGER NOT NULL
|
||||
)
|
||||
''');
|
||||
}
|
||||
|
||||
// Helper methods
|
||||
|
||||
// Inserts a row in the database where each key in the Map is a column name
|
||||
// and the value is the column value. The return value is the id of the
|
||||
// inserted row.
|
||||
Future<int> insert(Map<String, dynamic> row) async {
|
||||
Database db = await instance.database;
|
||||
return await db.insert(table, row);
|
||||
}
|
||||
|
||||
// All of the rows are returned as a list of maps, where each map is
|
||||
// a key-value list of columns.
|
||||
Future<List<Map<String, dynamic>>> queryAllRows() async {
|
||||
Database db = await instance.database;
|
||||
return await db.query(table);
|
||||
}
|
||||
|
||||
// All of the methods (insert, query, update, delete) can also be done using
|
||||
// raw SQL commands. This method uses a raw query to give the row count.
|
||||
Future<int> queryRowCount() async {
|
||||
Database db = await instance.database;
|
||||
return Sqflite.firstIntValue(
|
||||
await db.rawQuery('SELECT COUNT(*) FROM $table'));
|
||||
}
|
||||
|
||||
// We are assuming here that the id column in the map is set. The other
|
||||
// column values will be used to update the row.
|
||||
Future<int> update(Map<String, dynamic> row) async {
|
||||
Database db = await instance.database;
|
||||
int id = row[columnId];
|
||||
return await db.update(table, row, where: '$columnId = ?', whereArgs: [id]);
|
||||
}
|
||||
|
||||
// Deletes the row specified by the id. The number of affected rows is
|
||||
// returned. This should be 1 as long as the row exists.
|
||||
Future<int> delete(int id) async {
|
||||
Database db = await instance.database;
|
||||
return await db.delete(table, where: '$columnId = ?', whereArgs: [id]);
|
||||
}
|
||||
}
|
||||
5
lib/bups/dummy.dart
Normal file
5
lib/bups/dummy.dart
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
|
||||
void main() {
|
||||
runApp(const FormApp());
|
||||
}
|
||||
83
lib/bups/firebase_options.dart
Normal file
83
lib/bups/firebase_options.dart
Normal file
@@ -0,0 +1,83 @@
|
||||
// File generated by FlutterFire CLI.
|
||||
// ignore_for_file: lines_longer_than_80_chars, avoid_classes_with_only_static_members
|
||||
import 'package:firebase_core/firebase_core.dart' show FirebaseOptions;
|
||||
import 'package:flutter/foundation.dart'
|
||||
show defaultTargetPlatform, kIsWeb, TargetPlatform;
|
||||
|
||||
/// Default [FirebaseOptions] for use with your Firebase apps.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// import 'firebase_options.dart';
|
||||
/// // ...
|
||||
/// await Firebase.initializeApp(
|
||||
/// options: DefaultFirebaseOptions.currentPlatform,
|
||||
/// );
|
||||
/// ```
|
||||
class DefaultFirebaseOptions {
|
||||
static FirebaseOptions get currentPlatform {
|
||||
if (kIsWeb) {
|
||||
return web;
|
||||
}
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.android:
|
||||
return android;
|
||||
case TargetPlatform.iOS:
|
||||
return ios;
|
||||
case TargetPlatform.macOS:
|
||||
return macos;
|
||||
case TargetPlatform.windows:
|
||||
throw UnsupportedError(
|
||||
'DefaultFirebaseOptions have not been configured for windows - '
|
||||
'you can reconfigure this by running the FlutterFire CLI again.',
|
||||
);
|
||||
case TargetPlatform.linux:
|
||||
throw UnsupportedError(
|
||||
'DefaultFirebaseOptions have not been configured for linux - '
|
||||
'you can reconfigure this by running the FlutterFire CLI again.',
|
||||
);
|
||||
default:
|
||||
throw UnsupportedError(
|
||||
'DefaultFirebaseOptions are not supported for this platform.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
static const FirebaseOptions web = FirebaseOptions(
|
||||
apiKey: 'AIzaSyC3r_l3tj1x9Za0XXio89OIFWIY3Um40c8',
|
||||
appId: '1:497486937927:web:66bcf53b6b41b7d84d5662',
|
||||
messagingSenderId: '497486937927',
|
||||
projectId: 'dash-6e8c2',
|
||||
authDomain: 'dash-6e8c2.firebaseapp.com',
|
||||
storageBucket: 'dash-6e8c2.appspot.com',
|
||||
measurementId: 'G-XZVH49N86G',
|
||||
);
|
||||
|
||||
static const FirebaseOptions android = FirebaseOptions(
|
||||
apiKey: 'AIzaSyB5CA0f2J9qB4zt5YLaXCX-lMP8dKkFpmM',
|
||||
appId: '1:497486937927:android:133640261945f9744d5662',
|
||||
messagingSenderId: '497486937927',
|
||||
projectId: 'dash-6e8c2',
|
||||
storageBucket: 'dash-6e8c2.appspot.com',
|
||||
);
|
||||
|
||||
static const FirebaseOptions ios = FirebaseOptions(
|
||||
apiKey: 'AIzaSyA6mRZkuPnAFsvfYeClMRpeuB174x5-Aqk',
|
||||
appId: '1:497486937927:ios:0c6b9a9a0c06b4484d5662',
|
||||
messagingSenderId: '497486937927',
|
||||
projectId: 'dash-6e8c2',
|
||||
storageBucket: 'dash-6e8c2.appspot.com',
|
||||
iosClientId: '497486937927-80niucdhlshtgeragten316bdnpqgo8u.apps.googleusercontent.com',
|
||||
iosBundleId: 'com.example.dash',
|
||||
);
|
||||
|
||||
static const FirebaseOptions macos = FirebaseOptions(
|
||||
apiKey: 'AIzaSyA6mRZkuPnAFsvfYeClMRpeuB174x5-Aqk',
|
||||
appId: '1:497486937927:ios:0c6b9a9a0c06b4484d5662',
|
||||
messagingSenderId: '497486937927',
|
||||
projectId: 'dash-6e8c2',
|
||||
storageBucket: 'dash-6e8c2.appspot.com',
|
||||
iosClientId: '497486937927-80niucdhlshtgeragten316bdnpqgo8u.apps.googleusercontent.com',
|
||||
iosBundleId: 'com.example.dash',
|
||||
);
|
||||
}
|
||||
417
lib/bups/main - Copy.dart
Normal file
417
lib/bups/main - Copy.dart
Normal file
@@ -0,0 +1,417 @@
|
||||
import 'dart:collection';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'dart:async';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
// import 'package:flutter/src/widgets/form.dart';
|
||||
|
||||
void main() async {
|
||||
runApp(
|
||||
ChangeNotifierProvider(
|
||||
create: (context) => GarageModel(),
|
||||
child: MaterialApp(
|
||||
title: 'Dashboard',
|
||||
initialRoute: '/',
|
||||
routes: {
|
||||
'/' : (context) => const MyApp(),
|
||||
'/newcar' : (context) => const NewCarScreen(),
|
||||
'/about': (context) => const AboutScreen(),
|
||||
},
|
||||
// home: MyApp()
|
||||
),
|
||||
),
|
||||
);
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
final database = openDatabase(
|
||||
join(await getDatabasesPath(), 'dash_db_sqlite.db'),
|
||||
onCreate: (db, version) {
|
||||
return db.execute(
|
||||
'''CREATE TABLE cars(
|
||||
id INT INCREMENT,
|
||||
nickname STRING,
|
||||
vin STRING PRIMARY KEY,
|
||||
mileage INT,
|
||||
owner STRING)''',
|
||||
);
|
||||
},
|
||||
version: 1,
|
||||
);
|
||||
|
||||
/*
|
||||
Future<void> insertCar(Car car) async {
|
||||
final db = await database;
|
||||
await db.insert(
|
||||
'cars',
|
||||
car.toMap(),
|
||||
conflictAlgorithm: ConflictAlgorithm.abort,
|
||||
);
|
||||
}
|
||||
|
||||
Future<List<Car>> getCars() async {
|
||||
final db = await database;
|
||||
final List<Map<String, dynamic>> maps = await db.query('cars');
|
||||
return List.generate(maps.length, (i) {
|
||||
return Car(
|
||||
nickname: maps[i]['nickname'],
|
||||
vin: maps[i]['vin'],
|
||||
plate: maps[i]['plate'],
|
||||
mileage: maps[i]['mileage'],
|
||||
owner: maps[i]['owner'],
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> updateCar(Car car) async {
|
||||
final db = await database;
|
||||
await db.update(
|
||||
'cars',
|
||||
car.toMap(),
|
||||
where: 'vin=?',
|
||||
whereArgs: [car.vin],
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> deleteCar(Car car) async {
|
||||
final db = await database;
|
||||
await db.delete(
|
||||
'cars',
|
||||
where: 'vin=?',
|
||||
whereArgs: ['vin'],
|
||||
);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
class GarageModel extends ChangeNotifier {
|
||||
/// internal state of garage
|
||||
final List<Car> _cars = [];
|
||||
|
||||
UnmodifiableListView<Car> get cars => UnmodifiableListView(_cars);
|
||||
|
||||
void add(Car car) {
|
||||
_cars.add(car);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void removeAll() {
|
||||
_cars.clear();
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
/* class Car {
|
||||
final String vin;
|
||||
final String plate;
|
||||
final String nickname;
|
||||
final int mileage;
|
||||
final String owner;
|
||||
|
||||
const Car({
|
||||
required this.vin,
|
||||
required this.plate,
|
||||
required this.nickname,
|
||||
required this.owner,
|
||||
required this.mileage,
|
||||
});
|
||||
|
||||
// build map, keys must match db col names
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'vin': vin,
|
||||
'plate': plate,
|
||||
'nickname': nickname,
|
||||
'owner': owner,
|
||||
'mileage': mileage,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Car{nickname: $nickname, vin: $vin, plate: $plate, owner: $owner}';
|
||||
}
|
||||
} */
|
||||
|
||||
class Car {
|
||||
Car({this.vin, this.plate, this.nickname, this.owner, this.mileage});
|
||||
|
||||
String? vin;
|
||||
String? plate;
|
||||
String? nickname;
|
||||
String? owner;
|
||||
int? mileage;
|
||||
}
|
||||
|
||||
class MyApp extends StatefulWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
State<MyApp> createState() => _MyAppState();
|
||||
}
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
/* _list() => Expanded(
|
||||
child: Card(
|
||||
margin: EdgeInsets.fromLTRB(20,30,20,0),
|
||||
child: ListView.builder(
|
||||
padding: EdgeInsets.all(8),
|
||||
itemBuilder: (context, index){
|
||||
return Column(
|
||||
children: <Widget>[
|
||||
ListTile(
|
||||
leading: Icon(Icons.directions_car),
|
||||
title: Text(_cares[index].nickname ?? ''),
|
||||
subtitle: Text(_cares[index].vin ?? '')m
|
||||
),
|
||||
Divider(height: 5.0),
|
||||
]
|
||||
);
|
||||
},
|
||||
itemCount: _cares.length,
|
||||
)
|
||||
)
|
||||
); */
|
||||
|
||||
/* Widget carInfo = Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: _list(),
|
||||
); */
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: Colors.indigo,
|
||||
title: const Text('Dashboard'),
|
||||
// leading: IconButton(
|
||||
// icon: const Icon(Icons.menu),
|
||||
// ),
|
||||
),
|
||||
drawer: Drawer(
|
||||
child: ListView(
|
||||
padding: EdgeInsets.zero,
|
||||
children: <Widget>[
|
||||
const SizedBox(
|
||||
height: 64.0,
|
||||
child: DrawerHeader(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue,
|
||||
),
|
||||
child: Text(
|
||||
'Header',
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 18,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
ListTile(
|
||||
leading: Icon(Icons.settings),
|
||||
title: Text('Settings'),
|
||||
onTap: () {
|
||||
print('settings');
|
||||
},
|
||||
// do something
|
||||
),
|
||||
ListTile(
|
||||
leading: Icon(Icons.sync),
|
||||
title: Text('Setup Database'),
|
||||
onTap: () {
|
||||
print('dbsetup');
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
leading: Icon(Icons.info_outline),
|
||||
title: Text('About'),
|
||||
onTap: () {
|
||||
Navigator.of(context).push(MaterialPageRoute(
|
||||
builder: (context) => const AboutScreen()));
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
body: Container(
|
||||
color: Colors.lightBlue,
|
||||
child: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: _CarList(),
|
||||
),
|
||||
),
|
||||
const Divider(height: 3, color: Colors.grey),
|
||||
]
|
||||
)
|
||||
),
|
||||
/* ListView.builder(itemBuilder: (_, index) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: carInfo,
|
||||
);
|
||||
}), */
|
||||
floatingActionButton: FloatingActionButton(
|
||||
child: const Icon(Icons.add),
|
||||
onPressed: () { //replace with route
|
||||
Navigator.pushNamed(context, '/newcar');
|
||||
/* Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (context) => const NewCarScreen())); */
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// _list() => Container();
|
||||
|
||||
void updateCars(Care care) {
|
||||
setState(() {
|
||||
_cares.add(care);
|
||||
});
|
||||
} //will this work???????????????????????????
|
||||
}
|
||||
|
||||
class AboutScreen extends StatelessWidget {
|
||||
// change to AboutDialog
|
||||
const AboutScreen(
|
||||
{super.key}); //https://api.flutter.dev/flutter/material/AboutDialog-class.html
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('About Page'),
|
||||
backgroundColor: Colors.indigo,
|
||||
),
|
||||
body: const Center(
|
||||
child: Text('About Page Text, Lorum Ipsum and whatnot.'),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
class NewCarScreen extends StatefulWidget {
|
||||
const NewCarScreen({super.key});
|
||||
|
||||
@override
|
||||
State<NewCarScreen> createState() => _NewCarScreenState();
|
||||
}
|
||||
|
||||
class _NewCarScreenState extends State<NewCarScreen> {
|
||||
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
||||
Care _care = Care();
|
||||
List<Care> _cares = [];
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Add New Car'),
|
||||
backgroundColor: Colors.indigo,
|
||||
),
|
||||
body: Container(
|
||||
padding: EdgeInsets.all(32),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
//new car form text fields
|
||||
TextFormField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Nickname',
|
||||
),
|
||||
onSaved: (val) => setState(() => _care.nickname = val),
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a nickname';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'VIN',
|
||||
),
|
||||
onSaved: (val) => setState(() => _care.vin = val),
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a unique VIN';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Mileage',
|
||||
),
|
||||
onSaved: (val) =>
|
||||
setState(() => _care.mileage = int.parse(val ?? "")),
|
||||
// inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly] //try this?
|
||||
keyboardType: TextInputType.number,
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter car\'s current mileage';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16.0),
|
||||
child: ElevatedButton(
|
||||
onPressed: () {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
_onSubmit();
|
||||
}
|
||||
},
|
||||
child: Row(
|
||||
children: const <Widget>[
|
||||
Icon(Icons.add),
|
||||
Text('Add Car'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
_onSubmit() {
|
||||
var form = _formKey.currentState!;
|
||||
// _formKey.currentState!.save();
|
||||
form.save(); //callback in form text fields
|
||||
print(_care.nickname);
|
||||
setState(() { //ensure you re-pass values,
|
||||
_cares.add(Care(vin:_care.vin,nickname:_care.nickname,owner:_care.owner, plate:_care.plate, mileage:_care.mileage));
|
||||
});
|
||||
setState(() {
|
||||
widget._list.add(_care));
|
||||
});
|
||||
// widget.onSubmit(_care);
|
||||
form.reset();
|
||||
}
|
||||
|
||||
Future<void> _returnHome(BuildContext context) async {
|
||||
final newCar = await Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => const MyApp()),
|
||||
);
|
||||
|
||||
if (!mounted) return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* class SelectCarIconScreen extends StatelessWidget {
|
||||
const SelectCarIconScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return
|
||||
}
|
||||
} */
|
||||
264
lib/bups/main-med.dart
Normal file
264
lib/bups/main-med.dart
Normal file
@@ -0,0 +1,264 @@
|
||||
// Copyright 2018 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:dash/utils/dbhelper_sqflite.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
// import 'package:flutter/provider.dart';
|
||||
import 'dart:async';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
|
||||
|
||||
void main() async {
|
||||
runApp(MaterialApp(home: MyApp()));
|
||||
final dbHelper = DbHelperSqlite.instance; //medium method
|
||||
|
||||
var newCar = const Car(
|
||||
vin: '123abc',
|
||||
plate: 'n1ce',
|
||||
nickname: 'My Car',
|
||||
owner: 'Ben',
|
||||
mileage: 7,
|
||||
);
|
||||
}
|
||||
|
||||
class Car {
|
||||
final String vin;
|
||||
final String plate;
|
||||
final String nickname;
|
||||
final int mileage;
|
||||
final String owner;
|
||||
|
||||
const Car({
|
||||
required this.vin,
|
||||
required this.plate,
|
||||
required this.nickname,
|
||||
required this.owner,
|
||||
required this.mileage,
|
||||
});
|
||||
|
||||
// build map, keys must match db col names
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'vin': vin,
|
||||
'plate': plate,
|
||||
'nickname': nickname,
|
||||
'owner': owner,
|
||||
'mileage': mileage,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Car{nickname: $nickname, vin: $vin, plate: $plate, owner: $owner}';
|
||||
}
|
||||
}
|
||||
|
||||
class MyApp extends StatefulWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
State<MyApp> createState() => _MyAppState();
|
||||
}
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget carInfo = Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Row(children: [
|
||||
Image.asset(
|
||||
'images/mazda.png',
|
||||
),
|
||||
Icon(
|
||||
Icons.directions_car_filled,
|
||||
color: Colors.red[500],
|
||||
),
|
||||
const Text('Nickname'),
|
||||
]),
|
||||
);
|
||||
|
||||
return MaterialApp(
|
||||
title: 'Dashboard App',
|
||||
// home: const ManageScreen(),
|
||||
home: Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: Colors.indigo,
|
||||
title: const Text('Dashboard'),
|
||||
// leading: IconButton(
|
||||
// icon: const Icon(Icons.menu),
|
||||
// ),
|
||||
),
|
||||
drawer: Drawer(
|
||||
child: ListView(
|
||||
padding: EdgeInsets.zero,
|
||||
children: <Widget>[
|
||||
const SizedBox(
|
||||
height: 64.0,
|
||||
child: DrawerHeader(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue,
|
||||
),
|
||||
child: Text(
|
||||
'Header',
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 18,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
ListTile(
|
||||
leading: Icon(Icons.settings),
|
||||
title: Text('Settings'),
|
||||
onTap: () {
|
||||
print('settings');
|
||||
},
|
||||
// do something
|
||||
),
|
||||
ListTile(
|
||||
leading: Icon(Icons.sync),
|
||||
title: Text('Setup Database'),
|
||||
onTap: () {
|
||||
print('dbsetup');
|
||||
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
leading: Icon(Icons.info_outline),
|
||||
title: Text('About'),
|
||||
onTap: () {
|
||||
Navigator.of(context).push(MaterialPageRoute(
|
||||
builder: (context) => const AboutScreen()));
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
body: ListView.builder(itemBuilder: (_, index) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: carInfo,
|
||||
);
|
||||
}),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (context) => const NewCarScreen()));
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class AboutScreen extends StatelessWidget { // change to AboutDialog
|
||||
const AboutScreen({super.key}); //https://api.flutter.dev/flutter/material/AboutDialog-class.html
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('About Page'),
|
||||
backgroundColor: Colors.indigo,
|
||||
),
|
||||
body: const Center(
|
||||
child: Text('About Page Text, Lorum Ipsum and whatnot.'),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
class NewCarScreen extends StatefulWidget {
|
||||
const NewCarScreen({super.key});
|
||||
|
||||
@override
|
||||
State<NewCarScreen> createState() => _NewCarScreenState();
|
||||
}
|
||||
|
||||
class _NewCarScreenState extends State<NewCarScreen> {
|
||||
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Add New Car'),
|
||||
backgroundColor: Colors.indigo,
|
||||
),
|
||||
body: Container(
|
||||
padding: EdgeInsets.all(32),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
// Text('Add new car form here'),
|
||||
TextFormField(
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Nickname for this car',
|
||||
),
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a nickname';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Unique VIN',
|
||||
),
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a unique VIN';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Mileage',
|
||||
),
|
||||
keyboardType: TextInputType.number,
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter car\'s current mileage';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16.0),
|
||||
child: ElevatedButton(
|
||||
onPressed: () {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
// process
|
||||
}
|
||||
},
|
||||
child: Row(
|
||||
children: const <Widget>[
|
||||
Icon(Icons.add),
|
||||
Text('Add Car'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class SelectCarIconScreen extends StatelessWidget {
|
||||
const SelectCarIconScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return
|
||||
}
|
||||
}
|
||||
625
lib/bups/main_20221031.dart
Normal file
625
lib/bups/main_20221031.dart
Normal file
@@ -0,0 +1,625 @@
|
||||
import 'dart:collection';
|
||||
import 'package:dash/utils/dbhelper_sqflite.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
// import 'package:path/path.dart';
|
||||
// import 'package:sqflite/sqflite.dart';
|
||||
import 'package:dash/models/car.dart';
|
||||
// import 'package:flutter/src/widgets/form.dart';
|
||||
|
||||
void main() async {
|
||||
runApp(
|
||||
ChangeNotifierProvider(
|
||||
create: (context) => GarageModel(),
|
||||
child: const MaterialApp(
|
||||
title: 'Dashboard',
|
||||
home: MyApp(),
|
||||
/* initialRoute: '/',
|
||||
routes: {
|
||||
'/': (context) => const MyApp(),
|
||||
'/about': (context) => const AboutScreen(),
|
||||
'/newcar': (context) => const NewCarScreen(),
|
||||
'/detail': (context) => const CarDetailScreen(),
|
||||
'/edit': (context) => EditCarScreen(),
|
||||
}, */
|
||||
),
|
||||
),
|
||||
);
|
||||
// WidgetsFlutterBinding.ensureInitialized(); // is this necessary?
|
||||
}
|
||||
|
||||
class GarageModel extends ChangeNotifier {
|
||||
/// internal state of garage
|
||||
late List<Car> _cars = [];
|
||||
final DbHelperSqlite _dbHelper = DbHelperSqlite.instance;
|
||||
UnmodifiableListView<Car> get cars => UnmodifiableListView(_cars);
|
||||
|
||||
void add(Car car) {
|
||||
_cars.add(car);
|
||||
_dbHelper.insertCar(car);
|
||||
// getCars(); // prob nec to update `id`
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void update(Car car, int carIndex) {
|
||||
_cars[carIndex] = car; // replace list car with new values
|
||||
_dbHelper.updateCar(car);
|
||||
print('car.id is ${car.id}');
|
||||
getCars();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void delete(Car car) {
|
||||
_cars.removeWhere((_car) => _car.id == car.id);
|
||||
_dbHelper.deleteCar(car);
|
||||
print('${car} deleted');
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void clearCarList() {
|
||||
// clear _cars; doesn't affect database
|
||||
// useful for clearing cars that didn't make it into the db
|
||||
_cars.clear();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void deleteAllCars() {
|
||||
// delete rows from CarTable
|
||||
_cars.clear();
|
||||
_dbHelper.deleteAll();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> getCars() async {
|
||||
// _cars.clear();
|
||||
print('getcars');
|
||||
_cars = await _dbHelper.fetchCars();
|
||||
print(_cars);
|
||||
// notifyListeners(); //for some reason this inits db infinitely
|
||||
}
|
||||
}
|
||||
|
||||
class Garage extends StatefulWidget {
|
||||
const Garage({super.key});
|
||||
@override
|
||||
State<Garage> createState() => _Garage();
|
||||
}
|
||||
|
||||
class _Garage extends State<Garage> {
|
||||
late Future _cars;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
_cars = Provider.of<GarageModel>(context, listen: false).getCars();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FutureBuilder(
|
||||
future: _cars,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
} else {
|
||||
if (snapshot.error != null) {
|
||||
return const Center(
|
||||
child: Text('An error occurred'),
|
||||
);
|
||||
} else {
|
||||
return Expanded(
|
||||
child: Consumer<GarageModel>(builder: (context, garage, child) {
|
||||
return ListView.separated(
|
||||
// padding: const EdgeInsets.all(8),
|
||||
itemCount: garage._cars.length,
|
||||
itemBuilder: (context, index) => ListTile(
|
||||
leading: const Icon(Icons.directions_car),
|
||||
title: Text(garage.cars[index].nickname ?? "nick_ph"),
|
||||
subtitle: Text(garage.cars[index].vin ?? "vin_ph"),
|
||||
// onTap: () => Navigator.pushNamed(context, '/detail'),
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => EditCarScreen(
|
||||
car: garage._cars[index], carIndex: index)),
|
||||
);
|
||||
}),
|
||||
separatorBuilder: (BuildContext context, int index) =>
|
||||
const Divider(),
|
||||
);
|
||||
}));
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _CarList extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Consumer<GarageModel>(
|
||||
builder: (context, garage, child) {
|
||||
return ListView.separated(
|
||||
// padding: const EdgeInsets.all(8),
|
||||
itemCount: garage._cars.length,
|
||||
itemBuilder: (context, index) => ListTile(
|
||||
leading: const Icon(Icons.directions_car),
|
||||
title: Text(garage.cars[index].nickname ?? "nick_ph"),
|
||||
subtitle: Text(garage.cars[index].vin ?? "vin_ph"),
|
||||
// onTap: () => Navigator.pushNamed(context, '/detail'),
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => EditCarScreen(
|
||||
car: garage._cars[index], carIndex: index)),
|
||||
);
|
||||
}),
|
||||
separatorBuilder: (BuildContext context, int index) =>
|
||||
const Divider(),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyDrawer extends StatelessWidget {
|
||||
const MyDrawer({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Drawer(
|
||||
child: ListView(
|
||||
padding: EdgeInsets.zero,
|
||||
children: <Widget>[
|
||||
const SizedBox(
|
||||
height: 80.0,
|
||||
child: DrawerHeader(
|
||||
decoration: BoxDecoration(
|
||||
color: Color.fromARGB(255, 185, 47, 5),
|
||||
),
|
||||
child: Text(
|
||||
'Options',
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 18,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.settings),
|
||||
title: const Text('Settings'),
|
||||
onTap: () {
|
||||
print('settings');
|
||||
},
|
||||
// do something
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.sync),
|
||||
title: const Text('Setup Database'),
|
||||
onTap: () {
|
||||
print('dbsetup');
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.info_outline),
|
||||
title: const Text('About'),
|
||||
onTap: () {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (context) => const AboutScreen()));
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyApp extends StatefulWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
State<MyApp> createState() => _MyAppState();
|
||||
}
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// context.watch<GarageModel>().getCars(); // testing
|
||||
var garage = context.watch<GarageModel>(); // testing
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: const Color.fromARGB(255, 185, 47, 5),
|
||||
title: const Text('Dashboard'),
|
||||
),
|
||||
drawer: const MyDrawer(),
|
||||
body: Container(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: ElevatedButton(
|
||||
onPressed: (() => garage.clearCarList()),
|
||||
// onPressed: (() => VoidCallback),
|
||||
onLongPress: () {
|
||||
garage.deleteAllCars();
|
||||
// VoidCallback;
|
||||
},
|
||||
child: Row(
|
||||
children: const <Widget>[
|
||||
Icon(Icons.delete),
|
||||
Text('Remove All Cars'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: ElevatedButton(
|
||||
onPressed: (() => garage.getCars()),
|
||||
// onPressed: (() => VoidCallback),
|
||||
child: Row(
|
||||
children: const <Widget>[
|
||||
Icon(Icons.sync),
|
||||
Text('Refresh garage'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const Garage(),
|
||||
/* FutureBuilder(
|
||||
future:
|
||||
Provider.of<GarageModel>(context, listen: false).getCars(),
|
||||
builder: (context, dataSnapshot) {
|
||||
if (dataSnapshot.connectionState == ConnectionState.waiting) {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
} else {
|
||||
if (dataSnapshot.error != null) {
|
||||
return const Center(
|
||||
child: Text('An error occurred'),
|
||||
);
|
||||
} else {
|
||||
return Expanded(
|
||||
child: _CarList(),
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
), */
|
||||
],
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
child: const Icon(Icons.add),
|
||||
onPressed: () {
|
||||
Navigator.push(context,
|
||||
MaterialPageRoute(builder: (context) => const NewCarScreen()));
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class AboutScreen extends StatelessWidget {
|
||||
// change to AboutDialog
|
||||
const AboutScreen(
|
||||
{super.key}); //https://api.flutter.dev/flutter/material/AboutDialog-class.html
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('About Page'),
|
||||
backgroundColor: Colors.indigo,
|
||||
),
|
||||
body: const Center(
|
||||
child: Text('About Page Text, Lorum Ipsum and whatnot.'),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
class NewCarScreen extends StatefulWidget {
|
||||
const NewCarScreen({super.key});
|
||||
|
||||
@override
|
||||
State<NewCarScreen> createState() => _NewCarScreenState();
|
||||
}
|
||||
|
||||
class _NewCarScreenState extends State<NewCarScreen> {
|
||||
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
||||
Car _car = Car(); //initialization is required
|
||||
|
||||
/* DbHelperSqlite _dbHelper = DbHelperSqlite.instance;
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
setState(() {
|
||||
_dbHelper = DbHelperSqlite.instance;
|
||||
});
|
||||
} */
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Add New Car'),
|
||||
backgroundColor: Colors.indigo,
|
||||
),
|
||||
body: Container(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
//new car form text fields
|
||||
TextFormField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Nickname',
|
||||
),
|
||||
onSaved: (val) => setState(() => _car.nickname = val),
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a nickname';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'VIN',
|
||||
),
|
||||
onSaved: (val) => setState(() => _car.vin = val),
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a unique VIN';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Mileage',
|
||||
),
|
||||
onSaved: (val) =>
|
||||
setState(() => _car.mileage = int.parse(val ?? "")),
|
||||
// inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly] //try this?
|
||||
keyboardType: TextInputType.number,
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter car\'s current mileage';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16.0),
|
||||
child: ElevatedButton(
|
||||
// onPressed: () => _onSubmit(context),
|
||||
onPressed: () {
|
||||
final _form = _formKey.currentState!;
|
||||
if (_form.validate()) {
|
||||
//validate form
|
||||
_form
|
||||
.save(); //save values (reqd before putting them anywhere)
|
||||
setState(() {
|
||||
//modify state of car (necessary?)
|
||||
_car = Car(
|
||||
vin: _car.vin,
|
||||
nickname: _car.nickname,
|
||||
plate: _car.plate,
|
||||
mileage: _car.mileage);
|
||||
});
|
||||
var garage =
|
||||
context.read<GarageModel>(); //implement Provider
|
||||
garage.add(_car);
|
||||
_form.reset();
|
||||
Navigator.pushNamed(context, '/');
|
||||
}
|
||||
},
|
||||
child: Row(
|
||||
children: const <Widget>[
|
||||
Icon(Icons.add),
|
||||
Text('Add Car'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
_onSubmit(context) async {
|
||||
final _form = _formKey.currentState!;
|
||||
if (_form.validate()) {
|
||||
//validate form
|
||||
_form.save(); //save values (reqd before putting them anywhere)
|
||||
setState(() {
|
||||
//modify state of car (necessary?)
|
||||
_car = Car(
|
||||
vin: _car.vin,
|
||||
nickname: _car.nickname,
|
||||
plate: _car.plate,
|
||||
mileage: _car.mileage);
|
||||
});
|
||||
var garage = context.read<GarageModel>(); //implement Provider
|
||||
garage.add(_car);
|
||||
_form.reset();
|
||||
Navigator.pushNamed(context, '/');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class CarDetailScreen extends StatelessWidget {
|
||||
const CarDetailScreen({super.key});
|
||||
|
||||
//garage? load car using Provider?
|
||||
// then feed to update screen
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: const Color.fromARGB(255, 185, 47, 5),
|
||||
title: const Text('Vehicle Name'),
|
||||
),
|
||||
drawer: const MyDrawer(),
|
||||
body: Container(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: const Text('Details here'),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
child: const Icon(Icons.edit),
|
||||
onPressed: () {
|
||||
Navigator.pushNamed(context, '/edit');
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class EditCarScreen extends StatefulWidget {
|
||||
EditCarScreen({super.key, required this.car, required this.carIndex});
|
||||
Car car;
|
||||
int carIndex;
|
||||
|
||||
@override
|
||||
State<EditCarScreen> createState() => _EditCarScreenState();
|
||||
}
|
||||
|
||||
class _EditCarScreenState extends State<EditCarScreen> {
|
||||
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
||||
late Car car = widget.car; //initialization is required
|
||||
late int carIndex = widget.carIndex;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var garage = context.read<GarageModel>();
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text("Edit ${car.nickname}"),
|
||||
backgroundColor: const Color.fromARGB(255, 185, 47, 5),
|
||||
),
|
||||
body: Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
//new car form text fields
|
||||
TextFormField(
|
||||
initialValue: car.nickname,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Nickname',
|
||||
),
|
||||
onSaved: (val) => setState(() => car.nickname = val),
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a nickname';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
initialValue: car.vin,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'VIN',
|
||||
),
|
||||
onSaved: (val) => setState(() => car.vin = val),
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a unique VIN';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
initialValue: car.mileage.toString(),
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Mileage',
|
||||
),
|
||||
onSaved: (val) =>
|
||||
setState(() => car.mileage = int.parse(val ?? "")),
|
||||
// inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly] //try this?
|
||||
keyboardType: TextInputType.number,
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter car\'s current mileage';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: ElevatedButton(
|
||||
onPressed: () {
|
||||
final updateForm = _formKey.currentState!;
|
||||
if (updateForm.validate()) {
|
||||
//validate form
|
||||
updateForm
|
||||
.save(); //save values (reqd before putting them anywhere)
|
||||
setState(() {
|
||||
//modify state of car (necessary?)
|
||||
car = Car(
|
||||
vin: car.vin,
|
||||
nickname: car.nickname,
|
||||
plate: car.plate,
|
||||
mileage: car.mileage);
|
||||
}); //implement Provider
|
||||
garage.update(car, carIndex);
|
||||
updateForm.reset();
|
||||
Navigator.pushNamed(context, '/');
|
||||
}
|
||||
},
|
||||
child: Row(
|
||||
children: const <Widget>[
|
||||
//expand these children, too tight
|
||||
Icon(Icons.save),
|
||||
Text('Save Changes'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: ElevatedButton(
|
||||
onPressed: (() => VoidCallback),
|
||||
onLongPress: () {
|
||||
garage.delete(car);
|
||||
Navigator.pushNamed(context, '/');
|
||||
},
|
||||
child: Row(children: const <Widget>[
|
||||
Icon(Icons.delete),
|
||||
Text('Delete'),
|
||||
]),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: ElevatedButton(
|
||||
onPressed: (() => VoidCallback),
|
||||
child: Row(children: const <Widget>[
|
||||
Icon(Icons.sync_alt),
|
||||
Text('enable/disable'),
|
||||
]),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
790
lib/main.dart
Normal file
790
lib/main.dart
Normal file
@@ -0,0 +1,790 @@
|
||||
import 'dart:collection';
|
||||
import 'package:dash/utils/dbhelper_sqflite.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
// import 'package:path/path.dart';
|
||||
// import 'package:sqflite/sqflite.dart';
|
||||
import 'package:dash/models/car.dart';
|
||||
import 'package:dash/models/txn.dart';
|
||||
// import 'package:flutter/src/widgets/form.dart';
|
||||
|
||||
void main() async {
|
||||
runApp(
|
||||
ChangeNotifierProvider(
|
||||
create: (context) => GarageModel(),
|
||||
child: const MaterialApp(
|
||||
title: 'Dashboard',
|
||||
home: MyApp(),
|
||||
/* initialRoute: '/',
|
||||
routes: {
|
||||
'/': (context) => const MyApp(),
|
||||
'/about': (context) => const AboutScreen(),
|
||||
'/newcar': (context) => const NewCarScreen(),
|
||||
'/detail': (context) => const CarDetailScreen(),
|
||||
'/edit': (context) => EditCarScreen(),
|
||||
}, */
|
||||
),
|
||||
),
|
||||
);
|
||||
// WidgetsFlutterBinding.ensureInitialized(); // is this necessary?
|
||||
}
|
||||
|
||||
class GarageModel extends ChangeNotifier {
|
||||
/// internal state of garage
|
||||
late List<Car> _cars = [];
|
||||
final DbHelperSqlite _dbHelper = DbHelperSqlite.instance;
|
||||
UnmodifiableListView<Car> get cars => UnmodifiableListView(_cars);
|
||||
|
||||
void add(Car car) {
|
||||
// _cars.add(car);
|
||||
_dbHelper.insertCar(car);
|
||||
getCars(); // prob nec to update `id`
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void update(Car car, int carIndex) {
|
||||
_cars[carIndex] = car; // replace list car with new values
|
||||
_dbHelper.updateCar(car);
|
||||
print('updated car ${car.toMap()}');
|
||||
getCars();
|
||||
// print(_cars);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void delete(Car car) {
|
||||
// ignore: no_leading_underscores_for_local_identifiers
|
||||
_cars.removeWhere((_car) => _car.id == car.id);
|
||||
_dbHelper.deleteCar(car);
|
||||
print('$car with id ${car.id} deleted');
|
||||
getCars();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void clearCarList() {
|
||||
// clear _cars; doesn't affect database
|
||||
// useful for clearing cars that didn't make it into the db
|
||||
_cars.clear();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void deleteAllCars() {
|
||||
// delete rows from CarTable
|
||||
_cars.clear();
|
||||
_dbHelper.deleteAll();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> getCars() async {
|
||||
// _cars.clear();
|
||||
print('getcars');
|
||||
_cars = await _dbHelper.fetchCars();
|
||||
print(_cars);
|
||||
// notifyListeners(); //for some reason this inits db infinitely
|
||||
}
|
||||
}
|
||||
|
||||
class Garage extends StatefulWidget {
|
||||
const Garage({super.key});
|
||||
@override
|
||||
State<Garage> createState() => _Garage();
|
||||
}
|
||||
|
||||
class _Garage extends State<Garage> {
|
||||
late Future _cars;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
_cars = Provider.of<GarageModel>(context, listen: false).getCars();
|
||||
// _cars = Provider.of<GarageModel>(context).getCars();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FutureBuilder(
|
||||
future: _cars,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
} else {
|
||||
if (snapshot.error != null) {
|
||||
return const Center(
|
||||
child: Text('An error occurred'),
|
||||
);
|
||||
} else {
|
||||
return Expanded(
|
||||
child: Consumer<GarageModel>(builder: (context, garage, child) {
|
||||
return ListView.separated(
|
||||
// padding: const EdgeInsets.all(8),
|
||||
itemCount: garage._cars.length,
|
||||
itemBuilder: (context, index) => ListTile(
|
||||
leading: const Icon(Icons.directions_car),
|
||||
title: Text(garage.cars[index].nickname ?? "nick_ph"),
|
||||
subtitle: Text(garage.cars[index].vin ?? "vin_ph"),
|
||||
/* trailing: IconButton(
|
||||
onPressed: Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => EditCarScreen(
|
||||
car: garage._cars[index], carIndex: index)),
|
||||
),
|
||||
icon: Icons.edit),
|
||||
),
|
||||
onTap: () => Navigator.pushNamed(context, '/detail'), */
|
||||
onTap: () {
|
||||
Navigator.of(context).push(
|
||||
// context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => CarDetailScreen(
|
||||
car: garage._cars[index], carIndex: index),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
separatorBuilder: (BuildContext context, int index) =>
|
||||
const Divider(),
|
||||
);
|
||||
}));
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyDrawer extends StatelessWidget {
|
||||
const MyDrawer({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Drawer(
|
||||
child: ListView(
|
||||
padding: EdgeInsets.zero,
|
||||
children: <Widget>[
|
||||
const SizedBox(
|
||||
height: 80.0,
|
||||
child: DrawerHeader(
|
||||
decoration: BoxDecoration(
|
||||
color: Color.fromARGB(255, 185, 47, 5),
|
||||
),
|
||||
child: Text(
|
||||
'Options',
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 18,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.settings),
|
||||
title: const Text('Settings'),
|
||||
onTap: () {
|
||||
print('settings');
|
||||
},
|
||||
// do something
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.sync),
|
||||
title: const Text('Setup Database'),
|
||||
onTap: () {
|
||||
print('dbsetup');
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.info_outline),
|
||||
title: const Text('About'),
|
||||
onTap: () {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (context) => const AboutScreen()));
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyApp extends StatefulWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
State<MyApp> createState() => _MyAppState();
|
||||
}
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var garage = context.watch<GarageModel>(); // testing
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: const Color.fromARGB(255, 185, 47, 5),
|
||||
title: const Text('Dashboard'),
|
||||
),
|
||||
drawer: const MyDrawer(),
|
||||
body: Container(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: ElevatedButton(
|
||||
onPressed: (() => garage.clearCarList()),
|
||||
// onPressed: (() => VoidCallback),
|
||||
onLongPress: () {
|
||||
garage.deleteAllCars();
|
||||
// VoidCallback;
|
||||
},
|
||||
child: Row(
|
||||
children: const <Widget>[
|
||||
Icon(Icons.delete),
|
||||
Text('Remove All Cars'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: ElevatedButton(
|
||||
onPressed: (() => garage.getCars()),
|
||||
// onPressed: (() => VoidCallback),
|
||||
child: Row(
|
||||
children: const <Widget>[
|
||||
Icon(Icons.sync),
|
||||
Text('Refresh garage'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const Garage(), // list of cars in garage
|
||||
],
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
child: const Icon(Icons.add),
|
||||
onPressed: () {
|
||||
Navigator.push(context,
|
||||
MaterialPageRoute(builder: (context) => const NewCarScreen()));
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class AboutScreen extends StatelessWidget {
|
||||
// change to AboutDialog
|
||||
const AboutScreen(
|
||||
{super.key}); //https://api.flutter.dev/flutter/material/AboutDialog-class.html
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('About Page'),
|
||||
backgroundColor: const Color.fromARGB(255, 185, 47, 5),
|
||||
),
|
||||
body: const Center(
|
||||
child: Text('About Page Text, Lorum Ipsum and whatnot.'),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
class NewCarScreen extends StatefulWidget {
|
||||
const NewCarScreen({super.key});
|
||||
|
||||
@override
|
||||
State<NewCarScreen> createState() => _NewCarScreenState();
|
||||
}
|
||||
|
||||
class _NewCarScreenState extends State<NewCarScreen> {
|
||||
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
||||
Car _car = Car(); //initialization is required
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Add New Car'),
|
||||
backgroundColor: Colors.indigo,
|
||||
),
|
||||
body: Container(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
//new car form text fields
|
||||
TextFormField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Nickname',
|
||||
),
|
||||
onSaved: (val) => setState(() => _car.nickname = val),
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a nickname';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'VIN',
|
||||
),
|
||||
onSaved: (val) => setState(() => _car.vin = val),
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a unique VIN';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'License Plate',
|
||||
),
|
||||
onSaved: (val) => setState(() => _car.plate = val),
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a license plate';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Mileage',
|
||||
),
|
||||
onSaved: (val) =>
|
||||
setState(() => _car.mileage = int.parse(val ?? "")),
|
||||
// inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly] //try this?
|
||||
keyboardType: TextInputType.number,
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter car\'s current mileage';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16.0),
|
||||
child: ElevatedButton(
|
||||
// onPressed: () => _onSubmit(context),
|
||||
onPressed: () {
|
||||
final _form = _formKey.currentState!;
|
||||
if (_form.validate()) {
|
||||
//validate form
|
||||
_form
|
||||
.save(); //save values (reqd before putting them anywhere)
|
||||
setState(() {
|
||||
//modify state of car (necessary?)
|
||||
_car = Car(
|
||||
vin: _car.vin,
|
||||
nickname: _car.nickname,
|
||||
plate: _car.plate,
|
||||
mileage: _car.mileage);
|
||||
});
|
||||
var garage =
|
||||
context.read<GarageModel>(); //implement Provider
|
||||
garage.add(_car);
|
||||
_form.reset();
|
||||
Navigator.pushNamed(context, '/');
|
||||
}
|
||||
},
|
||||
child: Row(
|
||||
children: const <Widget>[
|
||||
Icon(Icons.add),
|
||||
Text('Add Car'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
_onSubmit(context) async {
|
||||
final _form = _formKey.currentState!;
|
||||
if (_form.validate()) {
|
||||
//validate form
|
||||
_form.save(); //save values (reqd before putting them anywhere)
|
||||
setState(() {
|
||||
//modify state of car (necessary?)
|
||||
_car = Car(
|
||||
vin: _car.vin,
|
||||
nickname: _car.nickname,
|
||||
plate: _car.plate,
|
||||
mileage: _car.mileage);
|
||||
});
|
||||
var garage = context.read<GarageModel>(); //implement Provider
|
||||
garage.add(_car);
|
||||
_form.reset();
|
||||
Navigator.pushNamed(context, '/');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class CarDetailScreen extends StatelessWidget {
|
||||
const CarDetailScreen({super.key, required this.car, required this.carIndex});
|
||||
final Car car;
|
||||
final int carIndex; //pass this thru for edit screen
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// const Car car = context.read<GarageModel>()._cars[index];
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: const Color.fromARGB(255, 185, 47, 5),
|
||||
title: Text(car.nickname ?? ""),
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: [
|
||||
const Text("plh img picker"),
|
||||
Center(
|
||||
child: Ink(
|
||||
decoration: const ShapeDecoration(
|
||||
color: Colors.lightBlue, shape: CircleBorder()),
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.edit),
|
||||
color: Colors.white,
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) =>
|
||||
EditCarScreen(car: car, carIndex: carIndex)),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
IconButton(
|
||||
iconSize: 48.0,
|
||||
icon: const Icon(Icons.directions_car),
|
||||
onPressed: (() => VoidCallback),
|
||||
),
|
||||
Text("Nickname: ${car.nickname}"),
|
||||
Text("VIN: ${car.vin}"),
|
||||
Text("License Plate: ${car.plate}"),
|
||||
Text("Mileage: ${car.mileage.toString()}"),
|
||||
],
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
child: const Icon(Icons.edit),
|
||||
onPressed: (() => VoidCallback), //add Transaction
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class EditCarScreen extends StatefulWidget {
|
||||
EditCarScreen({super.key, required this.car, required this.carIndex});
|
||||
Car car;
|
||||
int carIndex;
|
||||
|
||||
@override
|
||||
State<EditCarScreen> createState() => _EditCarScreenState();
|
||||
}
|
||||
|
||||
class _EditCarScreenState extends State<EditCarScreen> {
|
||||
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
||||
late Car car = widget.car; //initialization is required
|
||||
late int carIndex = widget.carIndex;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var garage = context.read<GarageModel>();
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text("Edit ${car.nickname}"),
|
||||
backgroundColor: const Color.fromARGB(255, 185, 47, 5),
|
||||
),
|
||||
body: Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
//new car form text fields
|
||||
TextFormField(
|
||||
initialValue: car.nickname,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Nickname',
|
||||
),
|
||||
onSaved: (val) => setState(() => car.nickname = val),
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a nickname';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
initialValue: car.vin,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'VIN',
|
||||
),
|
||||
onSaved: (val) => setState(() => car.vin = val),
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a unique VIN';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
initialValue: car.plate,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'License Plate',
|
||||
),
|
||||
onSaved: (val) => setState(() => car.plate = val),
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a license plate';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
initialValue: car.mileage.toString(),
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Mileage',
|
||||
),
|
||||
onSaved: (val) =>
|
||||
setState(() => car.mileage = int.parse(val ?? "")),
|
||||
// inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly] //try this?
|
||||
keyboardType: TextInputType.number,
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter car\'s current mileage';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: ElevatedButton(
|
||||
onPressed: () {
|
||||
final updateForm = _formKey.currentState!;
|
||||
if (updateForm.validate()) {
|
||||
//validate form
|
||||
updateForm
|
||||
.save(); //save values (reqd before putting them anywhere)
|
||||
/* setState(() {
|
||||
//modify state of car (necessary?)
|
||||
car = garage._cars[carIndex];
|
||||
car
|
||||
vin: car.vin,
|
||||
nickname: car.nickname,
|
||||
plate: car.plate,
|
||||
mileage: car.mileage);
|
||||
}); //implement Provider */
|
||||
garage.update(car, carIndex);
|
||||
updateForm.reset();
|
||||
Navigator.pushNamed(context, '/');
|
||||
}
|
||||
},
|
||||
child: Row(
|
||||
children: const <Widget>[
|
||||
//expand these children, too tight
|
||||
Icon(Icons.save),
|
||||
Text('Save Changes'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: ElevatedButton(
|
||||
onPressed: (() => VoidCallback),
|
||||
onLongPress: () {
|
||||
garage.delete(car);
|
||||
Navigator.pushNamed(context, '/');
|
||||
},
|
||||
child: Row(children: const <Widget>[
|
||||
Icon(Icons.delete),
|
||||
Text('Delete'),
|
||||
]),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: ElevatedButton(
|
||||
onPressed: (() => VoidCallback),
|
||||
child: Row(children: const <Widget>[
|
||||
Icon(Icons.sync_alt),
|
||||
Text('enable/disable'),
|
||||
]),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class NewTxn extends StatefulWidget {
|
||||
NewTxn({super.key, required this.car});
|
||||
Car car;
|
||||
|
||||
@override
|
||||
State<NewTxn> createState() => _NewTxnState();
|
||||
}
|
||||
|
||||
class _NewTxnState extends State<NewTxn> {
|
||||
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
||||
late Car car = widget.car; //initialization is required
|
||||
Txn txn = Txn();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text("Edit ${car.nickname}"),
|
||||
backgroundColor: const Color.fromARGB(255, 185, 47, 5),
|
||||
),
|
||||
body: Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
//new car form text fields
|
||||
/* RadioListTile(
|
||||
value: ,
|
||||
), */
|
||||
TextFormField(
|
||||
initialValue: car.vin,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'VIN',
|
||||
),
|
||||
onSaved: (val) => setState(() => car.vin = val),
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a unique VIN';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
initialValue: car.plate,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'License Plate',
|
||||
),
|
||||
onSaved: (val) => setState(() => car.plate = val),
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a license plate';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
initialValue: car.mileage.toString(),
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Mileage',
|
||||
),
|
||||
onSaved: (val) =>
|
||||
setState(() => car.mileage = int.parse(val ?? "")),
|
||||
// inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly] //try this?
|
||||
keyboardType: TextInputType.number,
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter car\'s current mileage';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: ElevatedButton(
|
||||
onPressed: () {
|
||||
final updateForm = _formKey.currentState!;
|
||||
if (updateForm.validate()) {
|
||||
//validate form
|
||||
updateForm
|
||||
.save(); //save values (reqd before putting them anywhere)
|
||||
/* setState(() {
|
||||
//modify state of car (necessary?)
|
||||
car = garage._cars[carIndex];
|
||||
car
|
||||
vin: car.vin,
|
||||
nickname: car.nickname,
|
||||
plate: car.plate,
|
||||
mileage: car.mileage);
|
||||
}); //implement Provider */
|
||||
garage.update(car, carIndex);
|
||||
updateForm.reset();
|
||||
Navigator.pushNamed(context, '/');
|
||||
}
|
||||
},
|
||||
child: Row(
|
||||
children: const <Widget>[
|
||||
//expand these children, too tight
|
||||
Icon(Icons.save),
|
||||
Text('Save Changes'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: ElevatedButton(
|
||||
onPressed: (() => VoidCallback),
|
||||
onLongPress: () {
|
||||
garage.delete(car);
|
||||
Navigator.pushNamed(context, '/');
|
||||
},
|
||||
child: Row(children: const <Widget>[
|
||||
Icon(Icons.delete),
|
||||
Text('Delete'),
|
||||
]),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: ElevatedButton(
|
||||
onPressed: (() => VoidCallback),
|
||||
child: Row(children: const <Widget>[
|
||||
Icon(Icons.sync_alt),
|
||||
Text('enable/disable'),
|
||||
]),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/* class IconPicker extends SimpleDialog {
|
||||
IconPicker({super.key})
|
||||
|
||||
@override
|
||||
Widget build (BuildContext context) {
|
||||
return SimpleDialog(
|
||||
title: const Text("Pick an icon"),
|
||||
|
||||
);
|
||||
}
|
||||
} */
|
||||
43
lib/models/car.dart
Normal file
43
lib/models/car.dart
Normal file
@@ -0,0 +1,43 @@
|
||||
class Car {
|
||||
static const tblCars = 'cars';
|
||||
static const colId = 'id';
|
||||
static const colVin = 'vin';
|
||||
static const colNickname = 'nickname';
|
||||
static const colPlate = 'plate';
|
||||
static const colMileage = 'mileage';
|
||||
|
||||
Car({this.id, this.vin, this.plate, this.nickname, this.mileage});
|
||||
|
||||
Car.fromMap(Map<String, dynamic> map) {
|
||||
id = map[colId];
|
||||
vin = map[colVin];
|
||||
nickname = map[colNickname];
|
||||
plate = map[colPlate];
|
||||
mileage = map[colMileage];
|
||||
}
|
||||
|
||||
int? id;
|
||||
String? vin;
|
||||
String? plate;
|
||||
String? nickname;
|
||||
int? mileage;
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
var map = <String, dynamic>{
|
||||
colVin: vin,
|
||||
colPlate: plate,
|
||||
colNickname: nickname,
|
||||
colMileage: mileage,
|
||||
};
|
||||
// if (id != null) {
|
||||
// map[colId] = id; //not sure https://www.youtube.com/watch?v=tj7Lj9a3fyM
|
||||
// }
|
||||
return map;
|
||||
}
|
||||
|
||||
/* <String, dynamic> toString() {
|
||||
const output = """nickname: $colNickname, id: $colId, vin: $colVin,
|
||||
plate: $colPlate, mileage: ($colMileage.runtimeType) $colMileage """;
|
||||
return output;
|
||||
} */
|
||||
}
|
||||
49
lib/models/txn.dart
Normal file
49
lib/models/txn.dart
Normal file
@@ -0,0 +1,49 @@
|
||||
class Txn {
|
||||
static const tblTxns = 'txns';
|
||||
static const colId = 'id';
|
||||
static const colType = 'txntype';
|
||||
static const colDatetime = 'datetime';
|
||||
static const colCost = 'cost';
|
||||
static const colMileage = 'mileage';
|
||||
static const colNote = 'note';
|
||||
static const colCarId = 'carid';
|
||||
|
||||
Txn(
|
||||
{this.id,
|
||||
this.txntype,
|
||||
this.datetime,
|
||||
this.cost,
|
||||
this.mileage,
|
||||
this.note,
|
||||
this.carid});
|
||||
|
||||
Txn.fromMap(Map<String, dynamic> map) {
|
||||
id = map[colId];
|
||||
txntype = map[colType];
|
||||
datetime = map[colDatetime];
|
||||
cost = map[colCost];
|
||||
mileage = map[colMileage];
|
||||
note = map[colNote];
|
||||
carid = map[colCarId];
|
||||
}
|
||||
|
||||
int? id;
|
||||
String? txntype;
|
||||
int? datetime; // store as integer unix time
|
||||
double? cost;
|
||||
int? mileage;
|
||||
String? note;
|
||||
int? carid;
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
var map = <String, dynamic>{
|
||||
colType: txntype,
|
||||
colDatetime: datetime,
|
||||
colCost: cost,
|
||||
colMileage: mileage,
|
||||
colNote: note,
|
||||
colCarId: carid,
|
||||
};
|
||||
return map;
|
||||
}
|
||||
}
|
||||
120
lib/utils/dbhelper_sqflite.dart
Normal file
120
lib/utils/dbhelper_sqflite.dart
Normal file
@@ -0,0 +1,120 @@
|
||||
import 'dart:io';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:dash/models/car.dart';
|
||||
import 'package:dash/models/txn.dart';
|
||||
|
||||
//https://suragch.medium.com/simple-sqflite-database-example-in-flutter-e56a5aaa3f91
|
||||
// class dbWrapper {
|
||||
// if (Platform.isWindows or Platform.isLinux or ) {
|
||||
// print('build does not support sqlite')
|
||||
// }
|
||||
// }
|
||||
|
||||
class DbHelperSqlite {
|
||||
final _dbName = "cars_sqlite.db";
|
||||
final _dbVersion = 1;
|
||||
|
||||
// make singleton class
|
||||
DbHelperSqlite._privateConstructor();
|
||||
static final DbHelperSqlite instance = DbHelperSqlite._privateConstructor();
|
||||
static Database? _database;
|
||||
|
||||
Future<Database?> get database async {
|
||||
_database = await _initDatabase();
|
||||
return _database!;
|
||||
}
|
||||
|
||||
// open db and create if not exists
|
||||
_initDatabase() async {
|
||||
print('initializing db');
|
||||
Directory documentsDirectory = await getApplicationDocumentsDirectory();
|
||||
String dbPath = join(documentsDirectory.path, _dbName);
|
||||
return await openDatabase(dbPath, version: _dbVersion, onCreate: _onCreate);
|
||||
}
|
||||
|
||||
Future _onCreate(Database db, int version) async {
|
||||
//docs say to avoid autoincrement
|
||||
//looks like this may be a null issue?
|
||||
await db.execute('''
|
||||
CREATE TABLE ${Car.tblCars} (
|
||||
${Car.colId} INTEGER PRIMARY KEY,
|
||||
${Car.colVin} TEXT UNIQUE,
|
||||
${Car.colNickname} TEXT,
|
||||
${Car.colMileage} INTEGER,
|
||||
${Car.colPlate} TEXT);
|
||||
CREATE TABLE ${Txn.tblTxns} (
|
||||
${Txn.colId} INTEGER PRIMARY KEY,
|
||||
${Txn.colType} TEXT,
|
||||
${Txn.colDatetime} INTEGER,
|
||||
${Txn.colCost} REAL,
|
||||
${Txn.colMileage} INTEGER,
|
||||
${Txn.colNote} TEXT,
|
||||
FOREIGN KEY(${Txn.colCarId}) REFERENCES ${Car.tblCars}(${Car.colId}),
|
||||
)''');
|
||||
}
|
||||
|
||||
Future<int> insertCar(Car car) async {
|
||||
Database db = await instance.database as Database;
|
||||
return await db.insert(Car.tblCars, car.toMap(),
|
||||
conflictAlgorithm: ConflictAlgorithm.abort);
|
||||
}
|
||||
|
||||
Future<List<Car>> fetchCars() async {
|
||||
Database db = await database as Database;
|
||||
List<Map<String, dynamic>> cars = await db.query(Car.tblCars);
|
||||
if (cars.isEmpty) {
|
||||
return [];
|
||||
} else {
|
||||
return cars.map((car) => Car.fromMap(car)).toList();
|
||||
}
|
||||
}
|
||||
|
||||
Future<int> updateCar(Car car) async {
|
||||
Database db = await instance.database as Database;
|
||||
print('car id to update: ${car.id}');
|
||||
return await db.update(Car.tblCars, car.toMap(),
|
||||
where: '${Car.colId}=?', whereArgs: [car.id]);
|
||||
}
|
||||
|
||||
Future<int> deleteCar(Car car) async {
|
||||
Database db = await instance.database as Database;
|
||||
print('car id to delete: ${car.id}');
|
||||
return await db
|
||||
.delete(Car.tblCars, where: '${Car.colId}=?', whereArgs: [car.id]);
|
||||
}
|
||||
|
||||
Future<int> deleteAll() async {
|
||||
Database db = await instance.database as Database;
|
||||
return await db.rawDelete("DELETE FROM ${Car.tblCars}");
|
||||
}
|
||||
}
|
||||
|
||||
// flutter ex: https://docs.flutter.dev/cookbook/persistence/sqlite
|
||||
/* Future<void> insertCar(Car car) async {
|
||||
final db = await database;
|
||||
await db.insert(
|
||||
'cars',
|
||||
car.toMap(),
|
||||
conflictAlgorithm: ConflictAlgorithm.abort,
|
||||
);
|
||||
} */
|
||||
|
||||
/* Future<void> insertTestCar(Car car) async {
|
||||
final db = await database;
|
||||
await db.insert('cars', car.toMap(),
|
||||
conflictAlgorithm: ConflictAlgorithm.abort);
|
||||
}
|
||||
|
||||
|
||||
Future<int> deleteCar(String vin) async {
|
||||
Database db = await instance.database;
|
||||
return await db.delete(tableCars, where: '$colVin=?', whereArgs: [vin]);
|
||||
}
|
||||
|
||||
Future<List<Map<String, dynamic>>> queryAllCars() async {
|
||||
Database db = await instance.database;
|
||||
return await db.query(tableCars);
|
||||
}
|
||||
} */
|
||||
Reference in New Issue
Block a user