Update status without creating widget directly - Flutter

Asked

Viewed 30 times

0

I have in my main.Art a Bottomnavigationbar that should always be visible. Some screens cannot be accessed by Bottomnavigationbar icons.

Main.Dart vvvvv

void main() async {
  await _storage.ready;
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  final int screen;
  final Widget widget;
  final VoidCallback callback;
  MyApp({this.screen, this.widget, this.callback});
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int _selectedScreen;
  static List<Widget> _screens = <Widget>[
    Home(),
    Pesquisar(),
    VerPedidos(),
    MyAccount(),
  ];

  void _onItemTapped(int screen) {
    setState(() {
      _selectedScreen = screen;
    });
  }

  void initState() {
    super.initState();
    widget.screen != null
        ? _selectedScreen = widget.screen
        : _selectedScreen = 0;
    if (widget.widget != null && widget.screen != null) {
      _screens[widget.screen] = widget.widget;
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: SafeArea(
          child: Scaffold(
            body: Center(
              child: _screens.elementAt(_selectedScreen),
            ),
            bottomNavigationBar: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              backgroundColor: ThemeData.dark().primaryColor,
              selectedItemColor: Color(0xffdb6a5f),
              unselectedItemColor: Colors.white,
              items: <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                  icon: Icon(Icons.home),
                  label: 'início',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.search_outlined),
                  label: 'Pesquisar',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.shopping_bag_outlined),
                  label: 'Meus Pedidos',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.account_box_rounded),
                  label: 'Minha Conta',
                ),
              ],
              currentIndex: _selectedScreen,
              onTap: _onItemTapped,
            ),
          ),
        ));
  }
}

usually when I need to change the status of a Children widget I make a callback in the widget. But in that case I don’t "instate" the widget.

child: _screens.elementAt(_selectedScreen)

i call a function that controls the screens being displayed;

how could I do something like:

child: Widget(callback: (){setState((){})}),
  • I don’t know if I understand very well... Your screens are being initialized right at the beginning. Have you tried in their definition already set these callbacks? They are a parameter in the constructor?

  • yes in its definition (widget that cannot be accessed by bottomNavigationBar) I have a Voidcallback that I pass as a parameter to Myapp.dart. Inside the main, I have a list with the bottomNavigationBar screens (_screens) and as scaffold Child I have that expression child: _screens.elementAt(_selectedScreen) . The problem is that by handling a list (and not a widget itself) I cannot take the element (Widget) and pass the parameters to it, which would be the parameter setState((){})

  • Yes, but what prevents you from setting the callback already inside the list, for example? It is the same parameter for all screens?

  • Widgets that cannot be accessed by Bottomnavigatorbar are not within this list. I call these widget by a parameter in the Myapp constructor (screen: 'screen that will be selected on bottomNavigator', widget: (receive widget that cannot be accessed by bottomNa..))

  • and then I check and add it to the corresponding Intel in the list, for example if I’m in the phone number check widget, this widget goes to the Intel where myAccount is in the list.

  • if (widget.widget != null && widget.screen != null) {&#xA; _screens[widget.screen] = widget.widget;&#xA; }

  • To make the state change as you wish, you would have to instill your screen and save the reference of her state, for example, 'State<Homepage',, but it is not a very cool practice... Why you need to change the state of another widget by Bottomnavbar?

  • After reading your reply I have come to the conclusion that I really will have to put all the screens within the list of screens, and not only the bottomNavigatorBar screens. With this I will be able to "instantiate" the widget and call the callback

Show 3 more comments
No answers

Browser other questions tagged

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