Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
74 views
in Technique[技术] by (71.8m points)

android - How to import data from one page to another in Flutter?

I'm new to Flutter. So I hope that someone can answer this question with a simple example.

Here is my small example.

Main Page

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'secondpage.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Start(),
      debugShowCheckedModeBanner: true,
    );
  }
}

class Start extends StatelessWidget  {

  int number;

  double contheight = 150.0;
  double contwidht = 200.0;
  double contfontsize = 25.0;

  @override
  Widget build(BuildContext context) {

    // Deactivate Android back button
    return new WillPopScope(
      onWillPop: () async => false,

      // return: Scaffold(
      child: Scaffold(

        backgroundColor: Colors.white,
        body: ListView(
          children: <Widget>[

            AppBar(
              elevation: 0,
              title: RichText(
                text: TextSpan(
                  children: <TextSpan>[
                    TextSpan(text: 'Main Screen', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25.0)),
                  ],
                ),
              ),
              centerTitle: true,
              backgroundColor: Colors.green, // Override the back arrow button
            ),


            SizedBox(height: 50.0),

            Column(

              children: <Widget>[

                Container(
                  height: 500.0,
                  decoration: BoxDecoration(
                    color: Colors.white,
                  ),

                  child: ListView(
                    primary: false,
                    padding: EdgeInsets.only(top: 40.0, left: 40.0, right: 40.0, bottom: 40.0),
                    children: <Widget>[

                      // Row - 1

                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[

                          Container(
                            height: contheight,
                            width: contwidht,
                            decoration: BoxDecoration(
                                border: Border.all(
                                    color: Colors.green,
                                    style: BorderStyle.solid,
                                    width: 1.0),
                                borderRadius: BorderRadius.only(topRight: Radius.circular(10.0), topLeft: Radius.circular(10.0),
                                                                bottomRight: Radius.circular(10.0), bottomLeft: Radius.circular(10.0)),
                                color: Colors.green),

                            child: FlatButton(
                              onPressed: (){
                                number = 1;
                                Navigator.push(
                                  context,
                                  MaterialPageRoute(builder: (context) => secondpage(number: this.number)),
                                );
                              },
                              child: Center(

                                  child: Text('1',
                                      style: TextStyle(
                                          fontFamily: 'Montserrat',
                                          color: Colors.white,
                                          fontSize: contfontsize))),

                            ),

                          ),

                        ],
                      ),



                      SizedBox(height: 30.0),



                      // Row - 2



                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[

                          Container(
                            height: contheight,
                            width: contwidht,
                            decoration: BoxDecoration(
                                border: Border.all(
                                    color: Colors.green,
                                    style: BorderStyle.solid,
                                    width: 1.0),
                                borderRadius: BorderRadius.only(topRight: Radius.circular(10.0), topLeft: Radius.circular(10.0),
                                    bottomRight: Radius.circular(10.0), bottomLeft: Radius.circular(10.0)),
                                color: Colors.green),

                            child: FlatButton(
                              onPressed: (){
                                number = 2;
                                Navigator.push(
                                  context,
                                  MaterialPageRoute(builder: (context) => secondpage(number: this.number)),
                                );
                              },
                              child: Center(
                                  child: Text('2',
                                      style: TextStyle(
                                          fontFamily: 'Montserrat',
                                          color: Colors.white,
                                          fontSize: contfontsize))),
                            ),

                          ),

                        ],
                      ),


                    ],
                  ),


                ),

              ],

            ),

          ],
        ),
      ),
    );
  }
}

Second Page

import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'main.dart';
import 'datapage.dart';


// // Use this data to test
// final List wordsenglish =[
//   'to eat',
//   'to speak',
//   'to drink',
// ];
//
// final List words =[
//   'comer',
//   'hablar',
//   'beber',
// ];

// int n = words.length;


class secondpage extends StatefulWidget  {

  secondpage({this.number});
  int number;

  @override
  _secondpageState createState() => _secondpageState(number);
}

class _secondpageState extends State<secondpage> {

  int number;
  _secondpageState(this.number);

  int i = 0;
  double contheight = 150.0;
  double contwidht = 200.0;
  double contfontsize = 25.0;


  // Import data before build (what I want to happen...)   -  Start

  List wordsenglish;
  List words;
  int n;

  void asyncMethod() async {
    await getThedata(number);
  }

  @override
  void initState() {
    asyncMethod();
    super.initState();
  }

  // Import data before build (what I want to happen...)   -  End


  @override
  Widget build(BuildContext context) {

    // Deactivate Android back button
    return new WillPopScope(
      onWillPop: () async => false,

      // return: Scaffold(
      child: Scaffold(

        backgroundColor: Colors.white,
        body: ListView(
          children: <Widget>[

            AppBar(
              elevation: 0,
              title: RichText(
                text: TextSpan(
                  children: <TextSpan>[
                    TextSpan(text: 'Second Page', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25.0)),
                  ],
                ),
              ),
              centerTitle: true,
              backgroundColor: Colors.green,
              leading: new IconButton(
                icon: new Icon(Icons.arrow_back),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => Start()),
                  );
                },
              ),// Override the back arrow button
            ),


            SizedBox(height: 50.0),

            Column(

              children: <Widget>[

                Container(
                  height: 450.0,
                  decoration: BoxDecoration(
                    color: Colors.white,
                  ),

                  child: ListView(
                    primary: false,
                    padding: EdgeInsets.only(top: 40.0, left: 40.0, right: 40.0, bottom: 40.0),
                    children: <Widget>[

                      // Row - 1

                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[

                          Container(
                            height: contheight,
                            width: contwidht,
                            decoration: BoxDecoration(
                                border: Border.all(
                                    color: Colors.green,
                                    style: BorderStyle.solid,
                                    width: 1.0),
                                borderRadius: BorderRadius.only(topRight: Radius.circular(10.0), topLeft: Radius.circular(10.0),
                                    bottomRight: Radius.circular(10.0), bottomLeft: Radius.circular(10.0)),
                                color: Colors.green),

                              child: Center(

                                  child: Text(wordsenglish[i],
                                      style: TextStyle(
                                          fontFamily: 'Montserrat',
                                          color: Colors.white,
                                          fontSize: contfontsize))),

                          ),

                        ],
                      ),



                      SizedBox(height: 30.0),



                      // Row - 2



                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[

                          Container(
                            height: contheight,
                            width: contwidht,
                            decoration: BoxDecoration(
                                border: Border.all(
                                    color: Colors.green,
                                    style: BorderStyle.solid,
                                    width: 1.0),
                                borderRadius: BorderRadius.only(topRight: Radius.circular(10.0), topLeft: Radius.circular(10.0),
                                    bottomRight: Radius.circular(10.0), bottomLeft: Radius.circular(10.0)),
                                color: Colors.green),

                              child: Center(
                                  child: Text(words[i],
                                      style: TextStyle(
                                          fontFamily: 'Montserrat',
                                          color: Colors.white,
                                          fontSize: contfontsize))),


                          ),

                        ],
                      ),


                    ],
                  ),


                ),

              ],

            ),

          ],
        ),

        floatingActionButton: FloatingActionButton(

          onPressed: (){
            setState(() {
              i = (i + 1) % n;
            });
          },

          child: Icon(Icons.arrow_forward, color: Colors.black), // icon
          backgroundColor: Colors.red,

        ),


      ),
    );
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...