Update a Listview in a Statefulwidget Son Flutter

Asked

Viewed 906 times

-1

my main layout features an aapBar, body and bottomNavigationBar through the bottomNavigationBar I update the body with other screens, which show a listview, in the appBar from the main screen I have a delete all button, I can not update the Listview from the open screen in body... The data from this Listview comes from a Futurebuilder, how can I do that? Darpat: Example on the Dartpad

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: MyWidget(),
    );
  }
}

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  _menuSel(String itemText) {
    switch (itemText) {
      case "Marcar todos":
        print("ItemSel Marcar todos");
        break;
      case "Desmarcar todos":
        print("Excluindo registros do banco e atualizar liste view Tela1");
        //função exclui itens do bd atualizar Scaffold/ListView em Tela1 ou Tela2
        break;
    }
  }

  int _indiceNav = 0;

  List<String> itensMenu = ["Marcar todos", "Desmarcar todos"];

  List<Widget> telas = [
    Tela1(),
    Tela2(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: Text("principal"),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.search),
            onPressed: () {},
          ),
          PopupMenuButton<String>(
            onSelected: _menuSel,
            itemBuilder: (context) {
              return itensMenu.map((String item) {
                return PopupMenuItem<String>(
                  value: item,
                  child: Text(item),
                );
              }).toList();
            },
          ),
        ],
      ),
      body: telas[_indiceNav],
      bottomNavigationBar: BottomNavigationBar(
          currentIndex: _indiceNav,
          onTap: (indice) {
            print("pressionado $indice");
            setState(() {
              _indiceNav = indice;
            });
          },
          items: [
            BottomNavigationBarItem(
                title: Text("Tela1"), icon: Icon(Icons.alarm)),
            BottomNavigationBarItem(
                title: Text("Tela2"), icon: Icon(Icons.list)),
          ]),
    );
  }
}

class Tela1 extends StatefulWidget {
  @override
  _Tela1State createState() => _Tela1State();
}

class _Tela1State extends State<Tela1> {
  @override
  Widget build(BuildContext context) {
    return Text("TEla 1");
    //return Scaffold(body: FutureBuilder... ListView...);
  }
}

class Tela2 extends StatefulWidget {
  @override
  _Tela2State createState() => _Tela2State();
}

class _Tela2State extends State<Tela2> {
  @override
  Widget build(BuildContext context) {
    return Text("TEla 2");
    //return Scaffold(body: FutureBuilder... ListView...);
  }
}

In the main project, Tela1, and Tela2 are separate files. In short, I need to access methods and update the open body screens!

  • Your question is a little confused and in your example there is no ListView... Just so I’m clear, you owned one BottomNavigator with 2 tabs, each one with its respective ListView and when you click on the "Delete all" button, you want it to be cleaned ListView of the tab being displayed, that’s it?

  • Exactly, the list is being filled in a Futurebuilder of data of an API, I did not put this part of the code not to be too extensive.. everything is working, including deletion of data from BD, I just need to update the screen where the listview is.

1 answer

0

I created an example for you that can be tested on Dartpad....

Since you haven’t shown us how your complete code is, try applying my example to your case, which you should solve:

import 'package:flutter/material.dart';

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

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

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

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

class _MyAppState extends State<MyApp> {
  final List<String> list = ["Item 2", "Item 2", "Item 3", "Item 4", "Item 5"];

  Future<List<String>> getData(){
    return Future.value(list);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: FutureBuilder(
          future: getData(),
          builder: (_, data){
            return ListView.builder(
              itemCount: list.length,
              itemBuilder: (_, int index){
                return Text(list[index].toString());
              }
            );
          },
        ),
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.close), 
          onPressed: (){
            setState((){
              list.clear();
            });
          },
        )
      ),
    );
  }
}

Observing

This code, my example, is related to only one screen, so you will have to apply in both

  • forgot to put the gist link on the dartpad.

  • I haven’t forgotten, I don’t put a Gist :D link, and all you have to do is copy the code and paste it into the right Dartpad...

  • 1

    I got it, baby.

Browser other questions tagged

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