|
1 | 1 | import 'package:flutter/material.dart'; |
| 2 | +import 'package:supabase_auth_ui/supabase_auth_ui.dart'; |
| 3 | + |
2 | 4 | import 'util.dart'; |
3 | 5 | import 'theme.dart'; |
4 | 6 |
|
5 | | -void main() { |
| 7 | +void main() async { |
| 8 | + await Supabase.initialize( |
| 9 | + url: 'https://jbpeszoljhnymgqhufmd.supabase.co', |
| 10 | + anonKey: |
| 11 | + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImpicGVzem9samhueW1ncWh1Zm1kIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDMyNTAzMDIsImV4cCI6MjA1ODgyNjMwMn0.NX3h_DpXqnpabbla3wR-RHQTuZskM3aJeX2qU1HNUe0', |
| 12 | + ); |
6 | 13 | runApp(const MyApp()); |
7 | 14 | } |
8 | 15 |
|
@@ -51,70 +58,115 @@ class MyHomePage extends StatefulWidget { |
51 | 58 | State<MyHomePage> createState() => _MyHomePageState(); |
52 | 59 | } |
53 | 60 |
|
54 | | -class _MyHomePageState extends State<MyHomePage> { |
55 | | - int _counter = 0; |
56 | | - |
57 | | - void _incrementCounter() { |
58 | | - setState(() { |
59 | | - // This call to setState tells the Flutter framework that something has |
60 | | - // changed in this State, which causes it to rerun the build method below |
61 | | - // so that the display can reflect the updated values. If we changed |
62 | | - // _counter without calling setState(), then the build method would not be |
63 | | - // called again, and so nothing would appear to happen. |
64 | | - _counter++; |
65 | | - }); |
| 61 | +class _MyHomePageState extends State<MyHomePage> |
| 62 | + with SingleTickerProviderStateMixin { |
| 63 | + @override |
| 64 | + Widget build(BuildContext context) { |
| 65 | + return Scaffold( |
| 66 | + appBar: AppBar( |
| 67 | + backgroundColor: Theme.of(context).colorScheme.inversePrimary, |
| 68 | + title: Text(widget.title), |
| 69 | + ), |
| 70 | + body: Center( |
| 71 | + child: Column( |
| 72 | + mainAxisAlignment: MainAxisAlignment.center, |
| 73 | + children: [ |
| 74 | + SupaEmailAuth( |
| 75 | + onSignInComplete: (response) { |
| 76 | + final session = response.session; |
| 77 | + if (session != null && session.user.email != null) { |
| 78 | + Navigator.of(context).pushReplacement( |
| 79 | + MaterialPageRoute( |
| 80 | + builder: |
| 81 | + (context) => |
| 82 | + WelcomeScreen(email: session.user.email!), |
| 83 | + ), |
| 84 | + ); |
| 85 | + } |
| 86 | + }, |
| 87 | + onSignUpComplete: (response) { |
| 88 | + final session = response.session; |
| 89 | + if (session != null && session.user.email != null) { |
| 90 | + Navigator.of(context).pushReplacement( |
| 91 | + MaterialPageRoute( |
| 92 | + builder: |
| 93 | + (context) => |
| 94 | + WelcomeScreen(email: session.user.email!), |
| 95 | + ), |
| 96 | + ); |
| 97 | + } |
| 98 | + }, |
| 99 | + ), |
| 100 | + SupaSocialsAuth( |
| 101 | + socialProviders: [OAuthProvider.google], |
| 102 | + colored: true, |
| 103 | + onSuccess: (session) { |
| 104 | + if (session != null && session.user.email != null) { |
| 105 | + Navigator.of(context).pushReplacement( |
| 106 | + MaterialPageRoute( |
| 107 | + builder: |
| 108 | + (context) => |
| 109 | + WelcomeScreen(email: session.user.email!), |
| 110 | + ), |
| 111 | + ); |
| 112 | + } |
| 113 | + }, |
| 114 | + ), |
| 115 | + ], |
| 116 | + ), |
| 117 | + ), |
| 118 | + ); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// Add this class at the end of your file |
| 123 | + |
| 124 | +class WelcomeScreen extends StatelessWidget { |
| 125 | + final String email; |
| 126 | + |
| 127 | + const WelcomeScreen({super.key, required this.email}); |
| 128 | + |
| 129 | + // Extract username from email (everything before @) |
| 130 | + String get username { |
| 131 | + return email.split('@')[0]; |
66 | 132 | } |
67 | 133 |
|
68 | 134 | @override |
69 | 135 | Widget build(BuildContext context) { |
70 | | - // This method is rerun every time setState is called, for instance as done |
71 | | - // by the _incrementCounter method above. |
72 | | - // |
73 | | - // The Flutter framework has been optimized to make rerunning build methods |
74 | | - // fast, so that you can just rebuild anything that needs updating rather |
75 | | - // than having to individually change instances of widgets. |
76 | 136 | return Scaffold( |
77 | 137 | appBar: AppBar( |
78 | | - // TRY THIS: Try changing the color here to a specific color (to |
79 | | - // Colors.amber, perhaps?) and trigger a hot reload to see the AppBar |
80 | | - // change color while the other colors stay the same. |
81 | 138 | backgroundColor: Theme.of(context).colorScheme.inversePrimary, |
82 | | - // Here we take the value from the MyHomePage object that was created by |
83 | | - // the App.build method, and use it to set our appbar title. |
84 | | - title: Text(widget.title), |
| 139 | + title: const Text('Welcome'), |
| 140 | + actions: [ |
| 141 | + IconButton( |
| 142 | + icon: const Icon(Icons.logout), |
| 143 | + onPressed: () async { |
| 144 | + await Supabase.instance.client.auth.signOut(); |
| 145 | + if (context.mounted) { |
| 146 | + Navigator.of(context).pushAndRemoveUntil( |
| 147 | + MaterialPageRoute( |
| 148 | + builder: |
| 149 | + (context) => |
| 150 | + const MyHomePage(title: 'Flutter Demo Home Page'), |
| 151 | + ), |
| 152 | + (route) => false, |
| 153 | + ); |
| 154 | + } |
| 155 | + }, |
| 156 | + ), |
| 157 | + ], |
85 | 158 | ), |
86 | 159 | body: Center( |
87 | | - // Center is a layout widget. It takes a single child and positions it |
88 | | - // in the middle of the parent. |
89 | 160 | child: Column( |
90 | | - // Column is also a layout widget. It takes a list of children and |
91 | | - // arranges them vertically. By default, it sizes itself to fit its |
92 | | - // children horizontally, and tries to be as tall as its parent. |
93 | | - // |
94 | | - // Column has various properties to control how it sizes itself and |
95 | | - // how it positions its children. Here we use mainAxisAlignment to |
96 | | - // center the children vertically; the main axis here is the vertical |
97 | | - // axis because Columns are vertical (the cross axis would be |
98 | | - // horizontal). |
99 | | - // |
100 | | - // TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint" |
101 | | - // action in the IDE, or press "p" in the console), to see the |
102 | | - // wireframe for each widget. |
103 | 161 | mainAxisAlignment: MainAxisAlignment.center, |
104 | | - children: <Widget>[ |
105 | | - const Text('You have pushed the button this many times:'), |
| 162 | + children: [ |
106 | 163 | Text( |
107 | | - '$_counter', |
| 164 | + 'π Hello ${username}', |
108 | 165 | style: Theme.of(context).textTheme.headlineMedium, |
109 | 166 | ), |
110 | 167 | ], |
111 | 168 | ), |
112 | 169 | ), |
113 | | - floatingActionButton: FloatingActionButton( |
114 | | - onPressed: _incrementCounter, |
115 | | - tooltip: 'Increment', |
116 | | - child: const Icon(Icons.add), |
117 | | - ), // This trailing comma makes auto-formatting nicer for build methods. |
118 | 170 | ); |
119 | 171 | } |
120 | 172 | } |
0 commit comments