SetState不使用Flutter Desktop更新屏幕中的值

发布于 2025-02-09 14:52:59 字数 7573 浏览 2 评论 0原文

因此,我正在尝试使用SetState更新下拉列表的值,但是当我从列表中选择一个值时,屏幕上的值将无法更改

    import 'package:demo_app_admin/Custom/custom_Font.dart';
    import 'package:flutter/material.dart';
    import '/Custom/Custom_Raised_Bottun.dart';
    import '/Custom/Header.dart';
    import '/Custom/inputField.dart';
    
    class LoginView extends StatefulWidget {
      @override
      _LoginViewState createState() => _LoginViewState();
    }
    
    class _LoginViewState extends State<LoginView> {
      @override
      Widget build(BuildContext context) {
        final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
        final isKeyboard = MediaQuery.of(context).viewInsets.bottom != 0;
        String _dropdownvalue = "Global admin";
        List<String> _items = <String> ["Global admin", "Institution admin"];
        return Scaffold(
          body: Container(
            width: double.infinity,
            decoration: BoxDecoration(
              gradient: LinearGradient(begin: Alignment.topCenter, colors: [
                Colors.teal[700]!,
                Colors.teal[200]!,
                Colors.teal[400]!
              ]),
            ),
            child: Column(
              children: <Widget>[
                const SizedBox(
                  height: 80,
                ),
                if (!isKeyboard)
                  Header(
                    padding: const EdgeInsets.all(20),
                    crossAxisAlignment: CrossAxisAlignment.start,
                    head: "Welcome",
                    headTextStyle:
                        const TextStyle(color: Colors.white, fontSize: 40),
                    sizedBoxHeight: 10,
                    subtitle: "Sign In to Your Admin Account",
                    subtitleTextStyle:
                        const TextStyle(color: Colors.white, fontSize: 18),
                  ),
                Expanded(
                    child: Container(
                  decoration: const BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(60),
                        topRight: Radius.circular(60),
                      )),
                  child: Padding(
                    padding: const EdgeInsets.all(30),
                    child: SingleChildScrollView(
                      reverse: true,
                      padding: EdgeInsets.all(32),
                      child: Form(
                        key: _formKey,
                        child: Column(
                          children: <Widget>[
                            const SizedBox(
                              height: 40,
                            ),
                            Container(
                              decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(10)),
                              child: Column(
                                children: <Widget>[
                                  InputField(
                                    labelText: 'Email',
                                    padding: EdgeInsets.all(10),
                                    borderSide: BorderSide(color: Colors.grey),
                                    hintText: "Enter your email",
                                    color: Colors.grey,
                                    inputBorder: InputBorder.none,
                                    obscureText: false,
                                    enableSuggestion: true,
                                    autoCorrect: true,
                                    onSaved: (value) {},
                                    // (value){controller.email = value!;},
                                    validator: (value) {
                                      if (value == null) {
                                        print("ERROR");
                                      }
                                    },
                                  ),
                                  InputField(
                                    labelText: 'Password',
                                    padding: EdgeInsets.all(10),
                                    borderSide: BorderSide(color: Colors.grey),
                                    hintText: "Enter your password",
                                    color: Colors.grey,
                                    inputBorder: InputBorder.none,
                                    obscureText: true,
                                    enableSuggestion: false,
                                    autoCorrect: false,
                                    onSaved: (value) {},
                                    // (value){ controller.password = value!;},
                                    validator: (value) {
                                      if (value == null) {
                                        print("ERROR");
                                      }
                                    },
                                  ),
                                ],
                              ),
                            ),
                            const SizedBox(
                              height: 40,
                            ),
here is the issue
                            DropdownButton<String>(
                                value: _dropdownvalue,
                                icon: Icon(Icons.keyboard_arrow_down),
                                items: _items.map((String _items) {
                                  return DropdownMenuItem<String>(
                                    value: _items,
                                    child: CustomFont(text: _items),
                                  );
                                }).toList(),
                                onChanged: (value) {
                                  setState(() => _dropdownvalue = value!);
                                }),
                            const SizedBox(
                              height: 40,
                            ),
                            CustomRaisedButton(
                                width: 200,
                                height: 50.0,
                                margin: const EdgeInsets.symmetric(horizontal: 50),
                                color: Colors.teal.shade500,
                                borderRadius: BorderRadius.circular(10),
                                text: "LOGIN",
                                textColor: Colors.white,
                                fontSize: 17,
                                fontWeight: FontWeight.bold,
                                function: () {}
                                // {
                                //   _formKey.currentState?.save();
                                //   if(_formKey.currentState?.validate() !=null){
                                //     controller.signInWithEmailAndPassword();
                                //   }
                                // },
                                ),
                            SizedBox(
                              height: 10,
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ))
              ],
            ),
          ),
        );
      }
    }

So I'm trying to update the value of the drop down list using the setState, but when I select a value from the list, the value on the screen won't be able to change

    import 'package:demo_app_admin/Custom/custom_Font.dart';
    import 'package:flutter/material.dart';
    import '/Custom/Custom_Raised_Bottun.dart';
    import '/Custom/Header.dart';
    import '/Custom/inputField.dart';
    
    class LoginView extends StatefulWidget {
      @override
      _LoginViewState createState() => _LoginViewState();
    }
    
    class _LoginViewState extends State<LoginView> {
      @override
      Widget build(BuildContext context) {
        final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
        final isKeyboard = MediaQuery.of(context).viewInsets.bottom != 0;
        String _dropdownvalue = "Global admin";
        List<String> _items = <String> ["Global admin", "Institution admin"];
        return Scaffold(
          body: Container(
            width: double.infinity,
            decoration: BoxDecoration(
              gradient: LinearGradient(begin: Alignment.topCenter, colors: [
                Colors.teal[700]!,
                Colors.teal[200]!,
                Colors.teal[400]!
              ]),
            ),
            child: Column(
              children: <Widget>[
                const SizedBox(
                  height: 80,
                ),
                if (!isKeyboard)
                  Header(
                    padding: const EdgeInsets.all(20),
                    crossAxisAlignment: CrossAxisAlignment.start,
                    head: "Welcome",
                    headTextStyle:
                        const TextStyle(color: Colors.white, fontSize: 40),
                    sizedBoxHeight: 10,
                    subtitle: "Sign In to Your Admin Account",
                    subtitleTextStyle:
                        const TextStyle(color: Colors.white, fontSize: 18),
                  ),
                Expanded(
                    child: Container(
                  decoration: const BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(60),
                        topRight: Radius.circular(60),
                      )),
                  child: Padding(
                    padding: const EdgeInsets.all(30),
                    child: SingleChildScrollView(
                      reverse: true,
                      padding: EdgeInsets.all(32),
                      child: Form(
                        key: _formKey,
                        child: Column(
                          children: <Widget>[
                            const SizedBox(
                              height: 40,
                            ),
                            Container(
                              decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(10)),
                              child: Column(
                                children: <Widget>[
                                  InputField(
                                    labelText: 'Email',
                                    padding: EdgeInsets.all(10),
                                    borderSide: BorderSide(color: Colors.grey),
                                    hintText: "Enter your email",
                                    color: Colors.grey,
                                    inputBorder: InputBorder.none,
                                    obscureText: false,
                                    enableSuggestion: true,
                                    autoCorrect: true,
                                    onSaved: (value) {},
                                    // (value){controller.email = value!;},
                                    validator: (value) {
                                      if (value == null) {
                                        print("ERROR");
                                      }
                                    },
                                  ),
                                  InputField(
                                    labelText: 'Password',
                                    padding: EdgeInsets.all(10),
                                    borderSide: BorderSide(color: Colors.grey),
                                    hintText: "Enter your password",
                                    color: Colors.grey,
                                    inputBorder: InputBorder.none,
                                    obscureText: true,
                                    enableSuggestion: false,
                                    autoCorrect: false,
                                    onSaved: (value) {},
                                    // (value){ controller.password = value!;},
                                    validator: (value) {
                                      if (value == null) {
                                        print("ERROR");
                                      }
                                    },
                                  ),
                                ],
                              ),
                            ),
                            const SizedBox(
                              height: 40,
                            ),
here is the issue
                            DropdownButton<String>(
                                value: _dropdownvalue,
                                icon: Icon(Icons.keyboard_arrow_down),
                                items: _items.map((String _items) {
                                  return DropdownMenuItem<String>(
                                    value: _items,
                                    child: CustomFont(text: _items),
                                  );
                                }).toList(),
                                onChanged: (value) {
                                  setState(() => _dropdownvalue = value!);
                                }),
                            const SizedBox(
                              height: 40,
                            ),
                            CustomRaisedButton(
                                width: 200,
                                height: 50.0,
                                margin: const EdgeInsets.symmetric(horizontal: 50),
                                color: Colors.teal.shade500,
                                borderRadius: BorderRadius.circular(10),
                                text: "LOGIN",
                                textColor: Colors.white,
                                fontSize: 17,
                                fontWeight: FontWeight.bold,
                                function: () {}
                                // {
                                //   _formKey.currentState?.save();
                                //   if(_formKey.currentState?.validate() !=null){
                                //     controller.signInWithEmailAndPassword();
                                //   }
                                // },
                                ),
                            SizedBox(
                              height: 10,
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ))
              ],
            ),
          ),
        );
      }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心未许 2025-02-16 14:52:59

问题在这里,您在build中声明变量。因此,变量可以重置为每个构建的默认值。表示setState。

在构建方法之外声明变量。

class _LoginViewState extends State<LoginView> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  String _dropdownvalue = "Global admin";
  List<String> _items = <String>["Global admin", "Institution admin"];
  @override
  Widget build(BuildContext context) {
    final isKeyboard = MediaQuery.of(context).viewInsets.bottom != 0;
    return Scaffold(

有关 statefulwidget

The issue is here, you are declaring variables inside build. Therefore, variables get reset to default value on every build. Means setState.

Declare variables outside the build method.

class _LoginViewState extends State<LoginView> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  String _dropdownvalue = "Global admin";
  List<String> _items = <String>["Global admin", "Institution admin"];
  @override
  Widget build(BuildContext context) {
    final isKeyboard = MediaQuery.of(context).viewInsets.bottom != 0;
    return Scaffold(

More about StatefulWidget.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文