riverpod_context 0.3.0 copy "riverpod_context: ^0.3.0" to clipboard
riverpod_context: ^0.3.0 copied to clipboard

This package brings back context.read and context.watch for riverpod

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_context/riverpod_context.dart';

var counter = StateProvider((ref) => 0);

void main() {
  runApp(const ProviderScope(
    // Add [InheritedConsumer] underneath the root [ProviderScope]
    child: InheritedConsumer(
      child: MyApp(),
    ),
  ));
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Riverpod Context Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Riverpod Context Demo'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Text(
                'You have pushed the button this many times:',
              ),

              // Wrap in builder to get new context. This will
              // only rebuild this builder when counter changes
              Builder(
                builder: (context) => Text(
                  '${context.watch(counter)}',
                  style: Theme.of(context).textTheme.headline4,
                ),
              ),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            // Read from anywhere
            context.read(counter.state).state++;
          },
          tooltip: 'Increment',
          child: const Icon(Icons.add),
        ),
      ),
    );
  }
}
18
likes
160
points
3.22k
downloads

Publisher

verified publisherschultek.dev

Weekly Downloads

This package brings back context.read and context.watch for riverpod

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, flutter_riverpod

More

Packages that depend on riverpod_context