Flutter – Implémentation de Pull to Refresh

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return MaterialApp(

      title: 'GeeksforGeeks',

      

      debugShowCheckedModeBanner: false,

      theme: ThemeData(

        primarySwatch: Colors.green,

      ),

      home: HomePage(),

    );

  }

}

class HomePage extends StatefulWidget {

  @override

  _HomePageState createState() => _HomePageState();

}

class _HomePageState extends State<HomePage> {

  

  

  

  GlobalKey<ScaffoldState> _scaffoldKey;

  

  List<String> _demoData;

  

  

  @override

  void initState() {

    

    _demoData = [

      "Flutter",

      "React Native",

      "Cordova/ PhoneGap",

      "Native Script"

    ];

    _scaffoldKey = GlobalKey();

    super.initState();

  }

  

  @override

  void dispose() {

    

    _scaffoldKey?.currentState?.dispose();

    super.dispose();

  }

  @override

  Widget build(BuildContext context) {

    return SafeArea(

      child: Scaffold(

        key: _scaffoldKey,

        appBar: AppBar(

          title: const Text('GeeksforGeeks'),

        ),

        

        body: RefreshIndicator(

          child: ListView.builder(

            itemBuilder: (ctx, idx) {

              

              return Card(

                child: ListTile(

                  title: Text(_demoData[idx]),

                ),

              );

            },

            

            itemCount: _demoData.length,

            

            

            physics: const AlwaysScrollableScrollPhysics(),

          ),

          

          

          onRefresh: () {

            return Future.delayed(

              Duration(seconds: 1),

              () {

                

                

                

                

                

                

                setState(() {

                  _demoData.addAll(["Ionic", "Xamarin"]);

                });

                

                _scaffoldKey.currentState.showSnackBar(

                  SnackBar(

                    content: const Text('Page Refreshed'),

                  ),

                );

              },

            );

          },

        ),

      ),

    );

  }

}

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Aller en haut