How to build a list view builder from a form with values from the form

大城市里の小女人 提交于 2020-01-16 08:59:16

问题


I have a Flutter app that takes an input from a user and build a list on to another page but when I try to build the list on the other page it routes but it does not build the list and I have another function with the thing but does not route to the page I want instead it routes to a page I just made up right there this is the form code

import 'package:flutter/material.dart';
import 'cont.dart';
import 'user.dart';

typedef OnDelete();

class UserForm extends StatefulWidget {
  final User user;
  final state = _UserFormState();
  final OnDelete onDelete;
  UserForm({Key key, this.user, this.onDelete}) : super(key: key);
  @override
  _UserFormState createState() => state;
  bool isValid() => state.validate();
}

class _UserFormState extends State<UserForm> {
  final form = GlobalKey<FormState>();
  List<UserForm> _pages = new List();
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(20),
      child: Column(
        children: <Widget>[
          AppBar(
            title: Text('Form'),
            centerTitle: true,
            backgroundColor: Color(0xFF0B0F1B).withOpacity(0.001),
            actions: <Widget>[
              Padding(
                padding: EdgeInsets.only(right: 30),
                child: InkWell(
                  child: Icon(
                    Icons.delete,
                    color: Colors.white,
                  ),
                  onTap: widget.onDelete,
                ),
              ),
            ],
          ),
          Material(
              color: Color(0xFF0B0F1B),
              shadowColor: Theme.of(context).accentColor.withOpacity(.5),
              elevation: 16,
              clipBehavior: Clip.antiAlias,
              borderRadius: BorderRadius.only(
                  bottomLeft: Radius.circular(30),
                  bottomRight: Radius.circular(30)),
              child: Padding(
                padding: EdgeInsets.only(left: 130),
                child: Form(
                  key: form,
                  child: Row(
                    children: <Widget>[
                      Column(
                        children: <Widget>[
                          Container(
                            margin: EdgeInsets.only(right: 20, top: 10),
                            height: 35,
                            width: 150,
                            decoration: new BoxDecoration(
                                border: new Border.all(
                                    width: 2, color: Colors.grey),
                                borderRadius: new BorderRadius.circular(20),
                                color: Colors.white.withOpacity(0.1)),
                            child: TextFormField(
                              autofocus: true,
                              keyboardType: TextInputType.number,
                              initialValue: widget.user.amount,
                              decoration: InputDecoration(
                                  suffixIcon: Icon(Icons.attach_money),
                                 
                                  contentPadding:
                                      EdgeInsets.only(top: 17, left: 10),
                                  border: OutlineInputBorder(
                                      borderRadius: BorderRadius.circular(20))),
                              validator: (value) => value.length == 0
                                  ? "Enter  Amount"
                                  : null,
                              onSaved: (value) => widget.user.amount = value,
                            ),
                          ),
                          Container(
                            height: 35,
                            width: 130,
                            decoration: new BoxDecoration(
                                border: new Border.all(
                                    width: 2, color: Colors.grey),
                                borderRadius: new BorderRadius.circular(20),
                                color: Colors.white.withOpacity(0.1)),
                            margin: const EdgeInsets.only(top: 40),
                            child: TextFormField(
                              initialValue: widget.user.person,
                              keyboardType: TextInputType.number,
                              decoration: InputDecoration(
                                  suffixIcon: Icon(Icons.attach_money),
                                 
                                  contentPadding:
                                      EdgeInsets.only(top: 17, left: 10),
                                  border: OutlineInputBorder(
                                      borderRadius: BorderRadius.circular(20))),
                              validator: (value) => value.length == 0
                                  ? "Enter Person"
                                  : null,
                              onSaved: (value) => widget.user.person = value,
                            ),
                          ),
                          Container(
                            height: 35,
                            width: 100,
                            decoration: new BoxDecoration(
                                border: new Border.all(
                                    width: 2, color: Colors.grey),
                                borderRadius: new BorderRadius.circular(20),
                                color: Colors.white.withOpacity(0.1)),
                            margin: const EdgeInsets.only(top: 40, right: 30),
                            child: TextFormField(
                              initialValue: widget.user.number,
                              keyboardType: TextInputType.number,
                              decoration: InputDecoration(
                                 
                                  contentPadding:
                                      EdgeInsets.only(top: 17, left: 10),
                                  border: OutlineInputBorder(
                                      borderRadius: BorderRadius.circular(20))),
                              validator: (value) => value.length == 0
                                  ? "Enter Number Of Participants "
                                  : null,
                              onSaved: (value) => widget.user.number = value,
                            ),
                          ),
                          Container(
                            height: 35,
                            width: 129,
                            decoration: new BoxDecoration(
                                border: new Border.all(
                                    width: 2, color: Colors.grey),
                                borderRadius: new BorderRadius.circular(20),
                                color: Colors.white.withOpacity(0.1)),
                            margin: const EdgeInsets.only(top: 40, right: 30),
                            child: TextFormField(
                              initialValue: widget.user.time,
                              keyboardType: TextInputType.number,
                              decoration: InputDecoration(
                                  contentPadding:
                                      EdgeInsets.only(top: 12, left: 10),
                                 
                                  border: OutlineInputBorder(
                                      borderRadius: BorderRadius.circular(80))),
                              validator: (value) => value.length == 0
                                  ? "Enter  Time Interval"
                                  : null,
                              onSaved: (value) => widget.user.time = value,
                            ),
                          ),
                          Container(
                            height: 40,
                            width: 110,
                            decoration: new BoxDecoration(
                                border: new Border.all(
                                    width: 2, color: Colors.grey),
                                borderRadius: new BorderRadius.circular(17),
                                color: Colors.white.withOpacity(0.1)),
                            margin: const EdgeInsets.only(top: 40, right: 80),
                            child: TextFormField(
                              initialValue: widget.user.joining,
                              keyboardType: TextInputType.number,
                              decoration: InputDecoration(
                                  contentPadding:
                                      EdgeInsets.only(top: 17, left: 10),
                                  border: OutlineInputBorder(
                                      borderRadius: BorderRadius.circular(15))),
                              validator: (value) => value.length == 0
                                  ? "Enter Joining Time"
                                  : null,
                              onSaved: (value) => widget.user.joining = value,
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.only(
                              bottom: 20,
                            ),
                            margin: EdgeInsets.only(top: 40),
                            child: InkWell(
                              child: Column(
                                children: <Widget>[
                                  Container(
                                    padding: EdgeInsets.all(3),
                                    height: 30,
                                    width: 100,
                                    decoration: BoxDecoration(
                                        color:
                                            Color(0xFF0B0F1B).withOpacity(0.8),
                                        borderRadius: BorderRadius.circular(15),
                                        border: Border.all(
                                            width: 1, color: Colors.grey)),
                                    child: Text(
                                      'Done',
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                          color: Colors.blue, fontSize: 17),
                                    ),
                                  ),
                                ],
                              ),
                              onTap: () {
                                setState(() {
                                  var data =
                                      _pages.map((it) => it.user).toList();

                                  Navigator.of(context).push(MaterialPageRoute(
                                      builder: (context) => OtherPage(
                                            items: _pages,
                                            data: data,
                                          )));
                                });
                              },
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              )),
        ],
      ),
    );
  }

  bool validate() {
    var valid = form.currentState.validate();
    if (valid) form.currentState.save();
    return valid;
  }
}

and this is the page where it is supposed to be viewed

import 'package:eq_program/form.dart';
import 'package:eq_program/user.dart';
import 'package:flutter/material.dart';

class OtherPage extends StatefulWidget {
  final List<UserForm> items;
  final List<User> data;
  OtherPage({Key key, this.items, this.data}) : super(key: key);

  @override
  _OtherPageState createState() => _OtherPageState();
}

class _OtherPageState extends State<OtherPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          elevation: 16.0,
          title: Text('My Details'),
          backgroundColor: Color(0xFF0B0F1B).withOpacity(0.98),
        ),
        backgroundColor: Color(0xFF0B0F1B),
        body: Center(
          child: ListView.builder(
            itemCount: widget.items.length,
            itemBuilder: (BuildContext context, int index) {
              return Padding(
                padding: EdgeInsets.all(13),
                child: Material(
                  borderRadius: BorderRadius.circular(20),
                  elevation: 26,
                  color: Theme.of(context).cardColor.withOpacity(.95),
                  shadowColor: Theme.of(context).accentColor.withOpacity(.5),
                  child: Container(
                    padding: EdgeInsets.all(10),
                    height: 150,
                    decoration: BoxDecoration(
                        color: Color(0xFF0B0F1B),
                        borderRadius: BorderRadius.circular(20),
                        border: Border.all(
                            width: 1, color: Colors.grey.withOpacity(0.5))),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        Text(widget.data[index].amount,
                            style: TextStyle(color: Colors.white)),
                        Text(widget.data[index].person,
                            style: TextStyle(color: Colors.white)),
                        Text(
                            
                                widget.data[index].time,
                            style: TextStyle(color: Colors.white)),
                        Text(widget.data[index].number,
                            style: TextStyle(color: Colors.white)),
                        Text(widget.data[index].joining,
                            style: TextStyle(color: Colors.white)),
                        Padding(
                          padding: EdgeInsets.only(top: 8.0),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                              SizedBox(
                                height: 40,
                              ),
                              Material(
                                borderRadius: BorderRadius.circular(20),
                                elevation: 26,
                                color: Color(0xFF0B0F1B),
                                child: Container(
                                  height: 30,
                                  width: 120,
                                  decoration: BoxDecoration(
                                      color: Color(0xFF0B0F1B),
                                      borderRadius: BorderRadius.circular(20),
                                      border: Border.all(
                                          width: 1,
                                          color: Colors.grey.withOpacity(0.5))),
                                  child: InkWell(
                                    onTap: () {
                                      int currentval =
                                          int.parse(widget.data[index].time);
                                      setState(() {
                                        currentval--;
                                        widget.data[index].time =
                                            currentval.toString();
                                        if (currentval == 0) {
                                          Navigator.push(
                                              context,
                                              MaterialPageRoute(
                                                  builder: (_) => Scaffold(
                                                        appBar: AppBar(
                                                          title: Text('Route'),
                                                        ),
                                                        body: Container(
                                                          color:
                                                              Colors.blueGrey,
                                                          height: 50,
                                                        ),
                                                      )));
                                        }
                                      });
                                    },
                                  ),
                                ),
                              ),
                              SizedBox(
                                width: 52,
                              ),
                              Material(
                                borderRadius: BorderRadius.circular(20),
                                elevation: 26,
                                color: Color(0xFF0B0F1B),
                                child: Container(
                                  height: 30,
                                  width: 120,
                                  decoration: BoxDecoration(
                                      color: Color(0xFF0B0F1B),
                                      borderRadius: BorderRadius.circular(20),
                                      border: Border.all(
                                          width: 1,
                                          color: Colors.grey.withOpacity(0.5))),
                                  child: InkWell(
                                    onTap: () {
                                      int currentval =
                                          int.parse(widget.data[index].time);
                                      setState(
                                        () {
                                          currentval--;
                                          widget.data[index].time =
                                              currentval.toString();
                                          if (currentval == 0) {
                                            Navigator.push(
                                              context,
                                              MaterialPageRoute(
                                                builder: (_) => Scaffold(
                                                  appBar: AppBar(
                                                    title: Text('Route'),
                                                  ),
                                                  body: Container(
                                                    color: Colors.blueGrey,
                                                    height: 50,
                                                  ),
                                                ),
                                              ),
                                            );
                                          }
                                        },
                                      );
                                    },
                                  ),
                                ),
                              )
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}

What am I missing?

来源:https://stackoverflow.com/questions/59509579/how-to-build-a-list-view-builder-from-a-form-with-values-from-the-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!