Flutter:将数据从一个文件传输到另一个文件

时间:2020-06-05 00:55:25

标签: flutter dart

我有两个文件:Workout.dart和Submit.dart。在Workout.dart中,我有变量:名称,集合,表示,pmg和smg。用户将为变量输入内容,然后在他们单击提交后,将打开一个新页面,显示数据。但是,我不知道该如何保留这些数据。我是Flutter和Dart的新手,请帮助!

Workout.dart

import 'package:flutter/material.dart';
import 'package:gyminprogress/Submit.dart';

class This extends StatefulWidget{
  @override
   Workout createState() => Workout();
}

class Workout extends State<This> {
  String name , sets , rep , pmg , smg ;

@override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blueGrey,
      appBar: AppBar(
        title: Text('Create Workouts'),
        centerTitle: true,
      ),
      body: Container(
        child: Padding(
          padding: const EdgeInsets.all(20.0),
          child: Column(
            children: <Widget>[
              TextField(
                decoration: InputDecoration(
                  hintText: 'Exercise Name'
                ),
                style: TextStyle(color:Colors.white),
                onChanged: (value){
                  setState(() {
                    name = value;
                  });
                },
              ),
              SizedBox(
                height: 40.0,
              ),
              TextField(
                decoration: InputDecoration(
                    hintText: 'Primary Muscle Group'
                ),
                style: TextStyle(color:Colors.white),
                onChanged: (value){
                  setState(() {
                    pmg = value;
                  });
                },
              ),
              SizedBox(
                height: 40.0,
              ),
              TextField(
                decoration: InputDecoration(
                    hintText: 'Secondary Muscle Group'
                ),
                style: TextStyle(color:Colors.white),
                onChanged: (value){
                  setState(() {
                    smg = value;
                  });
                },
              ),SizedBox(
                height: 40.0,
              ),
              TextField(
                decoration: InputDecoration(
                    hintText: 'Reps'
                ),
                style: TextStyle(color:Colors.white),
                onChanged: (value){
                  setState(() {
                    rep = value;
                  });
                },
              ),
              SizedBox(
                height: 40.0,
              ),
              TextField(
                decoration: InputDecoration(
                    hintText: 'Sets'
                ),
                style: TextStyle(color:Colors.white),
                onChanged: (value){
                  setState(() {
                    sets = value;
                  });
                },
              ),
              Expanded(
                child: Container(
                  child: Column(mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
              Row(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: <Widget>[
                    RaisedButton(
                      color: Colors.cyan[200],
                      padding: EdgeInsets.symmetric(
                      horizontal: 80.0,
                      vertical: 20.0
                      ,),
                      child: Text('Submit',style: TextStyle(
                        color: Colors.black,fontWeight: FontWeight.w200
                      ),) , 
                      onPressed: () {
                        Navigator.push(
                          context,
                        MaterialPageRoute(builder: (context) => Submit ( 

                        )),
                        );
                        },
                    ),
                  ]
                ),]),)
              )
            ]
          )
        ),
      ),
    );
    }
  }

Submit.dart

import 'package:flutter/material.dart';
import 'package:gyminprogress/Workout.dart';

class Submit extends StatefulWidget{
  @override
   Enter createState() => Enter();
}

class Enter extends State<Submit> {

@override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blueGrey,
      appBar: AppBar(
        title: Text('Create Workouts'),
        centerTitle: true,
      ),
      body: Container(
        child: Padding(
          padding: const EdgeInsets.all(20.0),
          child: Column(
            children: <Widget>[
              Text('Exercise Name : ${Workout.name}',
              style: TextStyle(fontSize: 20.0,color: Colors.white, fontWeight: FontWeight.w200),),
              Text('Primary Muscle Group : ${Workout.pmg} ',
              style: TextStyle(fontSize: 20.0,color: Colors.white, fontWeight: FontWeight.w200),),
              Text('Secondary Muscle Group : ${Workout.smg} ',
              style: TextStyle(fontSize: 20.0,color: Colors.white, fontWeight: FontWeight.w200),),
              Text('Reps : ${Workout.rep}',
              style: TextStyle(fontSize: 20.0,color: Colors.white, fontWeight: FontWeight.w200),),
              Text('Sets : ${Workout.sets}',
              style: TextStyle(fontSize: 20.0,color: Colors.white, fontWeight: FontWeight.w200),),
            ]
          )
        )
      )
    );
  }
}

2 个答案:

答案 0 :(得分:0)

您可以在导航器中传递数据。

ConceptualEntityModel

然后在第二个屏幕中

class Workout extends ....> {
  String name;


  Person(this.name);
}

// Navigate to second screen with data
Navigator.push(context, new MaterialPageRoute(builder: (context) => new SecondScreenWithData(person: new Person("Vishal"))));

答案 1 :(得分:0)

要将数据从一个小部件传递到另一个小部件,您必须做两件事,

  1. 创建一个小部件的构造函数,使其具有将接受的数据,

  2. 在从另一个小部件调用数据时传递该数据。

这是解决您问题的代码:

workout.dart

import "package:flutter/material.dart";
import './submit.dart';

class Workout extends StatefulWidget {
  @override
  _WorkoutState createState() => _WorkoutState();
}

class _WorkoutState extends State<Workout> {
  String name, sets, rep, pmg, smg;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Builder(
        builder: (context) => Scaffold(
          appBar: AppBar(
            title: Text('Create Workouts'),
            centerTitle: true,
          ),
          body: Container(
            child: Padding(
                padding: const EdgeInsets.all(20.0),
                child: Scaffold(
                    body: Column(children: <Widget>[
                  TextField(
                    decoration: InputDecoration(hintText: 'Exercise Name'),
                    style: TextStyle(color: Colors.black),
                    onChanged: (value) {
                      setState(() {
                        name = value;
                      });
                    },
                  ),
                  SizedBox(
                    height: 40.0,
                  ),
                  TextField(
                    decoration:
                        InputDecoration(hintText: 'Primary Muscle Group'),
                    style: TextStyle(color: Colors.black),
                    onChanged: (value) {
                      setState(() {
                        pmg = value;
                      });
                    },
                  ),
                  SizedBox(
                    height: 40.0,
                  ),
                  TextField(
                    decoration:
                        InputDecoration(hintText: 'Secondary Muscle Group'),
                    style: TextStyle(color: Colors.black),
                    onChanged: (value) {
                      setState(() {
                        smg = value;
                      });
                    },
                  ),
                  SizedBox(
                    height: 40.0,
                  ),
                  TextField(
                    decoration: InputDecoration(hintText: 'Reps'),
                    style: TextStyle(color: Colors.black),
                    onChanged: (value) {
                      setState(() {
                        rep = value;
                      });
                    },
                  ),
                  SizedBox(
                    height: 40.0,
                  ),
                  TextField(
                    decoration: InputDecoration(hintText: 'Sets'),
                    style: TextStyle(color: Colors.black),
                    onChanged: (value) {
                      setState(() {
                        sets = value;
                      });
                    },
                  ),
                  Expanded(
                      child: Container(
                    child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: <Widget>[
                          Row(
                              crossAxisAlignment: CrossAxisAlignment.end,
                              children: <Widget>[
                                RaisedButton(
                                  color: Colors.cyan[200],
                                  padding: EdgeInsets.symmetric(
                                    horizontal: 80.0,
                                    vertical: 20.0,
                                  ),
                                  child: Text(
                                    'Submit',
                                    style: TextStyle(
                                        color: Colors.black,
                                        fontWeight: FontWeight.w600),
                                  ),
                                  onPressed: () {
                                    //String name, sets, rep, pmg, smg;
                                    Navigator.push(
                                      context,
                                      MaterialPageRoute(
                                        builder: (context) => Submit(
                                          name: name,
                                          sets: sets,
                                          rep: rep,
                                          pmg: pmg,
                                          smg: smg,
                                        ),
                                      ),
                                    );
                                  },
                                ),
                              ]),
                        ]),
                  ))
                ]))),
          ),
        ),
      ),
    );
  }
}


submit.dart


import "package:flutter/material.dart";

class Submit extends StatelessWidget {
  final String name, sets, rep, pmg, smg;

  const Submit({
    Key key,
    this.name = "",
    this.sets = "",
    this.rep = "",
    this.pmg = "",
    this.smg = "",
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.blueGrey,
        appBar: AppBar(
          title: Text('Create Workouts'),
          centerTitle: true,
        ),
        body: Container(
            child: Padding(
                padding: const EdgeInsets.all(20.0),
                child: Column(children: <Widget>[
                  Text(
                    'Exercise Name : ${name}',
                    style: TextStyle(
                        fontSize: 20.0,
                        color: Colors.white,
                        fontWeight: FontWeight.w200),
                  ),
                  Text(
                    'Primary Muscle Group : ${pmg} ',
                    style: TextStyle(
                        fontSize: 20.0,
                        color: Colors.white,
                        fontWeight: FontWeight.w200),
                  ),
                  Text(
                    'Secondary Muscle Group : ${smg} ',
                    style: TextStyle(
                        fontSize: 20.0,
                        color: Colors.white,
                        fontWeight: FontWeight.w200),
                  ),
                  Text(
                    'Reps : ${rep}',
                    style: TextStyle(
                        fontSize: 20.0,
                        color: Colors.white,
                        fontWeight: FontWeight.w200),
                  ),
                  Text(
                    'Sets : ${sets}',
                    style: TextStyle(
                        fontSize: 20.0,
                        color: Colors.white,
                        fontWeight: FontWeight.w200),
                  ),
                ]))));
  }
}

输出:

enter image description here

New Updated code

输出:enter image description here