Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initState is being called on the Silidable child when it closes or opens #51

Closed
davidmartos96 opened this issue Feb 24, 2019 · 10 comments
Closed
Labels
1.0 test needed enhancement New feature or request

Comments

@davidmartos96
Copy link

Is this a bug? This behavior is not expected to happen. One example could be a network request inside the slidable's child, where you don't want to call this operation every time the user interacts with the slidable.

Here is a sample code.

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_slidable/flutter_slidable.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Sample",
      home: Scaffold(
        appBar: AppBar(
          title: Text("The child reloads when sliding"),
        ),
        body: Column(
          children: <Widget>[
            Slidable(
              child: StfulChild(),
              delegate: SlidableBehindDelegate(),
              actions: <Widget>[
                Icon(Icons.share),
              ],
              secondaryActions: <Widget>[
                Icon(Icons.add),
              ],
            )
          ],
        ),
      ),
    );
  }
}

class StfulChild extends StatefulWidget {
  _StfulChildState createState() => _StfulChildState();
}

class _StfulChildState extends State<StfulChild> {
  bool loading = true;

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration(seconds: 1)).then((_) {
      if (mounted)
        setState(() {
          loading = false;
        });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.green,
      height: 100,
      child: Center(
        child: loading ? CircularProgressIndicator() : Text("Loaded"),
      ),
    );
  }
}

@florianhaar
Copy link

Is there any progress for this bug? It is really anoying that initState is called 1-3 times while the item is slided.

@frankyvij
Copy link

Hello - can this please be prioritised? Its common to have animations in widgets and/or network request to get data. I have tried to find a workaround, but it seems impossible with current implementation.

@dineshsheoran24
Copy link

Any progress ??

@letsar
Copy link
Owner

letsar commented Nov 8, 2020

The widget is rebuilding because some widgets are removed/added in the tree during the animation. I will see if I can remove this behavior in my refactoring.

@husitawi
Copy link

For info, this behavior also happens with ValueListenableBuilder in the child's subtree. ( Many thanks for the package! )

@denisbabineau
Copy link

I've found that if you set a GlobalKey on your Slidable's child it fixes (works around?) this issue. My understanding is that it is caused by the child moving within the widget tree, setting a key allows it to reuse its state under a different parent as explained here:

https://www.youtube.com/watch?v=kn0EOS-ZiIc&feature=youtu.be&t=510

@letsar
Copy link
Owner

letsar commented Dec 11, 2020

I think this will be fixed in the upcoming version. Can someone test it with the revival branch?

@husitawi
Copy link

Ill try to test this ASAP

@ofoud2024
Copy link

Is this problem fixed. After some investigations, I found out that the problem is related to using InheritedWidget and changing the widget hierarchy when closing the slider. One potential solution would be to only use Stateful and Stateless widgets, and propagating the data for all Slidablechilds.

@letsar
Copy link
Owner

letsar commented Jul 18, 2021

Fixed in 1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.0 test needed enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants