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

SetState not changing the preview, is this even possible? #13

Closed
Luppakorva opened this issue Sep 23, 2021 · 1 comment
Closed

SetState not changing the preview, is this even possible? #13

Luppakorva opened this issue Sep 23, 2021 · 1 comment

Comments

@Luppakorva
Copy link

Thank you for the plugin!

Could you possibly let me know if it's possible to change the url in setstate?

When I try to do this absolutely nothing happens when url is changed and setstate launched.

What I got as an example:

  String previewUrl = 'https://google.com';


Widget build(BuildContext context) {
 

    PostsViewModel viewModel = Provider.of<PostsViewModel>(context);
    return WillPopScope(
      onWillPop: () async {
        await viewModel.resetPost();
        return true;
      },
      child: ModalProgressHUD(
        progressIndicator: circularProgress(context),
        inAsyncCall: viewModel.loading,
        child: Scaffold(
          key: viewModel.scaffoldKey,
          appBar: AppBar(
            leading: IconButton(
              icon: Icon(Feather.x),
              onPressed: () {
                viewModel.resetPost();
                Navigator.pop(context);
              },
            ),
            title: Text('MyApp'),
            centerTitle: true,
            actions: [
              InkWell(
                onTap: () async {
                  print(controller.text);
                  await postToApi(viewModel.description);
                  controller.clear();
                  viewModel.resetPost();

                  Navigator.pushReplacement(
                    context,
                    MaterialPageRoute(builder: (context) => TabScreen()),
                  );
                },
                child: Padding(
                  padding: const EdgeInsets.all(20.0),
                  child: Text(
                    'Post'.toUpperCase(),
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 15.0,
                      color: Theme.of(context).accentColor,
                    ),
                  ),
                ),
              )
            ],
          ),
          body: ListView(
            children: [

              AnyLinkPreview(
                link: previewUrl,
                displayDirection: UIDirection.UIDirectionHorizontal,
                cache: Duration(hours: 1),
                backgroundColor: Colors.grey[300],
                errorWidget: Container(
                  color: Colors.grey[300],
                  child: Container(),
                ),
              ),
Padding(
                padding: EdgeInsets.all(10),
                child: Column(children: [
                  Container(
                    width: double.infinity,
                    child: Text(
                      'Link'.toUpperCase(),
                      style: TextStyle(
                        fontSize: 15.0,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                  ),
                  TextFormField(
                    decoration: InputDecoration(
                      hintText: 'Source',
                      focusedBorder: UnderlineInputBorder(),
                    ),
                    maxLines: null,
                    onChanged: (value) {
                      _url1 = value;
                      previewUrl = value;

                    },
                  ),
                  TextButton(
                    child: Text("Fetch"),
                    onPressed:() {
                      setState(() {
                        print(previewUrl);
                      });
                    },
                  )
                ]),
              ),
]);

However this does not refresh the preview even though previewURL changes with setState. Any way to achieve this?

Running the latest RC as version.

@sur950
Copy link
Owner

sur950 commented Jan 8, 2022

Hey,

It's not recommended to use setState() inside the TextField's onChange method. Instead you can use Debouncer() to achieve the refresh.

However, I have verified the same locally & seems it is refreshing the link preview card.
Feel free to open the new issue if the issue persists.

Thanks!

@sur950 sur950 closed this as completed Jan 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants