【问题标题】:Make a list out of arguments received from second page in the home page列出从主页第二页收到的参数
【发布时间】:2021-01-07 03:39:19
【问题描述】:

我是 Flutter 的新手,我正在创建一个笔记应用程序。我想将新笔记的标题和文本从“新笔记”页面传递到所有其他笔记所在的主页。

我想将标题和文本传递到第一页,随着笔记数量的增加,我可以使用列表视图创建已保存笔记的列表。我究竟做错了什么? 这是我的主页代码:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:notes/newnote.dart';

void main() {
  runApp(MaterialApp(home: MyApp(), initialRoute: 'main.dart', routes: {
    '/home': (context) => MyApp(),
    '/newnote': (context) => NewNote(),
  }));
}


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Notes',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: FirstPage(),
    );
  }
}
int x = 0;

class FirstPage extends StatefulWidget {
   String title ;
   String text ;

  FirstPage({Key key, @required this.title,@required this.text}) : super(key: key);
  void pri() {print(title);}
  @override
  _MyAppState createState() => _MyAppState();
}

Map data = {};

class _MyAppState extends State<FirstPage> {
  @override
  Widget build(BuildContext context) {
    //final dat args = ModalRoute.of(context).settings.arguments;
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "Note-It!",
          textAlign: TextAlign.center,
          style: TextStyle(color: Colors.white),
        ),
        backgroundColor: Colors.black,
      ),
      body: Column(
        //
        children: <Widget>[
          Padding(
            padding: EdgeInsets.fromLTRB(19.0, 19.0, 19.0, 19.0),
          ),

          Expanded(
            child: ListView.builder(
                itemCount: x,
                itemBuilder: (context, index) {
                  return ListTile(
                    title: Text(widget.title),
                    // onTap: () {
                    //   Navigator.push( context,MaterialPageRoute( builder: (context) =>
                    //   DetailScreen(notedata: datas[index])));
                    // }
                  );
                }),
          ),

          Align(
            alignment: Alignment.bottomRight,
            child: FloatingActionButton(
                child: Icon(Icons.add),
                backgroundColor: Colors.black,
                onPressed: () {
                  setState(() {
                    Navigator.push(context, new MaterialPageRoute(
                      builder: (context) =>
                          new NewNote(t1: null, t2: null)
                    ));
                  });
                }),
          ),
        ],
      ),
    );
  }
}

这是我的“新笔记”页面代码

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:notes/main.dart';

void main() {
  runApp(MaterialApp(home: NewNote()));
}

final fromController1 = TextEditingController();
final fromController2 = TextEditingController();
var instance;

class NewNote extends StatelessWidget {
   String t1; //requesting data here
  String t2;
  NewNote({Key key, @required this.t1,@required this.t2}) : super(key: key);

  // final String d;
  //
  // NewNote({
  //   Key key,
  //   @required this.d,
  // }) : super(key: key);

  @override

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("New Note"),
        backgroundColor: Colors.black,
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: EdgeInsets.fromLTRB(19.0, 19.0, 19.0, 19.0),
          child: SingleChildScrollView(
            child: Column(children: <Widget>[
              TextField(
                controller: fromController1,
                decoration: InputDecoration(
                    border: OutlineInputBorder(), labelText: "Title"),
                style: TextStyle(fontSize: 28.0),
              ),
              Padding(padding: EdgeInsets.fromLTRB(19.0, 19.0, 19.0, 0.0)),
              TextField(
                controller: fromController2,
                decoration: InputDecoration(
                    border: OutlineInputBorder(), labelText: "Text"),
                style: TextStyle(fontSize: 20.0),
                maxLines: null,
              ),
              Padding(padding: EdgeInsets.fromLTRB(19.0, 19.0, 19.0, 0.0)),
              Align(
                alignment: Alignment.bottomCenter,
                child: FloatingActionButton.extended(
                  label: Text("Save Note"),
                  icon: Icon(Icons.save),
                  backgroundColor: Colors.black,
                  onPressed: () {
                    x++;
                    t1 = fromController1.text;
                    t2 = fromController2.text;
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => MyApp(), //pass your title and text to NewNote
                      ),
                    ).then((value){
                      FirstPage(title: t1, text: t2);

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

【问题讨论】:

标签: android flutter dart


【解决方案1】:

您已经在新笔记页面的第一页中传递了标题和字符串。您只是没有使用正确的语法将其显示在屏幕上。 改变这个

   title: Text(
          "Note-It!",
          textAlign: TextAlign.center,
          style: TextStyle(color: Colors.white),
        ),

   title: Text(
          this.widget.title,
          textAlign: TextAlign.center,
          style: TextStyle(color: Colors.white),
        ), 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-02
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    相关资源
    最近更新 更多