Flutter: Widget does not change dynamically on Tap

122 Views Asked by At

I want to dynamically change the screen with isUserRegistered of model.

If isUserRegistered is True, I wanna show Icon which sets isUserRegistered to false when clicked. If isUserRegistered is False, I wanna hide Icon.

I tried to use setState, but widget did not change. I think It's probably not good to set isUserRegistered = sites.isUserRegistered; inside Widget, but I don't know how to improve it.

plese help me.

class Sample extends StatefulWidget {
  @override
  _SampleState createState() => _SampleState();
}

class _SampleState extends State<Sample> {
  @override

  bool isUserRegistered = false;

  Widget build(BuildContext context) {
    create: (_) => SiteModel()..fetchSites(),
    child: Scaffold(
      resizeToAvoidBottomPadding: false,
      appBar: AppBar(
        title: Text(
          'Title',
        ),
      ),
      drawer: CustomDrawer(),
      body: Consumer<SiteModel>(
        builder: (context, model, child) {
          final sites = model.sites;
          isUserRegistered = sites.isUserRegistered;
          return ListView(
            leading: ... (abridgement)
            subtitle: ... (abridgement)
            trailing: !isUserRegistered
            ? IconButton(
                icon: Icon(Icons.add_circle_outline),
                onPressed: () {
                  Provider.of<UserModel>(context, listen:false).update UserRegistered(widget.siteId) // update UserRegistered
                  SetState(({
                    isUserRegistered = true;
                  }))
                }
              ),
            : 
          );
        }
      )
    );
  }
}
0

There are 0 best solutions below