|
| 1 | +import 'package:flutter/foundation.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:logging/logging.dart'; |
| 4 | +import 'package:powersync_flutter_demo/models/schema.dart'; |
| 5 | + |
| 6 | +import './powersync.dart'; |
| 7 | +import './widgets/lists_page.dart'; |
| 8 | +import './widgets/login_page.dart'; |
| 9 | +import './widgets/query_widget.dart'; |
| 10 | +import './widgets/status_app_bar.dart'; |
| 11 | + |
| 12 | + |
| 13 | +void main() async { |
| 14 | + Logger.root.level = Level.INFO; |
| 15 | + Logger.root.onRecord.listen((record) { |
| 16 | + if (kDebugMode) { |
| 17 | + print( |
| 18 | + '[${record.loggerName}] ${record.level.name}: ${record.time}: ${record.message}'); |
| 19 | + |
| 20 | + if (record.error != null) { |
| 21 | + print(record.error); |
| 22 | + } |
| 23 | + if (record.stackTrace != null) { |
| 24 | + print(record.stackTrace); |
| 25 | + } |
| 26 | + } |
| 27 | + }); |
| 28 | + |
| 29 | + WidgetsFlutterBinding |
| 30 | + .ensureInitialized(); //required to get sqlite filepath from path_provider before UI has initialized |
| 31 | + |
| 32 | + await openDatabase(); |
| 33 | + |
| 34 | + final loggedIn = await isLoggedIn(); |
| 35 | + |
| 36 | + runApp(MyApp(loggedIn: loggedIn)); |
| 37 | +} |
| 38 | + |
| 39 | +const defaultQuery = 'SELECT * from $todosTable'; |
| 40 | + |
| 41 | +const listsPage = ListsPage(); |
| 42 | +const homePage = listsPage; |
| 43 | + |
| 44 | +const sqlConsolePage = Scaffold( |
| 45 | + appBar: StatusAppBar(title: 'SQL Console'), |
| 46 | + body: QueryWidget(defaultQuery: defaultQuery)); |
| 47 | + |
| 48 | +const loginPage = LoginPage(); |
| 49 | + |
| 50 | +class MyApp extends StatelessWidget { |
| 51 | + final bool loggedIn; |
| 52 | + |
| 53 | + const MyApp({super.key, required this.loggedIn}); |
| 54 | + |
| 55 | + @override |
| 56 | + Widget build(BuildContext context) { |
| 57 | + return MaterialApp( |
| 58 | + title: 'PowerSync Django Todolist Demo', |
| 59 | + theme: ThemeData( |
| 60 | + primarySwatch: Colors.blue, |
| 61 | + ), |
| 62 | + home: loggedIn ? homePage : loginPage); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +class MyHomePage extends StatelessWidget { |
| 67 | + const MyHomePage( |
| 68 | + {super.key, |
| 69 | + required this.title, |
| 70 | + required this.content, |
| 71 | + this.floatingActionButton}); |
| 72 | + |
| 73 | + final String title; |
| 74 | + final Widget content; |
| 75 | + final Widget? floatingActionButton; |
| 76 | + |
| 77 | + @override |
| 78 | + Widget build(BuildContext context) { |
| 79 | + return Scaffold( |
| 80 | + appBar: StatusAppBar(title: title), |
| 81 | + body: Center(child: content), |
| 82 | + floatingActionButton: floatingActionButton, |
| 83 | + drawer: Drawer( |
| 84 | + // Add a ListView to the drawer. This ensures the user can scroll |
| 85 | + // through the options in the drawer if there isn't enough vertical |
| 86 | + // space to fit everything. |
| 87 | + child: ListView( |
| 88 | + // Important: Remove any padding from the ListView. |
| 89 | + padding: EdgeInsets.zero, |
| 90 | + children: [ |
| 91 | + const DrawerHeader( |
| 92 | + decoration: BoxDecoration( |
| 93 | + color: Colors.blue, |
| 94 | + ), |
| 95 | + child: Text(''), |
| 96 | + ), |
| 97 | + ListTile( |
| 98 | + title: const Text('SQL Console'), |
| 99 | + onTap: () { |
| 100 | + var navigator = Navigator.of(context); |
| 101 | + navigator.pop(); |
| 102 | + |
| 103 | + navigator.push(MaterialPageRoute( |
| 104 | + builder: (context) => sqlConsolePage, |
| 105 | + )); |
| 106 | + }, |
| 107 | + ), |
| 108 | + ListTile( |
| 109 | + title: const Text('Sign Out'), |
| 110 | + onTap: () async { |
| 111 | + var navigator = Navigator.of(context); |
| 112 | + navigator.pop(); |
| 113 | + await logout(); |
| 114 | + |
| 115 | + navigator.pushReplacement(MaterialPageRoute( |
| 116 | + builder: (context) => loginPage, |
| 117 | + )); |
| 118 | + }, |
| 119 | + ), |
| 120 | + ], |
| 121 | + ), |
| 122 | + ), |
| 123 | + ); |
| 124 | + } |
| 125 | +} |
0 commit comments