如何使用列表中的数据在 Flutter 中打印列表视图?

How to print listview in Flutter using data from List?

我有一个步进器表单,我用它来获取用户的输入。有 3 个步骤,用户在每个步骤中提供输入,然后单击“保存”按钮将输入保存到列表中。现在,问题是,我可以在我的控制台上打印列表。但我不知道如何在按钮上方的屏幕上打印它,或者如果任何字段为空,我想在按钮上方显示错误消息。谁能帮帮我吗?我有以下代码。

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Stepper Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Stepper Tutorial'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _currentStep = 0;
  TextEditingController nameController = TextEditingController();
  TextEditingController emailController = TextEditingController();
  TextEditingController addressController = TextEditingController();
  List<String> demoList = [];

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: Column(
        children: <Widget>[
          Stepper(
            steps: _mySteps(),
            currentStep: this._currentStep,
            onStepTapped: (step) {
              setState(
                () {
                  this._currentStep = step;
                },
              );
            },
            onStepContinue: () {
              setState(
                () {
                  if (this._currentStep < this._mySteps().length - 1) {
                    this._currentStep = this._currentStep + 1;
                  } else {
                    //Logic to check if everything is completed
                    print('Completed, check fields.');
                  }
                },
              );
            },
            onStepCancel: () {
              setState(
                () {
                  if (this._currentStep > 0) {
                    this._currentStep = this._currentStep - 1;
                  } else {
                    this._currentStep = 0;
                  }
                },
              );
            },
          ),
          ElevatedButton(
            onPressed: viewList,
            child: Text("Click to see List"),
          ),
        ],
      ),
    );
  }

  viewList() {
    if (nameController.text.isEmpty ||
        emailController.text.isEmpty ||
        addressController.text.isEmpty) {
      // //// Print Error message display in the screen above the button //
    } else {
      demoList.add(nameController.text);
      demoList.add(emailController.text);
      demoList.add(addressController.text);
    }
    print(demoList);
  }

  List<Step> _mySteps() {
    List<Step> _steps = [
      Step(
        title: Text('Step 1'),
        content: TextField(
          controller: nameController,
        ),
        isActive: _currentStep >= 0,
      ),
      Step(
        title: Text('Step 2'),
        content: TextField(
          controller: emailController,
        ),
        isActive: _currentStep >= 1,
      ),
      Step(
        title: Text('Step 3'),
        content: TextField(
          controller: addressController,
        ),
        isActive: _currentStep >= 2,
      )
    ];
    return _steps;
  }
}

虽然我不确定是否可以通过步骤完成,但我通常将 FormTextFormField 一起使用,您可以在其中为表单添加一个 validator 以检查是否该值是否为空并通过消息提醒用户。示例:

TextFormField(
                        validator: (value) {
                          if (value.isEmpty) {
                            return 'Please enter some text'; // the message which you alert the user that he needs to enter the value
                          }
                          return null;
                        },
                        keyboardType: TextInputType.text,
                      ),

作为 listView 条件的一部分,您可以使用一些 if/switch 大小写或迭代器。在按钮上方放置一个小部件(ListView.builder),就像所说的使用和 if/switch 案例或迭代器一样,您声明:

if (formIsEmpty){
//logic when the form is empty
} ? ListView.builder() : Cointainer()

所以,让我试着解释一下。起初,它会在按钮上方显示一个空容器或您喜欢的任何其他小部件,但是当满足 ListView.builder 的逻辑时,您会触发 lisview 并根据需要显示列表。

这通常用于登录表单,当您忘记密码时,会出现一个 link,link 以重设密码等。

bool _forgotPassword = false;
 _forgotPassword 
            ? TextButton(
                onPressed: () async {
                  //await your logic for forgotton password
                },
                child: Text('Forgot Password'))
            : Container(),

// ABOVE IS THE LOGIC FOR SHOWING THE WIDGET (IN YOUR CASE LISTVIEW.BUILDER) AFTER AN ACTION IS PERFORMED
        TextButton(
            onPressed: () async {
              //await your login logic, if it returns false\error\not authenticated, then you can change the state of _forgotPassword, and then shows 'forgot password TextButton'
              setState(() {
                _forgotPassword = true;
              });
            },
            child: Text('Login'))