Skip to content
Open
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
28 changes: 14 additions & 14 deletions lib/marquee_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import 'package:flutter/material.dart';
class MarqueeWidget extends StatefulWidget{

final String text;
final TextStyle textStyle;
final TextStyle? textStyle;
///滚动方向,水平或者垂直
final Axis scrollAxis;
///空白部分占控件的百分比
final double ratioOfBlankToScreen;

MarqueeWidget({
@required this.text,
required this.text,
this.textStyle,
this.scrollAxis:Axis.horizontal,
this.ratioOfBlankToScreen:0.25,
}) :assert(text!=null,);
});

@override
State<StatefulWidget> createState() {
Expand All @@ -27,11 +27,11 @@ class MarqueeWidget extends StatefulWidget{

class MarqueeWidgetState extends State<MarqueeWidget> with SingleTickerProviderStateMixin{

ScrollController scroController;
double screenWidth;
double screenHeight;
ScrollController? scroController;
late double screenWidth;
late double screenHeight;
double position=0.0;
Timer timer;
late Timer timer;
final double _moveDistance=3.0;
final int _timerRest=100;
GlobalKey _key=GlobalKey();
Expand All @@ -41,29 +41,29 @@ class MarqueeWidgetState extends State<MarqueeWidget> with SingleTickerProviderS
void initState() {
super.initState();
scroController=new ScrollController();
WidgetsBinding.instance.addPostFrameCallback((callback){
WidgetsBinding.instance!.addPostFrameCallback((callback){
startTimer();
});
}

void startTimer(){
double widgetWidth = _key.currentContext.findRenderObject().paintBounds.size.width;
double widgetHeight = _key.currentContext.findRenderObject().paintBounds.size.height;
double widgetWidth = _key.currentContext!.findRenderObject()!.paintBounds.size.width;
double widgetHeight = _key.currentContext!.findRenderObject()!.paintBounds.size.height;

timer=Timer.periodic(new Duration(milliseconds: _timerRest), (timer){
double maxScrollExtent=scroController.position.maxScrollExtent;
double pixels=scroController.position.pixels;
double maxScrollExtent=scroController!.position.maxScrollExtent;
double pixels=scroController!.position.pixels;
if(pixels+_moveDistance>=maxScrollExtent){
if(widget.scrollAxis==Axis.horizontal){
//TODO 我也看不懂怎么算的
position=(maxScrollExtent-screenWidth*widget.ratioOfBlankToScreen+widgetWidth)/2-widgetWidth+pixels-maxScrollExtent;
}else{
position=(maxScrollExtent-screenHeight*widget.ratioOfBlankToScreen+widgetHeight)/2-widgetHeight+pixels-maxScrollExtent;
}
scroController.jumpTo(position);
scroController!.jumpTo(position);
}
position+=_moveDistance;
scroController.animateTo(position,duration: new Duration(milliseconds: _timerRest),curve: Curves.linear);
scroController!.animateTo(position,duration: new Duration(milliseconds: _timerRest),curve: Curves.linear);
});
}

Expand Down
99 changes: 53 additions & 46 deletions pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,34 +1,55 @@
# Generated by pub
# See https://www.dartlang.org/tools/pub/glossary#lockfile
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0"
version: "2.6.1"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0"
characters:
dependency: transitive
description:
name: characters
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.4"
version: "1.1.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.2"
version: "1.2.0"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.0"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.14.11"
version: "1.15.0"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.2.0"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -43,37 +64,23 @@ packages:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.12.5"
version: "0.12.10"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.6"
version: "1.3.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.2"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.5.0"
quiver:
dependency: transitive
description:
name: quiver
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
version: "1.8.0"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -83,57 +90,57 @@ packages:
dependency: transitive
description:
name: source_span
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.5.5"
version: "1.8.1"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.9.3"
version: "1.10.0"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.0"
version: "2.1.0"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.4"
version: "1.1.0"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.0"
version: "1.2.0"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.2.4"
version: "0.3.0"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.6"
version: "1.3.0"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.8"
version: "2.1.0"
sdks:
dart: ">=2.2.0 <3.0.0"
dart: ">=2.12.0 <3.0.0"
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: marquee_flutter
description: A Marquee widght with ListView,Can scroll vertically or horizontally
version: 0.1.4
version: 1.0.0
author: baoolong <baoolong1987@gmail.com>
homepage: https://github.com/baoolong/MarqueeWidget

environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
sdk: '>=2.12.0 <3.0.0'

dependencies:
flutter:
Expand Down