Tabarek Ghassan
Tabarek Ghassan

Reputation: 806

FixedExtentScrollPhysics Flutter

I want to put my get response JSON data as FixedExtentScrollPhysics ViewList in My code whatever I tried some error appears and what I did actually is that I have a FixedExtentScrollPhysics code and a get response code and I mixed them but errors appeared

my Code is that

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

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

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  final FixedExtentScrollController fixedExtentScrollController =
  new FixedExtentScrollController();

  List data;


  final String url = "https://jsonplaceholder.typicode.com/todos";

  Future<String> getTitle() async {
    var res = await http
        .get(Uri.encodeFull(url), headers: {"Accept": "application/json"});

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

    return "Success!";
  }

  @override

  Widget build(BuildContext context) {

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.deepPurple,
          title: const Text('List'),
        ),
        body:new Expanded(
          flex: 1,
          child:ListView.builder(
          controller: fixedExtentScrollController,
          physics: FixedExtentScrollPhysics(),
          itemCount: data == null ? 0 : data.length,
          itemBuilder: (BuildContext context, int index) {

            return new Container(
              child: Center(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: <Widget>[
                    Card(
                      child: Container(
                          padding: EdgeInsets.all(15.0),
                          child: Row(
                            children: <Widget>[
                              Text("Title: "),
                              Text(data[index]["title"],
                                  style: TextStyle(
                                      fontSize: 11.0, color: Colors.black87)),
                            ],
                          )),
                    ),
                  ],
                ),
              ),
            );
          },
        ),
      ),
    ),
    );
  }

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

any help?

Upvotes: 3

Views: 3864

Answers (1)

diegoveloper
diegoveloper

Reputation: 103551

Check the error log :

 FixedExtentScrollController can only be used with ListWheelScrollViews

So you can't use FixedExtentScrollController with ListView, you need to use ListWheelScrollViews

This is your code fixed:

     @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              backgroundColor: Colors.deepPurple,
              title: const Text('List'),
            ),
            body: data == null
                ? Center(
                    child: CircularProgressIndicator(),
                  )
                : ListWheelScrollView(
                    itemExtent: 100.0,
                    controller: fixedExtentScrollController,
                    children: data
                        .map((val) => Container(
                              child: Center(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.stretch,
                                  children: <Widget>[
                                    Card(
                                      child: Container(
                                          padding: EdgeInsets.all(15.0),
                                          child: Row(
                                            children: <Widget>[
                                              Text("Title: "),
                                              Expanded(
                                                child: Text(val["title"],
                                                    style: TextStyle(
                                                        fontSize: 11.0,
                                                        color: Colors.black87)),
                                              ),
                                            ],
                                          )),
                                    ),
                                  ],
                                ),
                              ),
                            ))
                        .toList()),
          ),
        );
      }

Upvotes: 3

Related Questions