Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SplitViewController: add weights setter and notify changes #18

Merged
merged 1 commit into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 48 additions & 10 deletions lib/split_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ library split_view;
import 'dart:collection';
import 'dart:math';

import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
Expand Down Expand Up @@ -74,8 +75,38 @@ class _SplitViewState extends State<SplitView> {
void initState() {
super.initState();

_controller =
widget.controller != null ? widget.controller! : SplitViewController();
_controller = widget.controller ?? SplitViewController();
_controller.addListener(_handleWeightsChange);
}

@override
void dispose() {
_controller.removeListener(_handleWeightsChange);
if (widget.controller == null) {
_controller.dispose();
}

super.dispose();
}

@override
void didUpdateWidget(SplitView oldWidget) {
super.didUpdateWidget(oldWidget);

if (widget.controller != oldWidget.controller) {
oldWidget.controller?.removeListener(_handleWeightsChange);
widget.controller?.addListener(_handleWeightsChange);

if (oldWidget.controller != null && widget.controller == null) {
_controller = SplitViewController(
weights: _controller.weights,
limits: _controller.limits,
);
} else if (widget.controller != null && oldWidget.controller == null) {
_controller.dispose();
_controller = widget.controller!;
}
}
}

@override
Expand Down Expand Up @@ -284,14 +315,16 @@ class _SplitViewState extends State<SplitView> {
}
newWeight1 = _startWeight1 + _startWeight2 - newWeight2;
}
setState(() {
_controller._weights[index] = newWeight1;
_controller._weights[index + 1] = newWeight2;
});

if (widget.onWeightChanged != null) {
widget.onWeightChanged!(_controller.weights);
}
final weights = List.of(_controller._weights);
weights[index] = newWeight1;
weights[index + 1] = newWeight2;
_controller.weights = weights;
}

void _handleWeightsChange() {
setState(() {/* build() uses _controller.weights */});
widget.onWeightChanged?.call(_controller.weights);
}

Offset _getLocalPosition(BuildContext context, Offset pos) {
Expand All @@ -307,9 +340,14 @@ class _SplitViewState extends State<SplitView> {
}

/// Controller for [Splitview]
class SplitViewController {
class SplitViewController extends ChangeNotifier {
/// Specifies the weight of each views.
UnmodifiableListView<double?> get weights => UnmodifiableListView(_weights);
set weights(List<double?> weights) {
if (const ListEquality().equals(_weights, weights)) return;
_weights = weights;
notifyListeners();
}

/// Specifies the limits of each views.
UnmodifiableListView<WeightLimit?> get limits =>
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ environment:
sdk: '>=2.12.0 <3.0.0'

dependencies:
collection: ^1.16.0
flutter:
sdk: flutter

Expand Down
39 changes: 33 additions & 6 deletions test/split_view_test.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:split_view/split_view.dart';

void main() {
test('adds one to input values', () {
// final calculator = Calculator();
// expect(calculator.addOne(2), 3);
// expect(calculator.addOne(-7), -6);
// expect(calculator.addOne(0), 1);
// expect(() => calculator.addOne(null), throwsNoSuchMethodError);
testWidgets('controller', (tester) async {
final controller = SplitViewController(weights: [0.1, 0.3, 0.6]);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: SplitView(
gripSize: 10,
controller: controller,
viewMode: SplitViewMode.Horizontal,
children: [
Container(key: ValueKey(0)),
Container(key: ValueKey(1)),
Container(key: ValueKey(2)),
],
),
),
),
);

final totalWidth = tester.getSize(find.byType(SplitView)).width - 2 * 10;

expect(tester.getSize(find.byKey(ValueKey(0))).width, totalWidth * 0.1);
expect(tester.getSize(find.byKey(ValueKey(1))).width, totalWidth * 0.3);
expect(tester.getSize(find.byKey(ValueKey(2))).width, totalWidth * 0.6);

controller.weights = [0.1, 0.4, 0.5];
await tester.pump();

expect(tester.getSize(find.byKey(ValueKey(0))).width, totalWidth * 0.1);
expect(tester.getSize(find.byKey(ValueKey(1))).width, totalWidth * 0.4);
expect(tester.getSize(find.byKey(ValueKey(2))).width, totalWidth * 0.5);
});
}