8

I want to use the same instance of a widget in multiple places, changes in one place will be synchronized to another place.

But using the same instance in multiple places looks like multiple different instances.

So how can I achieve the feat?

  var test = InAppWebView(
    // ...
  );

  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        children: [
          test,  // widget one
          test,  // widget two
        ],
        controller: _pageController,
        physics: NeverScrollableScrollPhysics(),
        onPageChanged: onPageChanged,
      ),
     ),
    }

I'm use flutter_inappbrowser.

Widget one and widget two use the same instance, but when I open the web page with widget one, widget two is unaffected.

I want widget one and widget two to be the same widget, that when widget one changes, widget two will also be affected.

2
  • That is something that it could be achieved with a StatefulWidget or a controller of some sort. Maybe you should ask the maintainer of the plugin to see if it has that feature. Commented May 21, 2019 at 8:32
  • Why dont you use webview_flutter official plugin, that has controller that you can easily use. I have used in app browser but I had issue when I want to build IOS. If you want that try creating a method and passing relevant params and may be if in app browser has key property you can try with same key(im not sure about using same key) Commented May 21, 2019 at 8:54

1 Answer 1

1

create another class and return type like your required widget:

example related to my customButton i required in various classes:

 Widget customButton(title,context,ResponseCallback callBack,shadowColor) {
  return Container(
    margin: EdgeInsets.only(top: 20),
    height: 80,
    width: 150,
    decoration: BoxDecoration(
      color: Colors.blue,
      borderRadius: BorderRadius.circular(75),
      boxShadow: [
        BoxShadow(
          spreadRadius: 1,
          blurRadius: 4,
          offset: Offset(4, 3),
          color: shadowColor
        )
      ]
    ),
    child: FlatButton(
      onPressed: () {
        callBack(title);
      },
      child: Text(title,
          style: TextStyle(
              color: Colors.white,
              fontSize: 20,
              fontWeight: FontWeight.bold)),
    ),
  );
}

used:

child: customButton(
   comparedArray[index].category, context, buttonCallHandle,Colors.black),),



buttonCallHandle(e) {
dynamic instanceUpdated = instance.where((element) => element.category == e).toList();
Navigator.of(context).pushNamed('/choice', arguments: instanceUpdated);

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.