Add widget to homepage. Two widgets shows same (random) data, and just one request recorded on API server. It's good.
53 lines
1.4 KiB
Dart
53 lines
1.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
import '../widgets/consumer/random.dart';
|
|
import '../widgets/simples/random.dart';
|
|
|
|
part 'gt.g.dart';
|
|
|
|
// We create a "provider", which will store a value (here "Hello world").
|
|
// By using a provider, this allows us to mock/override the value exposed.
|
|
@riverpod
|
|
String helloWorld(HelloWorldRef ref) {
|
|
return 'Hello world';
|
|
}
|
|
|
|
void main() {
|
|
runApp(
|
|
// For widgets to be able to read providers, we need to wrap the entire
|
|
// application in a "ProviderScope" widget.
|
|
// This is where the state of our providers will be stored.
|
|
const ProviderScope(
|
|
child: MyApp(),
|
|
),
|
|
);
|
|
}
|
|
|
|
// Extend ConsumerWidget instead of StatelessWidget, which is exposed by Riverpod
|
|
class MyApp extends ConsumerWidget {
|
|
const MyApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final String value = ref.watch(helloWorldProvider);
|
|
|
|
return MaterialApp(
|
|
home: Scaffold(
|
|
appBar: AppBar(title: const Text('Example')),
|
|
body: Center(
|
|
child: Column(
|
|
children: <Widget>[
|
|
const Expanded(child: Text('1')),
|
|
Expanded(child: Text(value)),
|
|
const Expanded(child: RandomWidget()),
|
|
const Expanded(child: Random2Widget())
|
|
],
|
|
)
|
|
|
|
),
|
|
),
|
|
);
|
|
}
|
|
} |