繁体   English   中英

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

[英]How to reuse the contents of the same widget in 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

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

我希望小部件一和小部件二是同一个小部件,当小部件一改变时,小部件二也会受到影响。

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

与我在各种类中需要的 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);

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM