Skip to content

RtMultiProvider

The RtMultiProvider widget is a container for multiple providers that allows you to provide multiple dependencies to the widget tree.

Syntax

RtMultiProvider(
List<RtProvider> providers, {
Key? key,
Widget? child,
Widget builder(
BuildContext context,
Widget child,
)?,
})

Properties

  • providers: A list of RtProvider widgets that are to be wrapped by the RtMultiProvider .
  • key: An optional Key to use for identifying the widget.
  • child: An optional Widget that remains static while the widget tree is rebuilt. It is passed to the builder function if it is defined.
  • builder : An optional function which builds a widget depending on the RtMultiProvider . If it not defined, the child widget is returned. It receives the following arguments:
    • context: The BuildContext of the widget. A handle to the location of RtMultiProvider in the widget tree.
    • child: The child widget passed to the RtMultiProvider widget.

Usage

In the following example, we have a simple counter application that uses the RtMultiProvider widget to provide multiple dependencies to the widget tree.

1
import 'package:flutter/material.dart';
2
import 'package:flutter_reactter/flutter_reactter.dart';
3
import 'counter_view.dart';
4
import 'counter_controller.dart';
5
6
void main() {
7
runApp(MyApp());
8
}
9
10
class MyApp extends StatelessWidget {
11
@override
12
Widget build(BuildContext context) {
13
return MaterialApp(
14
home: RtMultiProvider(
15
[
16
RtProvider(() {
17
print('CounterController created');
18
return CounterController();
19
}),
20
RtProvider.lazy(
21
() {
22
print('Lazy CounterController created');
23
return CounterController();
24
},
25
id: 'counterLazy',
26
),
27
],
28
builder: (context, child) {
29
return CounterView();
30
},
31
),
32
);
33
}
34
}
1
import 'package:flutter/material.dart';
2
import 'counter.dart';
3
4
class CounterView extends StatelessWidget {
5
const CounterView({Key? key}) : super(key: key);
6
7
@override
8
Widget build(BuildContext context) {
9
return Scaffold(
10
appBar: AppBar(
11
title: const Text("Counter"),
12
),
13
body: Center(
14
child: Column(
15
mainAxisAlignment: MainAxisAlignment.center,
16
children: [
17
const Counter(),
18
const SizedBox(height: 8),
19
FutureBuilder(
20
future: Future.delayed(Duration(seconds: 1)),
21
builder: (context, snapshot) {
22
if (snapshot.connectionState != ConnectionState.done) {
23
return const CircularProgressIndicator();
24
}
25
26
return const Counter(id: "counterLazy");
27
},
28
),
29
],
30
),
31
),
32
);
33
}
34
}
1
import 'package:flutter/material.dart';
2
import 'package:flutter_reactter/flutter_reactter.dart';
3
import 'counter_controller.dart';
4
5
class Counter extends StatelessWidget {
6
final String? id;
7
8
const Counter({Key? key, this.id}) : super(key: key);
9
10
@override
11
Widget build(BuildContext context) {
12
// Locale the `CounterController` dependency
13
return RtConsumer<CounterController>(
14
id: id,
15
builder: (context, counterController, child) {
16
return Row(
17
mainAxisSize: MainAxisSize.min,
18
children: [
19
ElevatedButton(
20
onPressed: counterController.decrement,
21
child: const Icon(Icons.remove),
22
),
23
const SizedBox(width: 8),
24
// Observe the `count` property of the `counterController`
25
// and rebuild the widget tree when the `count` value changes
26
RtConsumer<CounterController>(
27
id: id,
28
listenStates: (counterController) => [counterController.count],
29
builder: (context, counterController, child) {
30
return Text("${counterController.count}");
31
},
32
),
33
const SizedBox(width: 8),
34
ElevatedButton(
35
onPressed: counterController.increment,
36
child: const Icon(Icons.add),
37
),
38
],
39
);
40
},
41
);
42
}
43
}
1
import 'package:reactter/reactter.dart';
2
3
class CounterController {
4
// Create a reactive state using the `Signal` class
5
final count = Signal(0);
6
7
void increment() {
8
count.value++;
9
}
10
11
void decrement() {
12
count.value--;
13
}
14
}