Skip to content

fix: Scrollbar should be in the side of the logview #397

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

Merged
merged 5 commits into from
Apr 9, 2024
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
4 changes: 2 additions & 2 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ scripts:

# run integration tests in all packages
integration_test: >
melos exec -c 1 --fail-fast --dir-exists=integration_test -- \
melos exec -c 1 --dir-exists=integration_test -- \
flutter test integration_test

# runs "flutter pub <arg(s)>" in all packages
pub: melos exec -c 1 -- flutter pub "$@"

# run tests in all packages
test: >
melos exec -c 1 --fail-fast --dir-exists=test -- \
melos exec -c 1 --dir-exists=test -- \
flutter test

# run pub upgrade in all packages
Expand Down
2 changes: 1 addition & 1 deletion packages/ubuntu_service/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:
sdk: '>=3.0.0 <4.0.0'

dependencies:
get_it: ^7.4.1
get_it: '>=7.4.1 <7.6.8'
meta: ^1.11.0

dev_dependencies:
Expand Down
41 changes: 26 additions & 15 deletions packages/ubuntu_widgets/lib/src/log_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@ class LogView extends StatefulWidget {
/// Creates a log view. A stream of [log] lines is required.
const LogView({
required this.log,
this.maxLines,
this.padding,
this.decoration,
this.background,
this.style,
this.scrollController,
super.key,
});

/// The stream of log lines to show.
final Stream<String> log;

/// See [TextField.maxLines]
final int? maxLines;

/// Padding around the log text.
final EdgeInsetsGeometry? padding;

Expand All @@ -33,14 +30,17 @@ class LogView extends StatefulWidget {
/// See [TextField.style]
final TextStyle? style;

/// See [TextField.scrollController]
final ScrollController? scrollController;

@override
State<LogView> createState() => _LogViewState();
}

class _LogViewState extends State<LogView> {
StreamSubscription<String>? _subscription;
final _controller = TextEditingController();
final _scrollController = ScrollController();
late final _scrollController = widget.scrollController ?? ScrollController();

@override
void initState() {
Expand Down Expand Up @@ -95,16 +95,27 @@ class _LogViewState extends State<LogView> {

@override
Widget build(BuildContext context) {
return Container(
padding: widget.padding,
decoration: widget.background,
child: TextField(
controller: _controller,
decoration: widget.decoration,
maxLines: widget.maxLines,
readOnly: true,
scrollController: _scrollController,
style: widget.style,
final contentPadding =
(widget.decoration?.contentPadding ?? EdgeInsets.zero)
.add(widget.padding ?? EdgeInsets.zero);
final decoration = (widget.decoration ?? const InputDecoration())
.copyWith(contentPadding: contentPadding);

return DecoratedBox(
decoration: widget.background ?? const BoxDecoration(),
child: Scrollbar(
key: const ValueKey('LogViewScrollbar'),
thumbVisibility: true,
controller: _scrollController,
child: SingleChildScrollView(
controller: _scrollController,
child: TextField(
controller: _controller,
decoration: decoration,
readOnly: true,
style: widget.style,
),
),
),
);
}
Expand Down
37 changes: 25 additions & 12 deletions packages/ubuntu_widgets/test/log_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ import 'package:ubuntu_widgets/ubuntu_widgets.dart';
void main() {
testWidgets('append lines', (tester) async {
final log = StreamController<String>(sync: true);
final scrollController = ScrollController();

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: LogView(log: log.stream, maxLines: 2),
body: SizedBox(
height: 5,
child: LogView(
log: log.stream,
scrollController: scrollController,
),
),
),
),
);
Expand All @@ -23,26 +30,23 @@ void main() {
final controller = textField.controller;
expect(controller, isNotNull);

final scrollController = textField.scrollController;
expect(scrollController, isNotNull);

log.add('line 1');
await tester.pump();
expect(controller!.text, equals('line 1'));
expect(scrollController!.position.extentAfter, equals(0.0));
expect(scrollController.position.maxScrollExtent, equals(0.0));
expect(scrollController.position.extentAfter, equals(0.0));
expect(scrollController.position.maxScrollExtent, isPositive);

log.add('line 2');
await tester.pump();
expect(controller.text, equals('line 1\nline 2'));
expect(scrollController.position.extentAfter, equals(0.0));
expect(scrollController.position.maxScrollExtent, equals(0.0));
expect(scrollController.position.maxScrollExtent, isPositive);

log.add('line 3');
await tester.pump();
await tester.pumpAndSettle();
expect(controller.text, equals('line 1\nline 2\nline 3'));
expect(scrollController.position.extentAfter, equals(0.0));
expect(scrollController.position.maxScrollExtent, greaterThan(0.0));
expect(scrollController.position.maxScrollExtent, isPositive);
});

testWidgets('rebuild with different stream', (tester) async {
Expand Down Expand Up @@ -78,7 +82,14 @@ void main() {
}

await tester.pumpWidget(
MaterialApp(home: Scaffold(body: LogView(log: log.stream, maxLines: 2))),
MaterialApp(
home: Scaffold(
body: SizedBox(
height: 20,
child: LogView(log: log.stream),
),
),
),
);

FlutterErrorDetails? error;
Expand All @@ -99,7 +110,7 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: LogView(log: log.stream, maxLines: 2),
body: SizedBox(height: 20, child: LogView(log: log.stream)),
),
),
);
Expand All @@ -109,7 +120,9 @@ void main() {
final controller = textField.controller;
expect(controller, isNotNull);

final scrollController = textField.scrollController;
final scrollView = tester
.widget<SingleChildScrollView>(find.byType(SingleChildScrollView));
final scrollController = scrollView.controller;
expect(scrollController, isNotNull);

for (var i = 1; i < 6; i++) {
Expand Down