Add counter and notifier for a value.
This commit is contained in:
parent
36e5156660
commit
e9d74e2326
@ -1,6 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||||
|
import 'package:riverpod_flutter_tuts/widgets/notifier/counter_widget.dart';
|
||||||
|
|
||||||
import '../widgets/consumer/random.dart';
|
import '../widgets/consumer/random.dart';
|
||||||
import '../widgets/simples/random.dart';
|
import '../widgets/simples/random.dart';
|
||||||
@ -42,7 +43,8 @@ class MyApp extends ConsumerWidget {
|
|||||||
const Expanded(child: Text('1')),
|
const Expanded(child: Text('1')),
|
||||||
Expanded(child: Text(value)),
|
Expanded(child: Text(value)),
|
||||||
const Expanded(child: RandomWidget()),
|
const Expanded(child: RandomWidget()),
|
||||||
const Expanded(child: Random2Widget())
|
const Expanded(child: Random2Widget()),
|
||||||
|
const Expanded(child: CounterWidget()),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
29
lib/widgets/notifier/counter.dart
Normal file
29
lib/widgets/notifier/counter.dart
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import 'package:flutter/cupertino.dart';
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||||
|
|
||||||
|
|
||||||
|
part 'counter.g.dart';
|
||||||
|
|
||||||
|
@riverpod
|
||||||
|
class CounterNotifier extends _$CounterNotifier {
|
||||||
|
|
||||||
|
int x = 0;
|
||||||
|
@override
|
||||||
|
Future<int> build() async{
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
void plus() {
|
||||||
|
x++;
|
||||||
|
ref.invalidateSelf();
|
||||||
|
// state = AsyncData(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
void minus() {
|
||||||
|
x--;
|
||||||
|
// ref.invalidateSelf();
|
||||||
|
state = AsyncData(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
34
lib/widgets/notifier/counter_widget.dart
Normal file
34
lib/widgets/notifier/counter_widget.dart
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import 'package:flutter/cupertino.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import 'package:riverpod_flutter_tuts/widgets/notifier/counter.dart';
|
||||||
|
|
||||||
|
|
||||||
|
class CounterWidget extends ConsumerWidget {
|
||||||
|
const CounterWidget({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
|
// TODO: implement build
|
||||||
|
|
||||||
|
var myRandom = ref.watch(counterNotifierProvider);
|
||||||
|
return Column(
|
||||||
|
children: [
|
||||||
|
Center(
|
||||||
|
child: switch (myRandom) {
|
||||||
|
AsyncData(:final value ) => Text(
|
||||||
|
'data: $value'
|
||||||
|
),
|
||||||
|
AsyncError() => const Text('Oops, something unexpected happened'),
|
||||||
|
_ => const CircularProgressIndicator(),
|
||||||
|
}),
|
||||||
|
ElevatedButton(onPressed:() {
|
||||||
|
ref.read(counterNotifierProvider.notifier).plus();
|
||||||
|
}, child: const Text("plus")),
|
||||||
|
ElevatedButton(onPressed: () {
|
||||||
|
ref.read(counterNotifierProvider.notifier).minus();
|
||||||
|
}, child: const Text("minus")),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user