2022年6月9日星期四

scrollController get offset (slow!!!! _scrollController.addListener)

 _scrollController.addListener is too slow!!!! use timer.periodic to do parallax scrolling

 import 'package:flutter/material.dart';


void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);

final String title;

@override
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

final ScrollController _scrollController = ScrollController(
initialScrollOffset: 0.0,
keepScrollOffset: true,
);



@override
void initState() {
super.initState();
print('init');
_scrollController.addListener(() { //very slow
print('_scrollController.offset: '+_scrollController.offset.toString());

});
}

@override
Widget build(BuildContext context) {

return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
width: double.infinity,
height: double.infinity,
child: SingleChildScrollView(
controller: _scrollController,
child: Column(children: [
Text('start', style: TextStyle(height: 2, fontSize: 30)),
Text('1234', style: TextStyle(height: 2, fontSize: 30)),
Text('1234', style: TextStyle(height: 2, fontSize: 30)),
Text('1234', style: TextStyle(height: 2, fontSize: 30)),
Text('1234', style: TextStyle(height: 2, fontSize: 30)),
Text('1234', style: TextStyle(height: 2, fontSize: 30)),
Text('1234', style: TextStyle(height: 2, fontSize: 30)),
Text('1234', style: TextStyle(height: 2, fontSize: 30)),
Text('1234', style: TextStyle(height: 2, fontSize: 30)),
Text('1234', style: TextStyle(height: 2, fontSize: 30)),
Text('1234', style: TextStyle(height: 2, fontSize: 30)),
Text('1234', style: TextStyle(height: 2, fontSize: 30)),
Text('1234', style: TextStyle(height: 2, fontSize: 30)),
Text('1234', style: TextStyle(height: 2, fontSize: 30)),
Text('1234', style: TextStyle(height: 2, fontSize: 30)),
Text('1234', style: TextStyle(height: 2, fontSize: 30)),
Text('1234', style: TextStyle(height: 2, fontSize: 30)),
Text('1234', style: TextStyle(height: 2, fontSize: 30)),
Text('1234', style: TextStyle(height: 2, fontSize: 30)),
Text('1234', style: TextStyle(height: 2, fontSize: 30)),
Text('end', style: TextStyle(height: 2, fontSize: 30)),
]))),
);
}
}

沒有留言:

發佈留言