Create an array list in the flutter

Asked

Viewed 4,227 times

0

I have two pages. On the first page I want to return a list. I want to insert in this list the values that come from the second page. So the flow is more or less the following: I enter the second page, send the value to the first page, save to an array type variable and display all values in a list.

The problem is that I can’t create the list. I can only display the last data I send instead of the list of everything I’ve sent.

Here’s the code:

Front page

import 'package:flutter/material.dart';

class PageTwo extends StatefulWidget {
  final String descricao;
  const PageTwo({Key key, this.descricao}) : super(key: key);

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

class _PageTwoState extends State<PageTwo> {
  final List<String> listDescricao = [];

  @override
  void initState() {
    listDescricao.add(widget.descricao);
    super.initState();
  }

  //List images items
  Widget imgListItem() {
    return ListView.builder(
        itemCount: listDescricao.length,
        itemBuilder: (BuildContext context, int index) {
          return widget.descricao == null
              ? Container()
              : Text(listDescricao[index]);
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: imgListItem(),
    );
  }
}

Second page

import 'package:flutter/material.dart';

class PageOne extends StatefulWidget {
  @override
  _PageOneState createState() => _PageOneState();
}

class _PageOneState extends State<PageOne> {
  final GlobalKey<ScaffoldState> _scaffoldkey = new GlobalKey<ScaffoldState>();
  final inputDescricaoController = TextEditingController();

  //send image
  doSend() async {
    String descricao = inputDescricaoController.text ??= "";

    Navigator.of(context).pushAndRemoveUntil(
        MaterialPageRoute(
            builder: (BuildContext context) => PageTwo(descricao: descricao)),
        (Route<dynamic> route) => false);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: <Widget>[
          TextField(
                controller: inputDescricaoController,
            ),
          RaisedButton(
        child: Column(
          children: <Widget>[

            Text(
              'send',
            ),
          ],
        ),
        onPressed: () async {
          doSend();
        },
      ),
        ]
      ) 

    );
  }
}
  • Is that right? The "Second Page" is the PageOne? And how do you open the PageOne? For in the PageTwo there is no reference to open the PageOne and it takes a String parameter so you would always have to open the other page to pass a new value...

  • the second page is pageTwo itself. To access pageTwo has a button, but at the time of summarizing the code I forgot to put, but basically it is a button with a 'push' to pageTwo

  • Adjust your question then, please... The title you placed on the page does not match the class name and add the button also for better understanding. Click on the button Edit which is just above the comments.

1 answer

0


I created an example of how you can do what you want, you can run it by Dartpad to test

import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatefulWidget {
  const MyWidget({ Key key }) : super(key: key);

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

class _MyWidgetState extends State<MyWidget> {

  List<String> dataList = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: ListView.builder(
          itemCount: dataList.length,
          itemBuilder: (_, int index){
            return Text(dataList[index]);
          },
        )
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: (){
          Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => ScreenTwo()),
          ).then((result){
            setState((){
              dataList.add(result);
            });
          });
        }
      ),
    );
  }
}

class ScreenTwo extends StatefulWidget {
  const ScreenTwo({ Key key }) : super(key: key);

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

class _ScreenTwoState extends State<ScreenTwo> {

  TextEditingController controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Column(
          children: <Widget>[
            TextField(
              controller: controller
            ),
            RaisedButton(
              child: Text("Salvar"),
              onPressed: (){
                Navigator.of(context).pop(controller.text);
              },
            )
          ],
        )
      ),
    );
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.