Navjot Aulakh
Navjot Aulakh

Reputation: 51

Flutter want to pass context to Widget type

Widget cusSearchBar = Text("Supplier");

is there any way to pass the context to cusSearchBar. Help will be appreciated.

Upvotes: 1

Views: 1908

Answers (3)

harsha.kuruwita
harsha.kuruwita

Reputation: 1331

Extract as Widget

class CusSearchBar extends StatelessWidget {
      const CusSearchBar({
        Key? key,
      }) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Text('Supplier');
      }
    }

Upvotes: 3

Anmol Mishra
Anmol Mishra

Reputation: 332

Widget cusSearchBar(BuildContext context){
return Text("Supplier");
}

Upvotes: 2

Jim
Jim

Reputation: 7601

try this:

Widget cusSearchBar = _cusSearchBar(context, 'Supplier');

Widget _cusSearchBar(BuildContext context, String txt) {
  //use of context...

  return Text(txt);
}

Upvotes: 2

Related Questions