목차
- 프로젝트 생성 및 리스트뷰 생성
- 애니메이션 구현
- 스크롤 이동 (완성)
1. 프로젝트 생성 및 리스트뷰 생성
* 소스코드 import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Example',
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.blue),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
Key? key,
}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Scroll To Top Example')),
body: Center(
child: ListView(
children: <Widget>[
for (int i = 0; i < 20; i++)
Container(
width: 200, height: 200,
color: Colors.red[((i % 4) + 1) * 100],
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: const Icon(Icons.arrow_upward_rounded),
),
);
}
}
|
* 실행화면 |
2. 애니메이션 구현
* 소스코드
import 'package:flutter/material.dart'; void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Example',
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.blue),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
Key? key,
}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final ScrollController scrollController = ScrollController();
double _opacity = 0.0;
@override
void initState() {
// Scroll Event
scrollController.addListener(() {
scrollListener();
});
super.initState();
}
@override
void dispose() {
scrollController.dispose();
super.dispose();
}
// Scroll Method
scrollListener() async {
if (scrollController.offset > 0) {
// Show Button
setState(() {
if (_opacity != 1.0) _opacity = 1.0;
});
} else {
// Hide Button
setState(() {
if (_opacity != 0.0) _opacity = 0.0;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Scroll To Top Example')),
body: Center(
child: ListView(
// Link
controller: scrollController,
children: <Widget>[
for (int i = 0; i < 20; i++)
Container(
width: 200, height: 200,
color: Colors.red[((i % 4) + 1) * 100],
),
],
),
),
floatingActionButton: AnimatedOpacity(
duration: const Duration(milliseconds: 700),
curve: Curves.easeIn,
// View
opacity: _opacity,
child: FloatingActionButton(
onPressed: () {},
child: const Icon(Icons.arrow_upward_rounded),
),
),
);
}
}
|
* 실행화면 |
3. 스크롤 이동 (완성)
* 소스코드
import 'package:flutter/material.dart'; void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Example',
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.blue),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
Key? key,
}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final ScrollController scrollController = ScrollController();
double _opacity = 0.0;
@override
void initState() {
// Scroll Event
scrollController.addListener(() {
scrollListener();
});
super.initState();
}
@override
void dispose() {
scrollController.dispose();
super.dispose();
}
// Scroll Method
scrollListener() async {
if (scrollController.offset > 0) {
// Show Button
setState(() {
if (_opacity != 1.0) _opacity = 1.0;
});
} else {
// Hide Button
setState(() {
if (_opacity != 0.0) _opacity = 0.0;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Scroll To Top Example')),
body: Center(
child: ListView(
// Link
controller: scrollController,
children: <Widget>[
for (int i = 0; i < 20; i++)
Container(
width: 200, height: 200,
color: Colors.red[((i % 4) + 1) * 100],
),
],
),
),
floatingActionButton: AnimatedOpacity(
duration: const Duration(milliseconds: 700),
curve: Curves.easeIn,
// View
opacity: _opacity,
child: FloatingActionButton(
!@# onPressed: () { setState(() {
scrollController.animateTo(
0,
duration: const Duration(milliseconds: 500),
curve: Curves.easeIn,
);
});
},
child: const Icon(Icons.arrow_upward_rounded),
),
),
);
}
}
|
* 실행화면 |
'App > Flutter' 카테고리의 다른 글
[ FLUTTER ] Android 빌드 서명 세팅 (0) | 2022.03.21 |
---|---|
[ FLUTTER ] Flexible ( Expanded ) 차이 (0) | 2022.03.16 |
[ FLUTTER ] GetX 페이지 누적 이동(preventDuplicates) (0) | 2022.02.07 |
[ FLUTTER ] launcher icon plugin (0) | 2022.02.03 |
[ FLUTTER ] 커스텀위젯 value값 주고받기 예시 (0) | 2021.12.27 |