0

I followed these line of code somewhere in youtube, displaying data was fine, and I added a refresh indicator.

Now I have these problems: how can I pass the data in details screen when I tap one of the list? And it needs to show loading text while fetching the data.

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;

import 'main_drawer.dart';

class NotificationScr extends StatefulWidget {
  @override
  _NotificationScrState createState() => _NotificationScrState();
}

class _NotificationScrState extends State<NotificationScr> {

  final String url = "https://ikns.info/api/announcement_data.php";
  List data;

  Future<String> getSWData() async {
    refreshKey.currentState?.show(atTop: false);
    //await Future.delayed(Duration(seconds: 2));
    var res = await http.get(Uri.parse(url), headers: {"Accept": "application/json"});

    setState(() {
       data = json.decode(res.body);  
      });



    return null;
  }

    var refreshKey = GlobalKey<RefreshIndicatorState>();

    @override
    void initState() {
      super.initState();
        this.getSWData();
    }

    //initialize the icon in appbar
    Icon actionIcon = new Icon(Icons.more_vert);

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor: Colors.white,
      appBar: new AppBar(
          centerTitle: true,  //centering the title
          title: new Text('Notification', style: TextStyle(color: Colors.white)),
          backgroundColor: Colors.red,
          actions: <Widget>[
            new IconButton(
              icon: actionIcon,
              onPressed:(){
                  Navigator.of(context).pushNamed('emailRoute');
              },
            ),
          ],
        ),
      drawer: MainDrawer(),


      body: RefreshIndicator(
        key: refreshKey,


        child: ListView.builder(


           itemCount: data == null ? 0 : data.length,
            itemBuilder: (BuildContext context, int index) {
          return Container(
            child: Center(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                   Container(
                        padding: EdgeInsets.all(15.0),
                        child: Row(
                          children: <Widget>[
                            Text(data[index]["title"],
                                style: TextStyle(
                                    fontSize: 18.0, color: Colors.black87)),
                          ],
                        )),
                ],
              ),
            ),
          );
        },
        ),
        onRefresh: getSWData,
      ),
    );
  }
}
E_net4
  • 27,810
  • 13
  • 101
  • 139
erwin
  • 99
  • 2
  • 9

0 Answers0