UnityTech / UIWidgets

UIWidget is a Unity Package which helps developers to create, debug and deploy efficient, cross-platform Apps.
Other
1.97k stars 256 forks source link

如何实现已渲染的页面内容,获取数据后得到更新? #422

Closed gier0759 closed 4 years ago

gier0759 commented 4 years ago

通过接口更新的变量值,并没有使已渲染的内容发生改变。(渲染的页继承自StatelessWidget;接口在进入页面时立即调用) 更新的内容是用户名、邮箱等必须从后台获取的信息。 期望的状态:页面相应位置从空白更新成带用户信息的状态

Leacae commented 4 years ago

你好 ,StatelessWidget 组件是无状态的(就是无法更新数据),如果你需要更新页面的数据,就需要用StatefulWidget组件,并且需要一个维护自身状态的State类!可以参考如下代码:

     class ExampleApp : StatefulWidget {
         public ExampleApp(Key key = null) : base(key) {
         }

         public override State createState() {
             return new ExampleState();
         }
     }

     class ExampleState : State<ExampleApp> {
         int counter = 0;

         public override Widget build(BuildContext context) {
             return new Column(
                 children: new List<Widget> {
                     new Text("Counter: " + this.counter),
                     new GestureDetector(
                         onTap: () => {
                             this.setState(()
                                 => {
                                 this.counter++;
                             });
                         },
                         child: new Container(
                             padding: EdgeInsets.symmetric(20, 20),
                             color: Colors.blue,
                             child: new Text("Click Me")
                         )
                     )
                 }
             );
         }
     }
 }