Git Product home page Git Product logo

simple_notifier's Introduction

Simplify Value Notifier Usage with simple_notifier Package

This is a simple Flutter application that demonstrates the usage of the simple_notifier package to simplify the management of state with ValueNotifier. With the simple_notifier package, you can streamline your code and easily listen to changes in state, making state management in Flutter apps much more straightforward.

pub package pub package

Overview

In this example, we create a basic Flutter app that displays a counter value and allows the user to increment and reset the counter using buttons. By utilizing the simple_notifier package, we efficiently manage the counter state using ValueNotifier, and we use the ValueListenableBuilder to listen for changes and update the UI accordingly.

Prerequisites

Make sure you have Flutter installed and set up on your development environment.

Setup

  1. Create a new Flutter project.
  2. Add the simple_notifier package to your pubspec.yaml file:
    dependencies:
      flutter:
        sdk: flutter
      simple_notifier: any
  3. Run flutter pub get to install the package.

Code Example

import 'package:flutter/material.dart';
import 'package:simple_notifier/value_notifier_extension_test.dart';

void main() {
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  MyApp({super.key});

  final _counter = 0.notifier;
  
@override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Simple Notifier Example'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              _counter.listen(
                builder: (context, value, child) {
                  return Text(
                    'Counter Value: $value',
                    style: const TextStyle(fontSize: 24),
                  );
                },
              ),
              const SizedBox(height: 20),
              ElevatedButton(
                onPressed: _incrementCounter,
                child: const Text('Increment Counter'),
              ),
              const SizedBox(height: 8),
              ElevatedButton(
                onPressed: _resetCounter,
                child: const Text('Reset Counter'),
              ),
            ],
          ),
        ),
      ),
    );
  }
  
  void _incrementCounter() {
    _counter.value++;
  }

  void _resetCounter() {
    _counter.value = 0;
  }
}

The _incrementCounter and _resetCounter functions update the _counter value notifier by incrementing or resetting the counter value.

Conclusion

In this example, we learned how to utilize the simple_notifier package to simplify the usage of ValueNotifier and manage state more efficiently in Flutter applications. We created a basic Flutter app that displays a counter value and allows users to increment and reset the counter using buttons. By using the simple_notifier package, we streamline the state management process, making it easier to manage state changes and update the UI accordingly.

simple_notifier's People

Contributors

kakzaki avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.