ActivitySelector<C extends ActiveController, V> class

Rebuild a subtree only when its immutable selected value changes under ==.

Select scalars or immutable records, not a collection mutated in place. The application owns the shared controller and the selector owns its subscription.

Gallery walkthrough · browser route /#/views. These examples use the current checkout; published 1.x APIs can differ.

Select a counter

import 'dart:async';
import 'package:activity/activity.dart';
import 'package:flutter/material.dart';

class CounterController extends ActiveController {
  final count = ActiveInt(0);
  @override
  Iterable<ActiveType> get activities => [count];
}

void main() => runApp(const MaterialApp(home: CounterOwner()));

class CounterOwner extends StatefulWidget {
  const CounterOwner({super.key});
  @override
  State<CounterOwner> createState() => _CounterOwnerState();
}

class _CounterOwnerState extends State<CounterOwner> {
  final controller = CounterController();
  @override
  void dispose() {
    unawaited(controller.dispose());
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
    body: ActivitySelector<CounterController, int>(
      controller: controller,
      select: (c) => c.count.value,
      builder:
          (_, count, child) => TextButton(
            onPressed: controller.count.increment,
            child: Text('Count $count'),
          ),
    ),
  );
}

Select a derived property

import 'dart:async';
import 'package:activity/activity.dart';
import 'package:flutter/material.dart';

class CounterController extends ActiveController {
  final count = ActiveInt(0);
  @override
  Iterable<ActiveType> get activities => [count];
}

void main() => runApp(const MaterialApp(home: CounterOwner()));

class CounterOwner extends StatefulWidget {
  const CounterOwner({super.key});
  @override
  State<CounterOwner> createState() => _CounterOwnerState();
}

class _CounterOwnerState extends State<CounterOwner> {
  final controller = CounterController();
  @override
  void dispose() {
    unawaited(controller.dispose());
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
    body: ActivitySelector<CounterController, bool>(
      controller: controller,
      select: (c) => c.count.value.isEven,
      builder:
          (_, even, child) => TextButton(
            onPressed: controller.count.increment,
            child: Text(even ? 'Even' : 'Odd'),
          ),
    ),
  );
}
Inheritance

Constructors

ActivitySelector({Key? key, required C controller, required V select(C), required Widget builder(BuildContext, V, Widget?), Widget? child})
const

Properties

builder → Widget Function(BuildContext, V, Widget?)
final
child → Widget?
final
controller → C
final
hashCode → int
The hash code for this object.
no setterinherited
key → Key?
Controls how one widget replaces another widget in the tree.
finalinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
select → V Function(C)
final

Methods

createElement() → StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() → State<ActivitySelector<C, V>>
Creates the mutable state for this widget at a given location in the tree.
override
debugDescribeChildren() → List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) → String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) → String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String
Returns a one-line detailed description of the object.
inherited
toStringShort() → String
A short, textual description of this widget.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited