简体   繁体   English

更新外部有状态小部件类的实例时如何更新屏幕

[英]How to update screen when instance of external stateful widget class is updated

I am displaying the weight of an instance of a person class on my homepage.我在我的主页上显示了一个 person 类的实例的权重。 When I update the weight of this instance through a form in a popup bottom sheet the displayed weight is only changed after a hot reload.当我通过弹出底部表单中的表单更新此实例的权重时,显示的权重仅在热重新加载后才会更改。 How can I trigger a setState in my person class when its instances parameters are changed in homepage?当主页中的实例参数更改时,如何在我的个人类中触发 setState?

main.dart main.dart

import 'package:flutter/material.dart';
import 'package:metricwidget/screens/homepage.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  // Root of application
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
      
        primarySwatch: Colors.blue,
      ),
      home: const Homepage(),
    );
  }
}

person.dart人.dart

import 'package:flutter/material.dart';

class person extends StatefulWidget {
  int? weight;
  person({Key? key, this.weight}) : super(key: key);

  void updateWeight(newWeight){
    weight = newWeight;
  }

  @override
  _personState createState() => _personState();
}

class _personState extends State<person> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text(
        widget.weight.toString(),
        style: const TextStyle(fontSize: 24),
      ),
    );
  }
}

homepage.dart主页.dart

import 'package:mvs/person.dart';
import 'package:flutter/material.dart';

class Homepage extends StatefulWidget {
  const Homepage({Key? key}) : super(key: key);

  @override
  _HomepageState createState() => _HomepageState();
}

class _HomepageState extends State<Homepage> {
  var joe = person(weight: 23);
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            child: joe,
          ),
          OutlinedButton(
            onPressed: () {
              showModalBottomSheet(
                context: context,
                builder: (context) {
                  return Form(
                    key: _formKey,
                    child: Column(
                      children: [
                        Padding(
                          padding: const EdgeInsets.all(12.0),
                          child: TextFormField(
                            onSaved: (String? value) {
                              if (int.parse(value!) > 0) {
                                setState(() {
                                  joe.updateWeight(int.parse(value));
                                });
                              }
                            },
                            keyboardType: TextInputType.number,
                            maxLength: 3,
                            initialValue: joe.weight.toString(),
                            decoration: const InputDecoration(
                              icon: Icon(Icons.label),
                            ),
                            validator: (value) {
                              if (value!.isEmpty) {
                                return "Please enter value";
                              }
                              return null;
                            },
                          ),
                        ),
                        OutlinedButton(
                          onPressed: () {
                            _formKey.currentState!.save();
                            Navigator.pop(context);
                          },
                          child: const Text("submit"),
                        )
                      ],
                    ),
                  );
                },
              );
            },
            child: const Text("Update"),
          )
        ],
      ),
    );
  }
}

Was able to solve this using provider and changenotifier, same as the format outlined in the docs below能够使用 provider 和 changenotifier 解决这个问题,与下面文档中概述的格式相同

Reference: https://pub.dev/packages/provider参考: https : //pub.dev/packages/provider

暂无
暂无

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

相关问题 Flutter:如何从另一个有状态类更新 UI 状态小部件 - Flutter:How to update a UI -stateful widget from another stateful class Flutter:如何检查对象是否是类的实例(有状态或无状态小部件) - Flutter: How to check if an object is an instance of a class(A stateful or stateless widget) 如何在更新父有状态小部件时更新子有状态小部件 - How to Update child stateful widget on update parent stateful widget 如何在来自另一个 class 的有状态小部件中触发 function(当 onUnityAdsFinish 时)? - How to trigger a function in a stateful widget from another class (when onUnityAdsFinish)? 状态窗口小部件子项未更新 - Stateful Widget child is not updated 使用来自另一个 class 的实例访问有状态小部件的变量时,在 null 上调用的 getter 长度 - getter length called on null when accessing a variable of a stateful widget using its instance from another class 当 stream 在小部件树之外的 class 中获得新值时,如何在有状态 class 中重建小部件? - How to rebuild widget in stateful class when stream gets new value in a class outside of widget tree? 在这种情况下如何更新有状态小部件中的状态? - How to update state in stateful widget in this case? 如何在类/有状态小部件之外调用 setState? - How to call setState outside class/stateful widget? ListView 中的有状态小部件未更新 - Stateful Widget in a ListView is not getting updated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM