SHARE
TWEET

Untitled

a guest Mar 24th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'package:flutter/material.dart';
  2. import 'dart:async';
  3. import 'package:flutter/services.dart';
  4. import 'package:barcode_scan/barcode_scan.dart';
  5. import 'package:path/path.dart';
  6.  
  7. import 'database/database.dart';
  8. import 'package:sqflite/sqflite.dart';
  9.  
  10. import 'variables/colors.dart';
  11. import 'package:ticketscanner/card.dart';
  12.  
  13.  
  14. class TicketDatabase{
  15.   static Database _database;
  16.  
  17.   Future<Database> get database async {
  18.     print("Getting Database");
  19.     if (_database != null) {
  20.       return _database;
  21.     }
  22.     _database = await initDatabase();
  23.     return _database;
  24.   }
  25.  
  26.   Future<Database> initDatabase() async {
  27.     print("Initiallizing Database");
  28.     var databasesPath = await getDatabasesPath();
  29.     var path = join(databasesPath, "database.db");
  30.     final Database db = await openDatabase(path, readOnly: false,).catchError((db) async {
  31.       print("OpenDatabase");
  32.       db = await openDatabase(path, version: 1,
  33.       onCreate: (Database db, int version) async {
  34.         print("OnCreate");
  35.         await db.execute(
  36.           'CREATE TABLE Tickets (id INTEGER PRIMARY KEY, startId INTEGER, endId INTEGER, hash TEXT, type TEXT, name TEXT, checked INTEGER)');
  37.         await db.transaction((txn) async {
  38.         print("OnInsert");
  39.           await txn.rawInsert('INSERT INTO Tickets(startId, endId, hash, type, name, checked) VALUES(119001, 119008, "none", "Standard", "Christine Möller", 0)');
  40.           await txn.rawInsert('INSERT INTO Tickets(startId, endId, hash, type, name, checked) VALUES(119009, 119009, "none", "Schüler", "Jule Brendemühl", 0)');
  41.           await txn.rawInsert('INSERT INTO Tickets(startId, endId, hash, type, name, checked) VALUES(119016, 119016, "Fah:*/0=TW", "Schüler", "Emma", 0)');
  42.         });
  43.       });
  44.     });
  45.     print(path);
  46.     print(db);
  47.     return db;
  48.   }
  49.  
  50. }
  51.  
  52. //Checks, if ticket is valid
  53.  
  54. Future checkValidity(String hash) async {
  55.   final db = await TicketDatabase().database;
  56.  
  57.   List<Map> list = await db.rawQuery('SELECT startId, endId, checked FROM Tickets WHERE hash=$hash');
  58.   if (list.length > 1) {
  59.     print("Mehr als 1x den Hash gefunden");
  60.   }
  61.   else{
  62.     Map _ticket = list[0];
  63.     int _ticketsAvailable = _ticket['endId'] - _ticket['startId'] + 1;
  64.     if (_ticketsAvailable - _ticket['checked'] > 0) {
  65.       //*Ticket is Valid
  66.       //TODO: Perform some task to show that its valid
  67.       updateData(hash, _ticket['checked']);
  68.     }
  69.     else{
  70.       //*Ticket is NOT VALID
  71.       //TODO: Perform some task to show that its not valid
  72.     }
  73.   }
  74. }
  75.  
  76. Future updateData(String hash, int checked) async{
  77.   final db = await TicketDatabase().database;
  78.   int _checked = checked++;
  79.   db.rawUpdate('UPDATE Tickets SET checked = ? WHERE hash = $hash',['$_checked']);
  80.   print("Ticket $hash updated");
  81. }
  82.  
  83. Future<List<Widget>> buildList() async {
  84.   final db = await TicketDatabase().database;
  85.   List<Map<String, dynamic>> records = await db.query('Tickets');
  86.   List<Widget> widgets;
  87.  
  88.   for (var item in records) {
  89.     widgets.insert(0, ticket(item['name'], item['checked'], item['startId'], item['endId']));
  90.   }
  91.   return widgets;
  92. }
  93.  
  94.  
  95. void main() {
  96.   runApp(MyApp());
  97. }
  98.  
  99. class MyApp extends StatelessWidget {
  100.   // This widget is the root of your application.
  101.   @override
  102.   Widget build(BuildContext context) {
  103.     return MaterialApp(
  104.       title: '80s Blade Ticket Scanner',
  105.       theme: ThemeData(
  106.         primarySwatch: Colors.blue,
  107.       ),
  108.       home: Home(),
  109.     );
  110.   }
  111. }
  112.  
  113. class Home extends StatefulWidget {
  114.   @override
  115.   State<StatefulWidget> createState() {
  116.     return _Home();
  117.   }
  118. }
  119.  
  120. class _Home extends State<Home>{
  121.  
  122.   String barcode = "Noch kein Code gescannt";
  123.  
  124.   Future barcodeScanning() async {
  125.     try {
  126.       String scan = await BarcodeScanner.scan();
  127.       setState(() => barcode = scan);
  128.     } on PlatformException catch (e) {
  129.       if (e.code == BarcodeScanner.CameraAccessDenied) {
  130.         setState(() {
  131.           barcode = 'No camera permission!';
  132.         });
  133.       } else {
  134.         setState(() => barcode = 'Unknown error: $e');
  135.       }
  136.     } on FormatException {
  137.       setState(() => barcode =
  138.           'Nothing captured.');
  139.     } catch (e) {
  140.       setState(() => barcode = 'Unknown error: $e');
  141.     }
  142.   }
  143.  
  144.  
  145.   @override
  146.   Widget build(BuildContext context) {
  147.    
  148.  
  149.     return Scaffold(
  150.       backgroundColor: Colors.black,
  151.       appBar: AppBar(
  152.             elevation: 5.0,
  153.             primary: true,
  154.             backgroundColor: primaryGrey,
  155.             title: Text("80s Blade Ticket Scanner", style: TextStyle(color:primaryColor),)
  156.         ),
  157.       body: Container(
  158.         padding: const EdgeInsets.all(20.0),
  159.         decoration: new BoxDecoration(
  160.         ),
  161.         child: Column(
  162.           children: <Widget>[
  163.             Row(
  164.               mainAxisAlignment: MainAxisAlignment.center,
  165.               children: <Widget>[
  166.                 Text("12", style: TextStyle(color: Colors.white, fontSize: 50.0),),
  167.                 Text(" von ", style: TextStyle(color: Colors.white),),
  168.                 Text("87", style: TextStyle(color: Colors.white, fontSize: 50.0),)
  169.               ],
  170.             ),
  171.             Text(barcode, style: TextStyle(color: Colors.white),),
  172.             FutureBuilder(
  173.               future: buildList(),
  174.               builder: (BuildContext context, AsyncSnapshot snapshot) {
  175.                 switch (snapshot.connectionState) {
  176.                   case ConnectionState.none:
  177.                   case ConnectionState.waiting:
  178.                     return new Text('loading...');
  179.                   default:
  180.                     if (snapshot.hasError)
  181.                       return new Text('Error: ${snapshot.error}');
  182.                     else
  183.                       return createListView(context, snapshot);
  184.                 }
  185.               },
  186.             ),
  187.           ],
  188.         )
  189.       ),
  190.       floatingActionButton: new FloatingActionButton(
  191.         //onPressed: barcodeScanning,
  192.         onPressed: (){setState(() {
  193.          
  194.         });},
  195.         backgroundColor: primaryColor,
  196.         foregroundColor: Colors.black,
  197.         child: Icon(Icons.camera_enhance),
  198.       ),
  199.       floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
  200.     );
  201.   }
  202. }
  203.  
  204.  
  205. Widget createListView(BuildContext context, AsyncSnapshot snapshot) {
  206.     List<Widget> values = snapshot.data;
  207.     return new ListView.builder(
  208.         itemCount: values.length,
  209.         padding: EdgeInsets.all(20.0),
  210.         itemBuilder: (BuildContext context, int index) {
  211.           return new Column(
  212.             children: <Widget>[
  213.               values[index]
  214.             ],
  215.           );
  216.         },
  217.     );
  218.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top