Skip to content

Commit

Permalink
Apply name styling to auto-scroll text (#1263)
Browse files Browse the repository at this point in the history
* Apply name styling to auto-scroll

* Use marquee widget directly instead of via package
  • Loading branch information
micahmo authored Apr 2, 2024
1 parent 8770d2c commit 5898954
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 20 deletions.
4 changes: 4 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,7 @@ specific requirements.
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU AGPL, see
<https://www.gnu.org/licenses/>.

Portions of this software are copyright of their respective authors and released
under the MIT license:
- marquee_widget.dart, Copyright (c) 2018 Marcel Garus
23 changes: 12 additions & 11 deletions lib/shared/input_dialogs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:text_scroll/text_scroll.dart';
import 'package:collection/collection.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:thunder/account/bloc/account_bloc.dart';
Expand All @@ -19,6 +18,8 @@ import 'package:thunder/feed/utils/community.dart';
import 'package:thunder/shared/avatars/community_avatar.dart';
import 'package:thunder/shared/dialogs.dart';
import 'package:thunder/shared/avatars/user_avatar.dart';
import 'package:thunder/shared/full_name_widgets.dart';
import 'package:thunder/shared/marquee_widget.dart';
import 'package:thunder/utils/global_context.dart';
import 'package:thunder/utils/instance.dart';
import 'package:thunder/utils/numbers.dart';
Expand Down Expand Up @@ -92,11 +93,11 @@ Widget buildUserSuggestionWidget(BuildContext context, PersonView payload, {void
),
subtitle: Semantics(
excludeSemantics: true,
child: TextScroll(
generateUserFullName(context, payload.person.name, fetchInstanceNameFromUrl(payload.person.actorId)),
delayBefore: const Duration(seconds: 2),
pauseBetween: const Duration(seconds: 3),
velocity: const Velocity(pixelsPerSecond: Offset(50, 0)),
child: Marquee(
animationDuration: const Duration(seconds: 2),
backDuration: const Duration(seconds: 2),
pauseDuration: const Duration(seconds: 1),
child: UserFullNameWidget(context, payload.person.name, fetchInstanceNameFromUrl(payload.person.actorId)),
),
),
),
Expand Down Expand Up @@ -197,11 +198,11 @@ Widget buildCommunitySuggestionWidget(BuildContext context, CommunityView payloa
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextScroll(
generateCommunityFullName(context, payload.community.name, fetchInstanceNameFromUrl(payload.community.actorId)),
delayBefore: const Duration(seconds: 2),
pauseBetween: const Duration(seconds: 3),
velocity: const Velocity(pixelsPerSecond: Offset(50, 0)),
Marquee(
animationDuration: const Duration(seconds: 2),
backDuration: const Duration(seconds: 2),
pauseDuration: const Duration(seconds: 1),
child: CommunityFullNameWidget(context, payload.community.name, fetchInstanceNameFromUrl(payload.community.actorId)),
),
Row(
children: [
Expand Down
70 changes: 70 additions & 0 deletions lib/shared/marquee_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
library marquee_widget;

import 'package:flutter/material.dart';

enum DirectionMarguee { oneDirection, twoDirection }

class Marquee extends StatelessWidget {
final Widget child;
final TextDirection textDirection;
final Axis direction;
final Duration animationDuration, backDuration, pauseDuration;
final DirectionMarguee directionMarguee;
final Cubic forwardAnimation;
final Cubic backwardAnimation;
final bool autoRepeat;
Marquee({
super.key,
required this.child,
this.direction = Axis.horizontal,
this.textDirection = TextDirection.ltr,
this.animationDuration = const Duration(milliseconds: 5000),
this.backDuration = const Duration(milliseconds: 5000),
this.pauseDuration = const Duration(milliseconds: 2000),
this.directionMarguee = DirectionMarguee.twoDirection,
this.forwardAnimation = Curves.easeIn,
this.backwardAnimation = Curves.easeOut,
this.autoRepeat = true,
});

final ScrollController _scrollController = ScrollController();

scroll(bool repeated) async {
do {
if (_scrollController.hasClients) {
await Future.delayed(pauseDuration);
if (_scrollController.hasClients) await _scrollController.animateTo(_scrollController.position.maxScrollExtent, duration: animationDuration, curve: forwardAnimation);
await Future.delayed(pauseDuration);
if (_scrollController.hasClients) {
switch (directionMarguee) {
case DirectionMarguee.oneDirection:
_scrollController.jumpTo(
0.0,
);
break;
case DirectionMarguee.twoDirection:
await _scrollController.animateTo(0.0, duration: backDuration, curve: backwardAnimation);
break;
}
}
repeated = autoRepeat;
} else {
await Future.delayed(pauseDuration);
}
} while (repeated);
}

@override
Widget build(BuildContext context) {
bool repeated = true;
scroll(repeated);
return Directionality(
textDirection: textDirection,
child: SingleChildScrollView(
scrollDirection: direction,
controller: _scrollController,
child: child,
),
);
}
}
8 changes: 0 additions & 8 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1631,14 +1631,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.6.1"
text_scroll:
dependency: "direct main"
description:
name: text_scroll
sha256: "7869d86a6fdd725dee56bdd150216a99f0372b82fbfcac319214dbd5f36e1908"
url: "https://pub.dev"
source: hosted
version: "0.2.0"
timezone:
dependency: transitive
description:
Expand Down
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ dependencies:
dart_ping: ^9.0.0
dart_ping_ios: ^4.0.0
flutter_typeahead: ^5.2.0
text_scroll: ^0.2.0
sliver_tools: ^0.2.12
screenshot: ^2.1.0
html_unescape: ^2.0.0
Expand Down

0 comments on commit 5898954

Please sign in to comment.