riverpod_context 0.2.0-dev.0 riverpod_context: ^0.2.0-dev.0 copied to clipboard
This package brings back context.read and context.watch for riverpod
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),
),
),
);
}
}