Hello! How to use future builder inside sliverlist wit silver app bar

1,782 views
Skip to first unread message

Attaullah Khan

unread,
May 22, 2020, 11:31:49 AM5/22/20
to Flutter Development (flutter-dev)
Here is my code,, im getting error integerdivisionby zero exception,,,so i need to use future builder inside that sliver list,please help

import 'package:flutter/material.dart';
import 'package:fffdesign/util_apis/api_util.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:transparent_image/transparent_image.dart';

final backgroundColor = Color(0xFF435719);
  String address="",open="",close="",banner="mybanner.png",restaurantname="",logo='mybanner.png';

class SingleRestaurant extends StatefulWidget {
  final String resslug;
  SingleRestaurant({Key key,this.resslug}) : super(key: key);

  @override
  _SingleRestaurantState createState() => _SingleRestaurantState();
}

class _SingleRestaurantState extends State<SingleRestaurant> {
  List deal=[];
 Future getsingleres() async {
    final String url = ApiUtility.Main_Url+ApiUtility.Single_res+widget.resslug;
    var res = await http .get(Uri.encodeFull(url), headers: {"Accept""application/json"});
    if (res.statusCode == 200) {
        var resBody = json.decode(res.body);
        Map data=resBody;
        setState(() {
         banner= data['users']['banner'];
         logo= data['users']['logo'];
          restaurantname=data['restaurant_name'];
          address=data['address'];
          open=data['open'];
          close=data['close'];
         deal=resBody['deals'];
        });
        return resBody['deals'];
       }else {
      // If that call was not successful, throw an error.
      throw Exception('Failed to load ');
    }

}
@override
 void initState() {
   super.initState();
this.getsingleres();
}
  @override
  Widget build(BuildContext context) {
     return Scaffold(
        body: Column(
          children: [
            Expanded(
              child: CustomScrollView(
                slivers: [
                  HeaderWidget(),
                  AlbumWidget(),
                  SliverToBoxAdapter(
                    child: Padding(
                      padding: const EdgeInsets.all(15.0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Text(
                            'OUR DEALS',
                            style: Theme.of(context).textTheme.headline3,
                          ),
                          Text(
                            '',
                            style: TextStyle(
                                color: Colors.pinkAccent, fontSize: 13),
                          )
                          
                        ],
                      ),
                    ),
                  ),
                 SliverList(
                  

                    delegate: SliverChildBuilderDelegate((_, index) {
                    final Map d = deal[index % deal.length];
                   return deal.isEmpty ? Center(child: Text('Empty')) :  Column(
                     children: <Widget>[
      Container(
      padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
      child: Card(
        elevation: 2.0,
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
          child: Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                Container(
      child: Column(
        children: <Widget>[
          Image(image: NetworkImage(ApiUtility.Assest_Url+d['dealimage']),
            height: 100.0
          )
        ],
      ),
    ),
                Expanded(
                                  child: Container(
      padding: EdgeInsets.only(left: 10.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text(d['deal_title'].toString(),overflow: TextOverflow.ellipsis,  maxLines: 3,style: Theme.of(context).textTheme.bodyText2),
          Row(
            children: <Widget>[
              Icon(Icons.location_on),
          Expanded(child: Text(d['deal_title'].toString(),overflow: TextOverflow.ellipsis,  maxLines: 2,style: Theme.of(context).textTheme.bodyText2)),
            ],
          ),
          Row(
            children: <Widget>[
              Icon(Icons.restaurant_menu),
              Text('Menu Items: 25')
            ],
          )
        ],
      ),
    ),
                ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                  RaisedButton(
                    onPressed: () {
                     
                    },
                    color: Colors.red,
                    textColor: Colors.white,
                    child: Text('add to cart'),)
                ],
              )
            ],
          ),
        ),
      ),
    )
                        ]
                      
                      );
                    }, childCount: 20),
                  ),
                ],
              ),
            ),
          ],
        ),
      );
  }
}

class AlbumWidget extends StatelessWidget {
  const AlbumWidget({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SliverToBoxAdapter(
      child: Padding(
        padding: const EdgeInsets.all(15.0),
        child: Row(
          children: [

            Image.network(
              ApiUtility.Assest_Url+logo,
              height: 90,
            ),
            const SizedBox(
              width: 12,
            ),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    'Location: ${address??''}',
                    style: TextStyle(
                      color: Colors.red,
                    ),
                  ),
                  RaisedButton(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(25),
                      
                      ),
                      color: Colors.red,
                      child: Text('TIMING Open $open - Close $close'),
                      onPressed: () => null)
                ],
              ),
            ),

           


          ],
        ),
      ),
    );
  }
}



class HeaderWidget extends StatelessWidget {
  const HeaderWidget({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SliverAppBar(
      expandedHeight: 300,
      stretch: true,
      backgroundColor: Color(0xFF0B0B14),
      flexibleSpace: FlexibleSpaceBar(
        stretchModes: [
          StretchMode.zoomBackground,
        ],
        background: Stack(
          fit: StackFit.expand,
          children: [
            Positioned.fill(
              child:FadeInImage.memoryNetwork(
                       placeholder: kTransparentImage,
                        image:ApiUtility.Assest_Url+banner,fit:BoxFit.cover,
              ),
            ),
            Align(
              alignment: Alignment.center,
              child: Padding(
                padding: const EdgeInsets.all(15.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      restaurantname??'',
                      style: Theme.of(context).textTheme.display1.copyWith(
                            color: Colors.white,
                            fontWeight: FontWeight.bold,
                          ),
                    ),
                  
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );

}


  
}

Souvik Dutta

unread,
May 23, 2020, 1:13:50 AM5/23/20
to Attaullah Khan, Flutter Development (flutter-dev)
You might be able to use future builder inside sliver list as you would do with any other widget. Just pass it as a child to sliver list. When using a future builder you will have to set a future which will be a function that returns a future object. Then you will have to specify a builder property which takes in a function which in turn takes two argument ( context and snapshot), snapshot is the future or the data you are passing in future. Now you are all set to use snapshot.data in the places where you wish to use your future and extract data from it. I hope this helps.

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/c6adc5c8-ecf7-43de-b03b-eb0338149006%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages