name27 / flutter

0 stars 0 forks source link

ScrollController, jumpTo, ListView.builder #44

Open name27 opened 1 year ago

name27 commented 1 year ago

image

import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List animalList = ['강아지', '고양이', '앵무새', '토끼', '오리', '거위', '원숭이'];
  final ScrollController _scrollController= ScrollController();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: ListView.builder(
            controller: _scrollController,
            itemCount: animalList.length,
            itemBuilder: (BuildContext context, int index) {
              return Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  SizedBox(height: 300, child: Text(animalList[index])),
                ],
              );
            },
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: (){
            _scrollController.jumpTo(0);
            setState(() {});
          },
          child: Icon(Icons.vertical_align_top),
        ),
      ),
    );
  }
}