简体   繁体   English

如何在flutter中保存checkboxListTile

[英]How to save checkboxListTile in flutter

I have a CheckboxListTile field, after the user selects it, he moves to a new page to see some details.我有一个 CheckboxListTile 字段,在用户选择它后,他移动到一个新页面以查看一些详细信息。 The problem after the user moves to the next page.用户移动到下一页后的问题。 The user's choice is deleted.用户的选择被删除。 After returning to the previous page, the option that the user previously chose is not found.返回上一页后,没有找到用户之前选择的选项。 How can the user's choice data be saved so that the user is able to see his choice after returning to the previous page?如何保存用户的选择数据,让用户返回上一页后可以看到自己的选择?

Full code完整代码

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'LawsOfNewUser.dart';


void main() {
  runApp(
    EasyLocalization(
      saveLocale: true,
      supportedLocales: [Locale('en', 'US'),
       ],
      path: 'assets/translations',
      fallbackLocale: Locale('en', 'US'),
      child: NewMembership(),
    ),
  );
}

class NewMembership extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: LoginScreen(),
    );
  }
}

class LoginScreen extends StatefulWidget {
  @override
  _LoginScreenState createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      body: Center(
        child: SingleChildScrollView(
          child: Container(
            child: Column(
              children: [
                Card(
                  child: Column(
                    children: [
                      CheckboxListTile(
                          title: Text('Terms'.tr()),
                          value: false,
                          onChanged: (bool newValue) {
                            Navigator.push(context, new MaterialPageRoute(builder: (context) => new LawsUser()
                            ),
                            );
                          }
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),

    );
  }
}




I tried to use shared_preferences but didn't know how to do this.我尝试使用 shared_preferences 但不知道如何执行此操作。 If anyone knows the solution to this problem please help me.如果有人知道这个问题的解决方案,请帮助我。

State managment is a vast topic. 国家管理是一个广阔的话题。 The idea is that, when you do Navigator.push... you create a new widget, therefore the widget is at its original state and you don't have any state restoration.这个想法是,当您执行Navigator.push...您创建了一个新的小部件,因此小部件处于其原始状态并且您没有任何状态恢复。 However the true power of route is that if you use Navigator.push... , you actually create a stack of widget.然而,路由的真正威力在于,如果您使用Navigator.push... ,您实际上会创建一堆小部件。 Therefore your old widget is still here, but "behind" the new one.因此,您的旧小部件仍然存在,但在新小部件的“后面”。 To remove the new one and get back to the previous, you can use Navigator.pop .要删除新的并返回到前一个,您可以使用Navigator.pop

Here is a working example:这是一个工作示例:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(home: NewMembership()));
}

class NewMembership extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: LoginScreen(),
    );
  }
}

class LoginScreen extends StatefulWidget {
  @override
  _LoginScreenState createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {
  bool termsAccepted = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: SingleChildScrollView(
          child: Container(
            child: Column(
              children: [
                Card(
                  child: Column(
                    children: [
                      CheckboxListTile(
                          title: Text('Terms'),
                          value: termsAccepted,
                          onChanged: (bool newValue) {
                            termsAccepted = !termsAccepted;
                            setState(() {});
                            if (termsAccepted) {
                              Navigator.push(
                                context,
                                new MaterialPageRoute(builder: (context) => new SimplePage()),
                              );
                            }
                          }),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class SimplePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.pop(
            context,
            new MaterialPageRoute(builder: (context) => new LoginScreen()),
          );
        },
      ),
    );
  }
}

Note however that while this work great in this case, it is often not enough for handling an entire application state.但是请注意,虽然这在这种情况下效果很好,但通常不足以处理整个应用程序状态。 For this you should look at the link I gave you at the start of this answer.为此,您应该查看我在本答案开头提供的链接。

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

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