From cb9237859b71160ccaf41273c977e8fed9dbb9e0 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Mon, 30 Nov 2026 11:59:38 +0100 Subject: [PATCH 1/2] chore(supabase_flutter): drop the deprecated Material import from the package examples --- README.md | 4 +- packages/supabase_flutter/README.md | 6 +- .../supabase_flutter/example/lib/main.dart | 246 ++++++++++++------ .../supabase_flutter/example/pubspec.yaml | 3 - packages/supabase_flutter_web_auth/README.md | 4 +- .../example/lib/main.dart | 129 +++++---- .../example/pubspec.yaml | 3 - 7 files changed, 254 insertions(+), 141 deletions(-) diff --git a/README.md b/README.md index 365afb796..0e655e0c7 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ Initialize Supabase once, then query through the generated types, imported from `supabase_schema.g.dart`: ```dart file -import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; import 'supabase_schema.g.dart'; @@ -106,7 +106,7 @@ Future main() async { url: 'https://your-project.supabase.co', publishableKey: 'your-publishable-key', ); - runApp(const MaterialApp(home: Placeholder())); + runApp(const Placeholder()); } Future> booksInPrint() { diff --git a/packages/supabase_flutter/README.md b/packages/supabase_flutter/README.md index f1790c95f..81f52bdd1 100644 --- a/packages/supabase_flutter/README.md +++ b/packages/supabase_flutter/README.md @@ -82,7 +82,7 @@ session before it returns. Find the URL and the publishable key of your project in the dashboard under **Project Settings > API Keys**. ```dart file -import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; Future main() async { @@ -91,7 +91,7 @@ Future main() async { url: 'https://your-project.supabase.co', publishableKey: 'your-publishable-key', ); - runApp(const MaterialApp(home: Placeholder())); + runApp(const Placeholder()); } final supabase = Supabase.instance.client; @@ -641,7 +641,7 @@ Create the stream once, in a `State` or a controller. A stream created inside connection. ```dart file -import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; import 'package:my_app/supabase_schema.g.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; diff --git a/packages/supabase_flutter/example/lib/main.dart b/packages/supabase_flutter/example/lib/main.dart index fcb5360df..1c219059d 100644 --- a/packages/supabase_flutter/example/lib/main.dart +++ b/packages/supabase_flutter/example/lib/main.dart @@ -1,11 +1,14 @@ import 'dart:async'; -// ignore: deprecated_member_use -import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; import 'supabase_schema.g.dart'; +const _accentColor = Color(0xFF3ECF8E); +const _errorColor = Color(0xFFD32F2F); +const _textColor = Color(0xFF1C1C1C); + Future main() async { await Supabase.initialize( url: 'SUPABASE_URL', @@ -19,9 +22,11 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { - return const MaterialApp( + return WidgetsApp( title: 'Supabase Flutter Demo', - home: MyWidget(), + color: _accentColor, + textStyle: const TextStyle(color: _textColor, fontSize: 16), + builder: (context, child) => const MyWidget(), ); } } @@ -63,11 +68,28 @@ class _MyWidgetState extends State { @override Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: const Text('Profile Example'), + return ColoredBox( + color: const Color(0xFFFFFFFF), + child: SafeArea( + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + const ColoredBox( + color: _accentColor, + child: Padding( + padding: EdgeInsets.all(16), + child: Text( + 'Profile Example', + style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold), + ), + ), + ), + Expanded( + child: _user == null ? const _LoginForm() : const _ProfileForm(), + ), + ], + ), ), - body: _user == null ? const _LoginForm() : const _ProfileForm(), ); } } @@ -81,6 +103,7 @@ class _LoginForm extends StatefulWidget { class _LoginFormState extends State<_LoginForm> { bool _loading = false; + String? _error; final _emailController = TextEditingController(); final _passwordController = TextEditingController(); @@ -94,23 +117,18 @@ class _LoginFormState extends State<_LoginForm> { Future _signIn() async { setState(() { _loading = true; + _error = null; }); - final scaffoldMessenger = ScaffoldMessenger.of(context); try { await Supabase.instance.client.auth.signInWithPassword( email: _emailController.text, password: _passwordController.text, ); } catch (_) { - scaffoldMessenger.showSnackBar( - const SnackBar( - content: Text('Login failed'), - backgroundColor: Colors.red, - ), - ); if (mounted) { setState(() { _loading = false; + _error = 'Login failed'; }); } } @@ -119,23 +137,18 @@ class _LoginFormState extends State<_LoginForm> { Future _signUp() async { setState(() { _loading = true; + _error = null; }); - final scaffoldMessenger = ScaffoldMessenger.of(context); try { await Supabase.instance.client.auth.signUp( email: _emailController.text, password: _passwordController.text, ); } catch (_) { - scaffoldMessenger.showSnackBar( - const SnackBar( - content: Text('Signup failed'), - backgroundColor: Colors.red, - ), - ); if (mounted) { setState(() { _loading = false; + _error = 'Signup failed'; }); } } @@ -143,32 +156,34 @@ class _LoginFormState extends State<_LoginForm> { @override Widget build(BuildContext context) { + final error = _error; return _loading - ? const Center(child: CircularProgressIndicator()) + ? const Center(child: Text('Loading')) : ListView( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 20), children: [ - TextFormField( + _TextInput( + label: 'Email', keyboardType: TextInputType.emailAddress, controller: _emailController, - decoration: const InputDecoration(label: Text('Email')), ), const SizedBox(height: 16), - TextFormField( + _TextInput( + label: 'Password', obscureText: true, controller: _passwordController, - decoration: const InputDecoration(label: Text('Password')), ), const SizedBox(height: 16), - ElevatedButton( + _Button( + label: 'Login', onPressed: () => unawaited(_signIn()), - child: const Text('Login'), ), const SizedBox(height: 16), - TextButton( + _Button( + label: 'Signup', onPressed: () => unawaited(_signUp()), - child: const Text('Signup'), ), + if (error != null) _Message(error, isError: true), ], ); } @@ -183,6 +198,8 @@ class _ProfileForm extends StatefulWidget { class _ProfileFormState extends State<_ProfileForm> { var _loading = true; + String? _message; + var _messageIsError = false; final _usernameController = TextEditingController(); final _websiteController = TextEditingController(); @@ -199,8 +216,15 @@ class _ProfileFormState extends State<_ProfileForm> { super.dispose(); } + void _showMessage(String message, {bool isError = false}) { + if (!mounted) return; + setState(() { + _message = message; + _messageIsError = isError; + }); + } + Future _loadProfile() async { - final scaffoldMessenger = ScaffoldMessenger.of(context); try { final userId = Supabase.instance.client.auth.currentUser!.id; final profile = await Supabase.instance.client @@ -215,12 +239,7 @@ class _ProfileFormState extends State<_ProfileForm> { }); } } catch (_) { - scaffoldMessenger.showSnackBar( - const SnackBar( - content: Text('Error occurred while getting profile'), - backgroundColor: Colors.red, - ), - ); + _showMessage('Error occurred while getting profile', isError: true); } if (mounted) { setState(() { @@ -230,15 +249,12 @@ class _ProfileFormState extends State<_ProfileForm> { } Future _signOut() async { - final scaffoldMessenger = ScaffoldMessenger.of(context); try { await Supabase.instance.client.auth.signOut(); } on AuthException { - scaffoldMessenger.showSnackBar( - const SnackBar( - content: Text('Signed out, but the session was not revoked'), - backgroundColor: Colors.red, - ), + _showMessage( + 'Signed out, but the session was not revoked', + isError: true, ); } } @@ -246,8 +262,8 @@ class _ProfileFormState extends State<_ProfileForm> { Future _saveProfile() async { setState(() { _loading = true; + _message = null; }); - final scaffoldMessenger = ScaffoldMessenger.of(context); try { final userId = Supabase.instance.client.auth.currentUser!.id; await Supabase.instance.client @@ -260,18 +276,9 @@ class _ProfileFormState extends State<_ProfileForm> { updatedAt: DateTime.now(), ), ); - scaffoldMessenger.showSnackBar( - const SnackBar( - content: Text('Saved profile'), - ), - ); + _showMessage('Saved profile'); } catch (_) { - scaffoldMessenger.showSnackBar( - const SnackBar( - content: Text('Error saving profile'), - backgroundColor: Colors.red, - ), - ); + _showMessage('Error saving profile', isError: true); } if (mounted) { setState(() { @@ -282,35 +289,128 @@ class _ProfileFormState extends State<_ProfileForm> { @override Widget build(BuildContext context) { + final message = _message; return _loading - ? const Center(child: CircularProgressIndicator()) + ? const Center(child: Text('Loading')) : ListView( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 20), children: [ - TextFormField( - controller: _usernameController, - decoration: const InputDecoration( - label: Text('Username'), - ), - ), + _TextInput(label: 'Username', controller: _usernameController), const SizedBox(height: 16), - TextFormField( - controller: _websiteController, - decoration: const InputDecoration( - label: Text('Website'), - ), - ), + _TextInput(label: 'Website', controller: _websiteController), const SizedBox(height: 16), - ElevatedButton( + _Button( + label: 'Save', onPressed: () => unawaited(_saveProfile()), - child: const Text('Save'), ), const SizedBox(height: 16), - TextButton( + _Button( + label: 'Sign Out', onPressed: () => unawaited(_signOut()), - child: const Text('Sign Out'), ), + if (message != null) _Message(message, isError: _messageIsError), ], ); } } + +class _TextInput extends StatefulWidget { + const _TextInput({ + required this.label, + required this.controller, + this.keyboardType, + this.obscureText = false, + }); + + final String label; + final TextEditingController controller; + final TextInputType? keyboardType; + final bool obscureText; + + @override + State<_TextInput> createState() => _TextInputState(); +} + +class _TextInputState extends State<_TextInput> { + final _focusNode = FocusNode(); + + @override + void dispose() { + _focusNode.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + spacing: 4, + children: [ + Text(widget.label), + DecoratedBox( + decoration: BoxDecoration( + border: Border.all(color: const Color(0xFF9E9E9E)), + borderRadius: BorderRadius.circular(4), + ), + child: Padding( + padding: const EdgeInsets.all(8), + child: EditableText( + controller: widget.controller, + focusNode: _focusNode, + keyboardType: widget.keyboardType, + obscureText: widget.obscureText, + style: const TextStyle(color: _textColor, fontSize: 16), + cursorColor: _accentColor, + backgroundCursorColor: const Color(0xFF9E9E9E), + ), + ), + ), + ], + ); + } +} + +class _Button extends StatelessWidget { + const _Button({required this.label, required this.onPressed}); + + final String label; + final VoidCallback onPressed; + + @override + Widget build(BuildContext context) { + return Semantics( + button: true, + child: GestureDetector( + onTap: onPressed, + child: DecoratedBox( + decoration: BoxDecoration( + color: _accentColor, + borderRadius: BorderRadius.circular(4), + ), + child: Padding( + padding: const EdgeInsets.all(12), + child: Text(label, textAlign: TextAlign.center), + ), + ), + ), + ); + } +} + +class _Message extends StatelessWidget { + const _Message(this.message, {required this.isError}); + + final String message; + final bool isError; + + @override + Widget build(BuildContext context) { + return Padding( + padding: const EdgeInsets.only(top: 16), + child: Text( + message, + style: TextStyle(color: isError ? _errorColor : _textColor), + ), + ); + } +} diff --git a/packages/supabase_flutter/example/pubspec.yaml b/packages/supabase_flutter/example/pubspec.yaml index 0d3a0aa25..0fc4e581e 100644 --- a/packages/supabase_flutter/example/pubspec.yaml +++ b/packages/supabase_flutter/example/pubspec.yaml @@ -21,6 +21,3 @@ dev_dependencies: sdk: flutter supabase_lints: ^0.1.2 - -flutter: - uses-material-design: true diff --git a/packages/supabase_flutter_web_auth/README.md b/packages/supabase_flutter_web_auth/README.md index df9f851ec..f7649329f 100644 --- a/packages/supabase_flutter_web_auth/README.md +++ b/packages/supabase_flutter_web_auth/README.md @@ -23,7 +23,7 @@ dependency. Add this package only if you want that trade-off. ## Usage ```dart file -import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; import 'package:supabase_flutter_web_auth/supabase_flutter_web_auth.dart'; @@ -36,7 +36,7 @@ Future main() async { oauthLauncher: FlutterWebAuth2OAuthLauncher(), ), ); - runApp(const MaterialApp(home: Placeholder())); + runApp(const Placeholder()); } ``` diff --git a/packages/supabase_flutter_web_auth/example/lib/main.dart b/packages/supabase_flutter_web_auth/example/lib/main.dart index 3d7dfde4a..b40ca4646 100644 --- a/packages/supabase_flutter_web_auth/example/lib/main.dart +++ b/packages/supabase_flutter_web_auth/example/lib/main.dart @@ -1,10 +1,13 @@ import 'dart:async'; import 'package:flutter/foundation.dart'; -import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; import 'package:supabase_flutter_web_auth/supabase_flutter_web_auth.dart'; +const _accentColor = Color(0xFF3ECF8E); +const _textColor = Color(0xFF1C1C1C); + Future main() async { await Supabase.initialize( url: 'SUPABASE_URL', @@ -21,9 +24,11 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { - return const MaterialApp( + return WidgetsApp( title: 'supabase_flutter_web_auth Demo', - home: MyHomePage(), + color: _accentColor, + textStyle: const TextStyle(color: _textColor, fontSize: 16), + builder: (context, child) => const MyHomePage(), ); } } @@ -37,6 +42,7 @@ class MyHomePage extends StatefulWidget { class _MyHomePageState extends State { User? _user; + String? _message; StreamSubscription? _authSubscription; @override @@ -47,13 +53,11 @@ class _MyHomePageState extends State { (data) { setState(() { _user = data.session?.user; + _message = null; }); }, onError: (Object error, StackTrace stackTrace) { - if (!mounted) return; - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Authentication error: $error')), - ); + _showMessage('Authentication error: $error'); }, ); } @@ -64,20 +68,14 @@ class _MyHomePageState extends State { super.dispose(); } - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar(title: const Text('OAuth via web auth session')), - body: _user == null ? const _SignInForm() : _SignedInView(user: _user!), - ); + void _showMessage(String message) { + if (!mounted) return; + setState(() { + _message = message; + }); } -} - -class _SignInForm extends StatelessWidget { - const _SignInForm(); - Future _signInWithGitHub(BuildContext context) async { - final messenger = ScaffoldMessenger.of(context); + Future _signInWithGitHub() async { try { final launched = await Supabase.instance.client.auth.signInWithOAuth( OAuthProvider.github, @@ -85,58 +83,79 @@ class _SignInForm extends StatelessWidget { ? null : 'io.supabase.flutterwebauthexample://oauth-callback', ); - if (!launched) { - messenger.showSnackBar( - const SnackBar(content: Text('Sign-in was cancelled')), - ); - } + if (!launched) _showMessage('Sign-in was cancelled'); } on Object catch (error) { - messenger.showSnackBar( - SnackBar(content: Text('Could not start sign-in: $error')), - ); + _showMessage('Could not start sign-in: $error'); + } + } + + Future _signOut() async { + try { + await Supabase.instance.client.auth.signOut(); + } on AuthException catch (error) { + _showMessage('Signed out, but not on the server: $error'); } } @override Widget build(BuildContext context) { - return Center( - child: ElevatedButton( - onPressed: () => unawaited(_signInWithGitHub(context)), - child: const Text('Sign in with GitHub'), + final user = _user; + final message = _message; + return ColoredBox( + color: const Color(0xFFFFFFFF), + child: SafeArea( + child: Center( + child: Column( + mainAxisSize: MainAxisSize.min, + spacing: 16, + children: [ + const Text( + 'OAuth via web auth session', + style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold), + ), + if (user == null) + _Button( + label: 'Sign in with GitHub', + onPressed: () => unawaited(_signInWithGitHub()), + ) + else ...[ + Text('Signed in as ${user.email}'), + _Button( + label: 'Sign out', + onPressed: () => unawaited(_signOut()), + ), + ], + if (message != null) Text(message, textAlign: TextAlign.center), + ], + ), + ), ), ); } } -class _SignedInView extends StatelessWidget { - const _SignedInView({required this.user}); +class _Button extends StatelessWidget { + const _Button({required this.label, required this.onPressed}); - final User user; - - Future _signOut(BuildContext context) async { - final messenger = ScaffoldMessenger.of(context); - try { - await Supabase.instance.client.auth.signOut(); - } on AuthException catch (error) { - messenger.showSnackBar( - SnackBar(content: Text('Signed out, but not on the server: $error')), - ); - } - } + final String label; + final VoidCallback onPressed; @override Widget build(BuildContext context) { - return Center( - child: Column( - mainAxisSize: MainAxisSize.min, - spacing: 16, - children: [ - Text('Signed in as ${user.email}'), - TextButton( - onPressed: () => unawaited(_signOut(context)), - child: const Text('Sign out'), + return Semantics( + button: true, + child: GestureDetector( + onTap: onPressed, + child: DecoratedBox( + decoration: BoxDecoration( + color: _accentColor, + borderRadius: BorderRadius.circular(4), + ), + child: Padding( + padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12), + child: Text(label), ), - ], + ), ), ); } diff --git a/packages/supabase_flutter_web_auth/example/pubspec.yaml b/packages/supabase_flutter_web_auth/example/pubspec.yaml index a764bb3bb..27ca01505 100644 --- a/packages/supabase_flutter_web_auth/example/pubspec.yaml +++ b/packages/supabase_flutter_web_auth/example/pubspec.yaml @@ -21,6 +21,3 @@ dev_dependencies: sdk: flutter supabase_lints: ^0.1.2 - -flutter: - uses-material-design: true From b09817bcbbce2d7e73c745c579c62767c04b2fc1 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Mon, 30 Nov 2026 12:03:31 +0100 Subject: [PATCH 2/2] chore(supabase_flutter): restore text selection, keyboard focus and sign-out message in the examples --- .../supabase_flutter/example/lib/main.dart | 262 +++++++++++++----- .../example/lib/main.dart | 51 +++- 2 files changed, 236 insertions(+), 77 deletions(-) diff --git a/packages/supabase_flutter/example/lib/main.dart b/packages/supabase_flutter/example/lib/main.dart index 1c219059d..42fb2a160 100644 --- a/packages/supabase_flutter/example/lib/main.dart +++ b/packages/supabase_flutter/example/lib/main.dart @@ -6,8 +6,12 @@ import 'package:supabase_flutter/supabase_flutter.dart'; import 'supabase_schema.g.dart'; const _accentColor = Color(0xFF3ECF8E); +const _borderColor = Color(0xFF9E9E9E); const _errorColor = Color(0xFFD32F2F); const _textColor = Color(0xFF1C1C1C); +const _whiteColor = Color(0xFFFFFFFF); + +typedef _ShowMessage = void Function(String? message, {bool isError}); Future main() async { await Supabase.initialize( @@ -26,7 +30,12 @@ class MyApp extends StatelessWidget { title: 'Supabase Flutter Demo', color: _accentColor, textStyle: const TextStyle(color: _textColor, fontSize: 16), - builder: (context, child) => const MyWidget(), + home: const MyWidget(), + pageRouteBuilder: (settings, builder) => PageRouteBuilder( + settings: settings, + pageBuilder: (context, animation, secondaryAnimation) => + builder(context), + ), ); } } @@ -40,6 +49,8 @@ class MyWidget extends StatefulWidget { class _MyWidgetState extends State { User? _user; + String? _message; + var _messageIsError = false; StreamSubscription? _authSubscription; @override @@ -48,8 +59,10 @@ class _MyWidgetState extends State { _user = Supabase.instance.client.auth.currentUser; _authSubscription = Supabase.instance.client.auth.onAuthStateChange.listen( (data) { + final user = data.session?.user; setState(() { - _user = data.session?.user; + if (user?.id != _user?.id) _message = null; + _user = user; }); }, onError: (error, stackTrace) { @@ -66,10 +79,19 @@ class _MyWidgetState extends State { super.dispose(); } + void _showMessage(String? message, {bool isError = false}) { + if (!mounted) return; + setState(() { + _message = message; + _messageIsError = isError; + }); + } + @override Widget build(BuildContext context) { + final message = _message; return ColoredBox( - color: const Color(0xFFFFFFFF), + color: _whiteColor, child: SafeArea( child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, @@ -85,8 +107,11 @@ class _MyWidgetState extends State { ), ), Expanded( - child: _user == null ? const _LoginForm() : const _ProfileForm(), + child: _user == null + ? _LoginForm(onMessage: _showMessage) + : _ProfileForm(onMessage: _showMessage), ), + if (message != null) _Message(message, isError: _messageIsError), ], ), ), @@ -95,7 +120,9 @@ class _MyWidgetState extends State { } class _LoginForm extends StatefulWidget { - const _LoginForm(); + const _LoginForm({required this.onMessage}); + + final _ShowMessage onMessage; @override State<_LoginForm> createState() => _LoginFormState(); @@ -103,7 +130,6 @@ class _LoginForm extends StatefulWidget { class _LoginFormState extends State<_LoginForm> { bool _loading = false; - String? _error; final _emailController = TextEditingController(); final _passwordController = TextEditingController(); @@ -117,18 +143,18 @@ class _LoginFormState extends State<_LoginForm> { Future _signIn() async { setState(() { _loading = true; - _error = null; }); + widget.onMessage(null); try { await Supabase.instance.client.auth.signInWithPassword( email: _emailController.text, password: _passwordController.text, ); } catch (_) { + widget.onMessage('Login failed', isError: true); if (mounted) { setState(() { _loading = false; - _error = 'Login failed'; }); } } @@ -137,18 +163,18 @@ class _LoginFormState extends State<_LoginForm> { Future _signUp() async { setState(() { _loading = true; - _error = null; }); + widget.onMessage(null); try { await Supabase.instance.client.auth.signUp( email: _emailController.text, password: _passwordController.text, ); } catch (_) { + widget.onMessage('Signup failed', isError: true); if (mounted) { setState(() { _loading = false; - _error = 'Signup failed'; }); } } @@ -156,7 +182,6 @@ class _LoginFormState extends State<_LoginForm> { @override Widget build(BuildContext context) { - final error = _error; return _loading ? const Center(child: Text('Loading')) : ListView( @@ -165,12 +190,14 @@ class _LoginFormState extends State<_LoginForm> { _TextInput( label: 'Email', keyboardType: TextInputType.emailAddress, + autofillHints: const [AutofillHints.email], controller: _emailController, ), const SizedBox(height: 16), _TextInput( label: 'Password', obscureText: true, + autofillHints: const [AutofillHints.password], controller: _passwordController, ), const SizedBox(height: 16), @@ -183,14 +210,15 @@ class _LoginFormState extends State<_LoginForm> { label: 'Signup', onPressed: () => unawaited(_signUp()), ), - if (error != null) _Message(error, isError: true), ], ); } } class _ProfileForm extends StatefulWidget { - const _ProfileForm(); + const _ProfileForm({required this.onMessage}); + + final _ShowMessage onMessage; @override State<_ProfileForm> createState() => _ProfileFormState(); @@ -198,8 +226,6 @@ class _ProfileForm extends StatefulWidget { class _ProfileFormState extends State<_ProfileForm> { var _loading = true; - String? _message; - var _messageIsError = false; final _usernameController = TextEditingController(); final _websiteController = TextEditingController(); @@ -216,14 +242,6 @@ class _ProfileFormState extends State<_ProfileForm> { super.dispose(); } - void _showMessage(String message, {bool isError = false}) { - if (!mounted) return; - setState(() { - _message = message; - _messageIsError = isError; - }); - } - Future _loadProfile() async { try { final userId = Supabase.instance.client.auth.currentUser!.id; @@ -239,7 +257,7 @@ class _ProfileFormState extends State<_ProfileForm> { }); } } catch (_) { - _showMessage('Error occurred while getting profile', isError: true); + widget.onMessage('Error occurred while getting profile', isError: true); } if (mounted) { setState(() { @@ -249,10 +267,11 @@ class _ProfileFormState extends State<_ProfileForm> { } Future _signOut() async { + widget.onMessage(null); try { await Supabase.instance.client.auth.signOut(); } on AuthException { - _showMessage( + widget.onMessage( 'Signed out, but the session was not revoked', isError: true, ); @@ -262,8 +281,8 @@ class _ProfileFormState extends State<_ProfileForm> { Future _saveProfile() async { setState(() { _loading = true; - _message = null; }); + widget.onMessage(null); try { final userId = Supabase.instance.client.auth.currentUser!.id; await Supabase.instance.client @@ -276,9 +295,9 @@ class _ProfileFormState extends State<_ProfileForm> { updatedAt: DateTime.now(), ), ); - _showMessage('Saved profile'); + widget.onMessage('Saved profile'); } catch (_) { - _showMessage('Error saving profile', isError: true); + widget.onMessage('Error saving profile', isError: true); } if (mounted) { setState(() { @@ -289,7 +308,6 @@ class _ProfileFormState extends State<_ProfileForm> { @override Widget build(BuildContext context) { - final message = _message; return _loading ? const Center(child: Text('Loading')) : ListView( @@ -297,7 +315,11 @@ class _ProfileFormState extends State<_ProfileForm> { children: [ _TextInput(label: 'Username', controller: _usernameController), const SizedBox(height: 16), - _TextInput(label: 'Website', controller: _websiteController), + _TextInput( + label: 'Website', + keyboardType: TextInputType.url, + controller: _websiteController, + ), const SizedBox(height: 16), _Button( label: 'Save', @@ -308,7 +330,6 @@ class _ProfileFormState extends State<_ProfileForm> { label: 'Sign Out', onPressed: () => unawaited(_signOut()), ), - if (message != null) _Message(message, isError: _messageIsError), ], ); } @@ -319,20 +340,35 @@ class _TextInput extends StatefulWidget { required this.label, required this.controller, this.keyboardType, + this.autofillHints, this.obscureText = false, }); final String label; final TextEditingController controller; final TextInputType? keyboardType; + final Iterable? autofillHints; final bool obscureText; @override State<_TextInput> createState() => _TextInputState(); } -class _TextInputState extends State<_TextInput> { +class _TextInputState extends State<_TextInput> + implements TextSelectionGestureDetectorBuilderDelegate { final _focusNode = FocusNode(); + late final _gestureDetectorBuilder = TextSelectionGestureDetectorBuilder( + delegate: this, + ); + + @override + final editableTextKey = GlobalKey(); + + @override + bool get forcePressEnabled => false; + + @override + bool get selectionEnabled => true; @override void dispose() { @@ -342,54 +378,152 @@ class _TextInputState extends State<_TextInput> { @override Widget build(BuildContext context) { - return Column( - crossAxisAlignment: CrossAxisAlignment.start, - spacing: 4, - children: [ - Text(widget.label), - DecoratedBox( - decoration: BoxDecoration( - border: Border.all(color: const Color(0xFF9E9E9E)), - borderRadius: BorderRadius.circular(4), - ), - child: Padding( - padding: const EdgeInsets.all(8), - child: EditableText( - controller: widget.controller, - focusNode: _focusNode, - keyboardType: widget.keyboardType, - obscureText: widget.obscureText, - style: const TextStyle(color: _textColor, fontSize: 16), - cursorColor: _accentColor, - backgroundCursorColor: const Color(0xFF9E9E9E), + return _gestureDetectorBuilder.buildGestureDetector( + behavior: HitTestBehavior.translucent, + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + spacing: 4, + children: [ + Text(widget.label), + DecoratedBox( + decoration: BoxDecoration( + border: Border.all(color: _borderColor), + borderRadius: BorderRadius.circular(4), + ), + child: Padding( + padding: const EdgeInsets.all(8), + child: EditableText( + key: editableTextKey, + controller: widget.controller, + focusNode: _focusNode, + keyboardType: widget.keyboardType, + autofillHints: widget.autofillHints, + obscureText: widget.obscureText, + rendererIgnoresPointer: true, + style: const TextStyle(color: _textColor, fontSize: 16), + cursorColor: _accentColor, + backgroundCursorColor: _borderColor, + selectionColor: _accentColor.withValues(alpha: 0.4), + contextMenuBuilder: (context, editableTextState) => + _TextContextMenu(editableTextState: editableTextState), + ), ), ), + ], + ), + ); + } +} + +class _TextContextMenu extends StatelessWidget { + const _TextContextMenu({required this.editableTextState}); + + final EditableTextState editableTextState; + + @override + Widget build(BuildContext context) { + final anchors = editableTextState.contextMenuAnchors; + return CustomSingleChildLayout( + delegate: TextSelectionToolbarLayoutDelegate( + anchorAbove: anchors.primaryAnchor, + anchorBelow: anchors.secondaryAnchor ?? anchors.primaryAnchor, + ), + child: DecoratedBox( + decoration: BoxDecoration( + color: _whiteColor, + border: Border.all(color: _borderColor), + borderRadius: BorderRadius.circular(4), + ), + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + for (final item in editableTextState.contextMenuButtonItems) + _TextContextMenuButton(item: item), + ], + ), + ), + ); + } +} + +class _TextContextMenuButton extends StatelessWidget { + const _TextContextMenuButton({required this.item}); + + final ContextMenuButtonItem item; + + String get _label => + item.label ?? + switch (item.type) { + ContextMenuButtonType.cut => 'Cut', + ContextMenuButtonType.copy => 'Copy', + ContextMenuButtonType.paste => 'Paste', + ContextMenuButtonType.selectAll => 'Select all', + ContextMenuButtonType.delete => 'Delete', + ContextMenuButtonType.lookUp => 'Look up', + ContextMenuButtonType.searchWeb => 'Search web', + ContextMenuButtonType.share => 'Share', + ContextMenuButtonType.liveTextInput => 'Scan text', + ContextMenuButtonType.custom => '', + }; + + @override + Widget build(BuildContext context) { + return Semantics( + button: true, + child: GestureDetector( + behavior: HitTestBehavior.opaque, + onTap: item.onPressed, + child: Padding( + padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8), + child: Text(_label), ), - ], + ), ); } } -class _Button extends StatelessWidget { +class _Button extends StatefulWidget { const _Button({required this.label, required this.onPressed}); final String label; final VoidCallback onPressed; + @override + State<_Button> createState() => _ButtonState(); +} + +class _ButtonState extends State<_Button> { + var _focused = false; + + void _activate(ActivateIntent intent) => widget.onPressed(); + @override Widget build(BuildContext context) { return Semantics( button: true, - child: GestureDetector( - onTap: onPressed, - child: DecoratedBox( - decoration: BoxDecoration( - color: _accentColor, - borderRadius: BorderRadius.circular(4), + child: FocusableActionDetector( + mouseCursor: SystemMouseCursors.click, + onShowFocusHighlight: (focused) => setState(() => _focused = focused), + actions: { + ActivateIntent: CallbackAction( + onInvoke: _activate, ), - child: Padding( - padding: const EdgeInsets.all(12), - child: Text(label, textAlign: TextAlign.center), + }, + child: GestureDetector( + onTap: widget.onPressed, + child: DecoratedBox( + decoration: BoxDecoration( + color: _accentColor, + border: Border.all( + color: _focused ? _textColor : _accentColor, + width: 2, + ), + borderRadius: BorderRadius.circular(4), + ), + child: Padding( + padding: const EdgeInsets.all(12), + child: Text(widget.label, textAlign: TextAlign.center), + ), ), ), ), @@ -406,7 +540,7 @@ class _Message extends StatelessWidget { @override Widget build(BuildContext context) { return Padding( - padding: const EdgeInsets.only(top: 16), + padding: const EdgeInsets.all(16), child: Text( message, style: TextStyle(color: isError ? _errorColor : _textColor), diff --git a/packages/supabase_flutter_web_auth/example/lib/main.dart b/packages/supabase_flutter_web_auth/example/lib/main.dart index b40ca4646..eecd55ce0 100644 --- a/packages/supabase_flutter_web_auth/example/lib/main.dart +++ b/packages/supabase_flutter_web_auth/example/lib/main.dart @@ -51,9 +51,10 @@ class _MyHomePageState extends State { _user = Supabase.instance.client.auth.currentUser; _authSubscription = Supabase.instance.client.auth.onAuthStateChange.listen( (data) { + final user = data.session?.user; setState(() { - _user = data.session?.user; - _message = null; + if (user?.id != _user?.id) _message = null; + _user = user; }); }, onError: (Object error, StackTrace stackTrace) { @@ -68,7 +69,7 @@ class _MyHomePageState extends State { super.dispose(); } - void _showMessage(String message) { + void _showMessage(String? message) { if (!mounted) return; setState(() { _message = message; @@ -76,6 +77,7 @@ class _MyHomePageState extends State { } Future _signInWithGitHub() async { + _showMessage(null); try { final launched = await Supabase.instance.client.auth.signInWithOAuth( OAuthProvider.github, @@ -90,6 +92,7 @@ class _MyHomePageState extends State { } Future _signOut() async { + _showMessage(null); try { await Supabase.instance.client.auth.signOut(); } on AuthException catch (error) { @@ -134,26 +137,48 @@ class _MyHomePageState extends State { } } -class _Button extends StatelessWidget { +class _Button extends StatefulWidget { const _Button({required this.label, required this.onPressed}); final String label; final VoidCallback onPressed; + @override + State<_Button> createState() => _ButtonState(); +} + +class _ButtonState extends State<_Button> { + var _focused = false; + + void _activate(ActivateIntent intent) => widget.onPressed(); + @override Widget build(BuildContext context) { return Semantics( button: true, - child: GestureDetector( - onTap: onPressed, - child: DecoratedBox( - decoration: BoxDecoration( - color: _accentColor, - borderRadius: BorderRadius.circular(4), + child: FocusableActionDetector( + mouseCursor: SystemMouseCursors.click, + onShowFocusHighlight: (focused) => setState(() => _focused = focused), + actions: { + ActivateIntent: CallbackAction( + onInvoke: _activate, ), - child: Padding( - padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12), - child: Text(label), + }, + child: GestureDetector( + onTap: widget.onPressed, + child: DecoratedBox( + decoration: BoxDecoration( + color: _accentColor, + border: Border.all( + color: _focused ? _textColor : _accentColor, + width: 2, + ), + borderRadius: BorderRadius.circular(4), + ), + child: Padding( + padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12), + child: Text(widget.label), + ), ), ), ),