简体   繁体   English

在 Flutter 中使用 SQFlite 在两个页面之间传递数据

[英]Passing data between two pages using SQFlite in Flutter

I have two pages with each page containing one TextFormField .我有两页,每页包含一个TextFormField I want to store data entered in first page and display it in the second page and so on.我想存储在第一页中输入的数据并在第二页中显示它,依此类推。 I want this to happen using Database(Sqflite).我希望使用数据库(Sqflite)来实现这一点。 I'm unable to figure out on how to store two different values belonging to the same row of a single Database table.我无法弄清楚如何存储属于单个数据库表的同一行的两个不同值。

Since the app requires multiple pages, I think PageView would be the best for this case.由于该应用程序需要多个页面,因此我认为PageView最适合这种情况。

For the answer to your question checkout below code:要回答您的问题,请查看以下代码:

import 'package:flutter/material.dart';

class PageViewDemo extends StatefulWidget {
  @override
  _PageViewDemoState createState() => _PageViewDemoState();
}

class _PageViewDemoState extends State<PageViewDemo> {
  Map<String, bool> _importantData;
  List<String> _pages;
  @override
  void initState() {
    super.initState();
    _importantData = Map<String, bool>();
    _pages = <String>['one', 'two', 'three', 'four'];
  }

  @override
  Widget build(BuildContext context) {
    return PageView(
      children: _pages.map<Widget>((String pageText) {
        return Center(
          child: Card(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  pageText.toUpperCase(),
                  style: TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold),
                ),
                CheckboxListTile(
                  title: Text('isChecked: '),
                  controlAffinity: ListTileControlAffinity.leading,
                  value: _importantData[pageText] ?? false,
                  onChanged: (bool newValue) {
                    setState(() {
                      _importantData[pageText] = newValue;
                    });
                  },
                )
              ],
            ),
          ),
        );
      }).toList(),
    );
  }
}

Note: This code is just for an explanation, It is not meant to be used directly for the solution.注意:此代码仅用于说明,不能直接用于解决方案。

As you can see, there are 4 different pages with checkboxes for each page.如您所见,有 4 个不同的页面,每个页面都有复选框。 The map _importantData is used for storing the checked status for each page.地图_importantData用于存储每个页面的检查状态。

Just like _importantData to store the checked status, any model class instance can be used for storing the data. Like就像 _importantData to store the checked status, any model class instance can be used for storing the data. Like to store the checked status, any model class instance can be used for storing the data. Like _importantData`, any model class can store data that needs to be accessed across all the pages. to store the checked status, any model class instance can be used for storing the data. Like _importantData` to store the checked status, any model class instance can be used for storing the data. Like ,任何模型类都可以存储需要跨所有页面访问的数据。

For example, you can create a data class to store values that user will input on each page.例如,您可以创建一个数据类来存储用户将在每个页面上输入的值。 Just make sure to create a single instance of the model class and pass it through each page to access/update relative values.只需确保创建模型类的单个实例并将其传递到每个页面以访问/更新相关值。

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

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