skip to Main Content

So, this is the code I’m using right now. It calls for a custom button from another page where the path screen is changed. But when I try to setState for the original screen upon popping, it doesn’t set state.

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {

  void set() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomIcon(
                iconLabel: 'Button',
                iconPath: 'assets/icons/button.svg',
                iconTitle: 'Button',
                functionRoute: NewScreen.routeName,
                callback: set,
          ),
    );
  }
}

And the code for custom icon is

class CustomIcon extends StatefulWidget {
  String iconPath;
  String iconLabel;
  String iconTitle;
  WidgetRef? ref;
  String? functionRoute;
  dynamic arguments;
  Function? callback;
  CustomIcon(
      {Key? key,
      this.ref,
      this.onPressed,
      required this.iconLabel,
      required this.iconPath,
      required this.iconTitle,
      this.callback,
      this.arguments})
      : super(key: key);

  @override
  State<CustomIcon> createState() => _CustomIconState();
}

class _CustomIconState extends State<CustomIcon> {
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        if (widget.arguments != null) {
          Navigator.pushNamed(context, widget.functionRoute!,
                  arguments: widget.arguments)
              .then((value) {
            widget.callback!;
          });
        } else {
          Navigator.pushNamed(context, widget.functionRoute!).then((value) {
            widget.callback!;
          });
        }
      },
      child: Column(
        children: [
          Container(
            padding: const EdgeInsets.all(8.0),
            decoration: BoxDecoration(
              color: kHeader2,
              borderRadius: BorderRadius.circular(15),
            ),
            child: SvgPicture.asset(
              widget.iconPath,
              color: Colors.white,
              semanticsLabel: widget.iconLabel,
              height: 100,
              width: 100,
            ),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 8.0),
            child: Text(
              widget.iconTitle,
              style: const TextStyle(fontSize: 25),
            ),
          )
        ],
      ),
    );
  }
}

But the setState is not called when I pop the page. When I save the file once again, which sets state in the app the changes are made. But not through this function. How to fix this?

2

Answers


  1. try calling the method like : set() and instead of set

    callback: set(),
    
    Login or Signup to reply.
  2. You are not calling the call back. Change where you have

    widget.callback!;
    

    to

    widget.callback!();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search