flutter_riverpod: ^2.5.1
import 'package:flutter_riverpod/flutter_riverpod.dart';
class Todo {
bool isCompleted;
String description;
Todo(this.isCompleted, this.description);
}
final todoProvider = StateProvider<Todo>((ref) {
return Todo(true, "공부하기");
});
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:stateapp/todo.dart';
void main() {
runApp(ProviderScope(child: MyApp()));
}
class MyApp extends StatelessWidget {
MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends ConsumerWidget {
HomePage({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
Todo todo = ref.read(todoProvider);
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"완료 : ${todo.isCompleted.toString()}, 내용 : ${todo.description}",
style: TextStyle(fontSize: 30),
),
ElevatedButton(onPressed: (){
ref.read(todoProvider).isCompleted = false;
}, child: Text("공부완료")),
Deps1(),
],
),
),
);
}
}
class Deps1 extends StatelessWidget {
const Deps1({
super.key,
});
@override
Widget build(BuildContext context) {
return Deps2();
}
}
class Deps2 extends StatelessWidget {
const Deps2({
super.key,
});
@override
Widget build(BuildContext context) {
return Deps3();
}
}
class Deps3 extends ConsumerWidget {
const Deps3({
super.key,
});
@override
Widget build(BuildContext context, WidgetRef ref) {
Todo todo = ref.read(todoProvider);
return Text("Deps3 컨텍스트 : ${todo.isCompleted}", style: TextStyle(color: Colors.red, fontSize: 30),);
}
}