Skip to content

Instantly share code, notes, and snippets.

@yaizudamashii
Created April 5, 2021 20:13
Embed
What would you like to do?
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_voice_chat_using_agora/app/home/empty_feed.dart';
import 'package:flutter_voice_chat_using_agora/app/top_level_providers.dart';
class AuthWidget extends ConsumerWidget {
const AuthWidget({
Key key,
@required this.signedInBuilder,
@required this.nonSignedInBuilder,
}) : super(key: key);
final WidgetBuilder nonSignedInBuilder;
final WidgetBuilder signedInBuilder;
@override
Widget build(BuildContext context, ScopedReader watch) {
final authStateChanges = watch(authStateChangesProvider);
return authStateChanges.when(
data: (user) => _data(context, user),
loading: () => Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
),
error: (_, __) => Scaffold(
body: EmptyFeed(
title: 'Something went wrong',
message: 'Can\'t load data right now.',
),
),
);
}
Widget _data(BuildContext context, User user) {
if (user != null) {
return signedInBuilder(context);
}
return nonSignedInBuilder(context);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment