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

outdated

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),
        ),
      ),
    );
  }
}
17
likes
0
pub points
81%
popularity

Publisher

verified publisherschultek.de

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

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, flutter_riverpod

More

Packages that depend on riverpod_context