43 lines
946 B
Dart
43 lines
946 B
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
class StreamProviderPage extends StatelessWidget {
|
|
const StreamProviderPage({super.key});
|
|
|
|
Stream<int> countStream() async* {
|
|
int i = 0;
|
|
while (true) {
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
yield i++;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return StreamProvider<int>(
|
|
initialData: 0,
|
|
create: (_) => countStream(),
|
|
child: Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('StreamProvider Example'),
|
|
),
|
|
body: const Center(
|
|
child: CounterDisplay(),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class CounterDisplay extends StatelessWidget {
|
|
const CounterDisplay({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Text(
|
|
'Counter: ${context.watch<int>()}',
|
|
style: Theme.of(context).textTheme.headlineMedium,
|
|
);
|
|
}
|
|
}
|