Giter Site home page Giter Site logo

Comments (7)

arteminthesky avatar arteminthesky commented on May 12, 2024

I seem to have found a similar problem, but one is closed by the author and it is not entirely clear how he solved it.
#42

I would be very happy if you could help me find a way to change the number of elements in the list so that this would not cause problems. (I suppose if the new list has more elements than it was, this should not cause a problem)

from flutter.widgets.

AurelianTimu avatar AurelianTimu commented on May 12, 2024

Having the same issue.

from flutter.widgets.

vabawen avatar vabawen commented on May 12, 2024

+1

Code Sample

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

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

class _MyHomePageState extends State<MyHomePage> {
  List<String> titles = [];
  ItemScrollController scrollController = ItemScrollController();

  @override
  void initState() {
    super.initState();
    while(titles.length<30) {
      titles.add('Some Text ${titles.length}');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Expanded(
                child: ScrollablePositionedList.builder(
                  itemScrollController: scrollController,
                  itemBuilder: (context, index) => Container(
                    alignment: Alignment.bottomCenter,
                    padding: EdgeInsets.symmetric(vertical: 10),
                    child: Text(titles[index]),
                  ),
                  itemCount: titles.length,
                )
            ),
            Divider(),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                OutlineButton(
                  child: Text('1.Jump to last item'),
                  onPressed: () {
                    scrollController.jumpTo(index: titles.length-1);
                  },
                ),
                OutlineButton(
                  child: Text('2.Delete the last item'),
                  onPressed: () {
                    titles.removeAt(titles.length-1);
                    setState(() {

                    });
                  },
                ),
              ],
            )
          ],
        ),
      ), 
    );
  }
}

from flutter.widgets.

AurelianTimu avatar AurelianTimu commented on May 12, 2024

@iKrupp

I got it working like follows :

I have a variable _deletedItems that I use to keep track of the elements that are removed from entityList. When an element is removed, I just increment _deletedItems.
When index becomes equal to entityList.length and _deletedItems != 0 it means that an element was removed from the list, hence a Container() is returned.

In my case, the code looks something like this :

....

  @override
  void didUpdateWidget(CustomStatefulWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.entityList.length > widget.entityList.length) {
      // An item was removed/deleted from the list
        _deletedItems += 1;
    }
    entityList = widget.entityList;
  }
....
itemBuilder: (BuildContext context, int index) {
     if (index <= entityList.length ) {
      final entity = entityList[index];
      return CustomWidget(..); 
    } else if (index == entityList.length && _deletedItems == 0) {
      final entity = entityList[index];
      return CustomWidget(..); 
    } else {
      return Container()
    }
 },
....

from flutter.widgets.

arteminthesky avatar arteminthesky commented on May 12, 2024

@vabawen thank you for example! Could you also duplicate it here: #42

from flutter.widgets.

arteminthesky avatar arteminthesky commented on May 12, 2024

@AurelianTimu this was my first thought - returning the stub to an inaccessible index. (for this, it seems, you do not even need to count the number of deleted elements). However, I tried Offstage instead of a container. I will try the container, however this is still a workaround. It would be better if this were fixed on the side of the plugin

from flutter.widgets.

AurelianTimu avatar AurelianTimu commented on May 12, 2024

Correct, you dont need the exact count of deleted items in this case. I am using the count for a different purpose. I forgot to mention it in my comment.

from flutter.widgets.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.