Flutter代码,接受用户显示所需字段的值

问题描述 投票:-1回答:1

**您好,我正在开发一个应用程序,其中用户应输入整数值。根据该值,我们应该显示那些文本字段..

  • 例如: 1) enter total number of subject=10; 2) click on the button; 3) After button is clicked should display 10 text fields; 4) I have no idea what to do so if you know then help me plz.** import'package:flutter / material.dart'; class imcaaddsubject extends StatefulWidget { @override _imcaaddsubjectState createState() => _imcaaddsubjectState(); } class _imcaaddsubjectState extends State<imcaaddsubject> { @override Widget build(BuildContext context) { return Container( padding: EdgeInsets.all(5), child: Card( child: ListView( padding: EdgeInsets.all(10), children: <Widget>[ Padding( padding: EdgeInsets.only(top: 10), ), new TextField( decoration: InputDecoration( border: OutlineInputBorder( borderSide: BorderSide(style: BorderStyle.solid)), labelText: "NUMBER OF SUBJECTS", hintText: "Enter number of subjects", ),keyboardType: TextInputType.number, ), Padding( padding: EdgeInsets.only(top: 15), ), new RaisedButton( child: Text( "OK", style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold), ), color: Colors.cyan, onPressed: () {}, ) ], ), ), ); } }
dart flutter
1个回答
0
投票

你可以尝试这样:

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
      home: MyApp(),
    ));

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  int numberOfSubjects = 0;

  Widget subjectsBuilder(int numberOfSubjects) {
    if (numberOfSubjects > 0) {
      return ListView.builder(
        itemBuilder: (BuildContext context, numberOfSubjects) {
          return TextField(
            decoration: InputDecoration(hintText: "enter a text"),
          );
        },
        itemCount: numberOfSubjects,
      );
    } else {
      return SizedBox();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(children: <Widget>[
        Expanded(
          child: Container(
            padding: EdgeInsets.all(5),
            child: Card(
              child: ListView(
                padding: EdgeInsets.all(10),
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.only(top: 10),
                  ),
                  new TextField(
                    onSubmitted: (String value) {
                      setState(() {
                        numberOfSubjects = int.parse(value);
                      });
                    },
                    decoration: InputDecoration(
                      border: OutlineInputBorder(
                          borderSide: BorderSide(style: BorderStyle.solid)),
                      labelText: "NUMBER OF SUBJECTS",
                      hintText: "Enter number of subjects",
                    ),
                    keyboardType: TextInputType.number,
                  ),
                  Padding(
                    padding: EdgeInsets.only(top: 15),
                  ),
                ],
              ),
            ),
          ),
        ),
        Expanded(
          child: subjectsBuilder(numberOfSubjects),
        )
      ]),
    );
  }
}

此代码将根据第一个文本字段下方的用户输入显示多个文本字段,当然您可以在新页面中呈现它。您也不必使用按钮,这足以让用户提交文本。

© www.soinside.com 2019 - 2024. All rights reserved.