Firebase Authentication

Jun's Coding Journey·2023년 9월 18일
0

[Learn] Firebase

목록 보기
4/7

State Provider


StateProvider typically refers to a component or class that manages the state of a widget or a portion of your application. Flutter is a UI framework that allows you to build user interfaces using a widget-based architecture. Widgets are the building blocks of your UI, and they can have associated state.

A StateProvider is not a built-in or standard Flutter concept; it's more of a general term that developers might use to describe a class or component responsible for managing the state of a widget or a part of their Flutter application.

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

// Define a class to manage the state and business logic
class CounterProvider with ChangeNotifier {
  int _count = 0;

  int get count => _count;

  void increment() {
    _count++;
    notifyListeners(); // Notify listeners when the state changes
  }
}

// Use a widget to provide the state to the widget tree
class MyApp extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ChangeNotifierProvider(
        create: (context) => CounterProvider(),
        child: MyHomePage(),
      ),
    );
  }
}

// Consume the state using a Consumer widget
class MyHomePage extends StatelessWidget {
  
  Widget build(BuildContext context) {
    final counter = Provider.of<CounterProvider>(context);

    return Scaffold(
      appBar: AppBar(
        title: Text('Counter App'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('Count: ${counter.count}'),
            ElevatedButton(
              onPressed: () => counter.increment(),
              child: Text('Increment'),
            ),
          ],
        ),
      ),
    );
  }
}

void main() => runApp(MyApp());

 

Stream


A "stream" typically refers to a real-time data synchronization mechanism that allows you to receive updates in real-time as changes occur in a Firebase Realtime Database or Firestore database. Firebase provides a powerful way to manage and sync data across devices and clients using streams.

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return MaterialApp(
      home: StreamBuilder(
        stream: FirebaseFirestore.instance.collection('items').snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return CircularProgressIndicator();
          }
          final items = snapshot.data.docs;
          List<Widget> itemWidgets = [];
          for (var item in items) {
            final itemName = item['name'];
            final itemPrice = item['price'];

            itemWidgets.add(
              ListTile(
                title: Text(itemName),
                subtitle: Text('\$$itemPrice'),
              ),
            );
          }

          return ListView(
            children: itemWidgets,
          );
        },
      ),
    );
  }
}

 

Snackbar


SnackBar widget is a small pop-up message that appears at the bottom of the screen temporarily to provide information or feedback to the user. It is typically used to display non-intrusive messages, notifications, or actions, such as confirming an action, displaying a message, or providing a brief status update.

A SnackBar typically consists of a short message and an optional action button that allows the user to take some action in response to the message. Users can dismiss the SnackBar by tapping outside of it or by letting it disappear automatically after a certain duration.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('SnackBar Example'),
        ),
        body: SnackBarDemo(),
      ),
    );
  }
}

class SnackBarDemo extends StatelessWidget {
  void _showSnackBar(BuildContext context) {
    final snackBar = SnackBar(
      content: Text('This is a SnackBar'),
      duration: Duration(seconds: 3), // How long the SnackBar will be displayed
      action: SnackBarAction(
        label: 'Undo',
        onPressed: () {
          // Add your action logic here
          ScaffoldMessenger.of(context).hideCurrentSnackBar(); // Hide the SnackBar
        },
      ),
    );

    // Show the SnackBar
    ScaffoldMessenger.of(context).showSnackBar(snackBar);
  }

  
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        onPressed: () {
          _showSnackBar(context);
        },
        child: Text('Show SnackBar'),
      ),
    );
  }
}

profile
Greatness From Small Beginnings

0개의 댓글