[flutter] scroll 위치값 얻어오기 (flutter listview track scroll position)

adbr·2020년 6월 23일
1

Scroll Controller

final ScrollController _scrollController = ScrollController();

  
  void initState() {
    super.initState();
    _scrollController.addListener(() {
      print('offset = ${_scrollController.offset}');
    });
  }

  
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  
  Widget build(BuildContext context) {
    return Scaffold(
      body: _buildBody(),
    );
  }

  Widget _buildBody() {
    return ListView(
      scrollDirection: Axis.horizontal,
      controller: _scrollController,
      children: <Widget>[
        Container(
          width: MediaQuery.of(context).size.width,
          child: Image.asset(
            'assets/IMG_0878.JPG',
            fit: BoxFit.cover,
            alignment: Alignment.topLeft,
          ),
        ),
        Container(
          width: MediaQuery.of(context).size.width,
          child: Image.asset(
            'assets/IMG_0879.JPG',
            fit: BoxFit.cover,
            alignment: Alignment.topLeft,
          ),
        ),
        Container(
          width: MediaQuery.of(context).size.width,
          child: Image.asset(
            'assets/IMG_0880.JPG',
            fit: BoxFit.cover,
            alignment: Alignment.topLeft,
          ),
        ),
      ],
    );
  }

console

I/flutter (26962): offset = 226.30580357142867
I/flutter (26962): offset = 213.64955357142867
I/flutter (26962): offset = 199.08482142857153
I/flutter (26962): offset = 183.9174107142858
I/flutter (26962): offset = 166.9419642857144
I/flutter (26962): offset = 149.0625000000001
I/flutter (26962): offset = 131.7857142857144
I/flutter (26962): offset = 9.265577463252342
I/flutter (26962): offset = 0.0

cf

https://stackoverflow.com/questions/43881103/current-scroll-offset-inside-a-flutter-listview-sliverlist-etc

profile
𝙸 𝚊𝚖 𝚊 𝚌𝚞𝚛𝚒𝚘𝚞𝚜 𝚍𝚎𝚟𝚎𝚕𝚘𝚙𝚎𝚛 𝚠𝚑𝚘 𝚎𝚗𝚓𝚘𝚢𝚜 𝚍𝚎𝚏𝚒𝚗𝚒𝚗𝚐 𝚊 𝚙𝚛𝚘𝚋𝚕𝚎𝚖. 🇰🇷👩🏻‍💻

0개의 댓글