如何在flutter中重用同一小部件​​的内容?

时间:2019-05-21 07:58:27

标签: dart flutter

我想在多个地方使用窗口小部件的相同实例,一个地方的更改将同步到另一地方。

但是在多个位置使用同一实例看起来就像是多个不同的实例。

那我该如何实现壮举?

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

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

我正在使用flutter_inappbrowser

小部件一和小部件二使用相同的实例,但是当我用小部件一打开网页时,小部件二不受影响。

我希望小部件1和小部件2成为同一小部件​​,当小部件1更改时,小部件2也将受到影响。

1 个答案:

答案 0 :(得分:1)

创建另一个类并返回所需的小部件之类的类型:

与我的customButton有关的示例,在各种类中都需要:

 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)),
    ),
  );
}

已使用:

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);

}