0

So i have this toggle() method in the Stateful SideBar class

class SideBar extends StatefulWidget { const SideBar({super.key}); @override State<SideBar> createState() => _SideBarState(); } class _SideBarState extends State<SideBar> with SingleTickerProviderStateMixin{ void toggle() { if (_controller.isCompleted) { _controller.reverse(); } else {_controller.forward();} } } 

and i want to use it in

class SideBarWidget extends StatelessWidget { SideBarWidget({Key? key}) : super(key: key); final SideBar sideBarWidget = SideBar(...); void toggle() { // here i want to use the toggle() method } @override Widget build(BuildContext context) { return sideBarWidget; } } 

I cannot use sideBarWidget.toggle() I also cannot pass it as a parameter becasue the _controller is in the SideBar() widget

0

3 Answers 3

1

There are many ways, but the most common and simple is to create an instance of the SideBar class inside the SideBarWidget class, for example:

var side = SideBar(); 

And then you can access the toggle() method like this: side.toggle().

Sign up to request clarification or add additional context in comments.

2 Comments

I've tried that as you see in the SideBarWidget class but I cant access the toggle method..
I need more of your code and the error your IDE shows.
0

remove underscore from SideBarState

to use method of SideBarState in SideBarWidget use: SideBarState().toggle();

**screen1.dart** class ParentWidget extends StatefulWidget { const ParentWidget({Key? key}) : super(key: key); @override State<ParentWidget> createState() => ParentWidgetState(); } class ParentWidgetState extends State<ParentWidget> { void printData() { print("parent"); } @override Widget build(BuildContext context) { return const Placeholder(); } } **screen2.dart** import 'package:demo/screen_1.dart'; import 'package:flutter/material.dart'; class ChildWidget extends StatelessWidget { const ChildWidget({Key? key}) : super(key: key); @override Widget build(BuildContext context) { ParentWidgetState().printData(); return const Placeholder(); } } 

Comments

0

One way is to give the SideBar a GlobalKey and get the state from the key afterwards. An example:

class SideBar extends StatefulWidget { const SideBar({super.key}); @override State<SideBar> createState() => SideBarState(); } class SideBarState extends State<SideBar> with SingleTickerProviderStateMixin{ @override Widget build(BuildContext context) { return Container(); } void toggle() { print('toggle'); } } class SideBarWidget extends StatelessWidget { SideBarWidget({Key? key}) : super(key: key); final GlobalKey<SideBarState> sideBarKey = GlobalKey(); late final SideBar sideBarWidget = SideBar(key: sideBarKey); void toggle() { sideBarKey.currentState?.toggle(); } @override Widget build(BuildContext context) { return Column( children: [ TextButton(onPressed: toggle, child: const Text('click')), sideBarWidget, ], ); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.