Dart - SilverAppBar

박정규·2022년 1월 27일
0
post-thumbnail

SilverAppBar란?

앱에 스크롤이 될 때마다 형태가 바뀌거나 사라지게 할 수 이있는 에니메이션 헤더를 설정할 때 SilverAppBar로 설정할 수 있다.

어떻게 생성할까?

다양한 설정 가능한 속성들이 있는데, 예시 코드를 통해 살펴보자.

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

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

  static const String _title = 'Flutter Code Sample';

  
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);

  
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  bool _pinned = true;
  bool _snap = false;
  bool _floating = false;

// [SliverAppBar]s are typically used in [CustomScrollView.slivers], which in
// turn can be placed in a [Scaffold.body].
  
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            pinned: _pinned,
            snap: _snap,
            floating: _floating,
            expandedHeight: 160.0,  // appBar가 얼마나 늘어날지 결정
            flexibleSpace: const FlexibleSpaceBar(  // expandedHeight 안에서 움직일 수 있도록 설정해준다.
              title: Text('SliverAppBar'),
              background: FlutterLogo(),
            ),
          ),
          const SliverToBoxAdapter(
            child: SizedBox(
              height: 20,
              child: Center(
                child: Text('Scroll to see the SliverAppBar in effect.'),
              ),
            ),
          ),
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) {
                return Container(
                  color: index.isOdd ? Colors.white : Colors.black12,
                  height: 100.0,
                  child: Center(
                    child: Text('$index', textScaleFactor: 5),
                  ),
                );
              },
              childCount: 20,
            ),
          ),
        ],
      ),
    );
  }
}

우리는 평소 appBar만 설정했을 때와는 다르게 움직임 변화가 있는 것을 확인할 수 있을 것이다.

profile
초보 개발자

0개의 댓글