Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access BuildContext outside of a stateful or stateless widget?

I created a class that extends the AppBar class in Flutter so I can reuse it whenever I need it. My problem is how do I access the Stateful/Stateless widget build context?

class AppBarLayout extends AppBar {

  static final AppController _appController = new AppController();

  final GlobalKey<ScaffoldState> _scaffoldKey;
  final String appBarTitle;

  AppBarLayout(this.appBarTitle,this._scaffoldKey): super(
    title: Text(appBarTitle),
      leading: IconButton(
        onPressed: () => _scaffoldKey.currentState.openDrawer(),
        iconSize: 28,
        icon: Icon(Icons.menu,color: Colors.white),
      ),
      actions: <Widget>[
        IconButton(
          onPressed: () => _appController.signOut().then((_) {
            _appController.navigateTo(context, new GoogleSignView());
          }),
          icon: Icon(Icons.account_box),
          padding: EdgeInsets.all(0.0),
          ),
      ],
  );
}
like image 545
Amine Avatar asked May 02 '19 16:05

Amine


1 Answers

You would need to wrap your Scaffold in a Staless or Stateful widget, so you can get the context, e.g.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarLayout(GlobalKey(debugLabel: 'someLabel'), appBarTitle: 'The Title', context: context,),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}


class AppBarLayout extends AppBar {

  final GlobalKey<ScaffoldState> _scaffoldKey;
  final String appBarTitle;
  final BuildContext context;

  AppBarLayout(this._scaffoldKey, {this.appBarTitle, this.context}): super(
    title: Text(appBarTitle),
      leading: IconButton(
        onPressed: () => _scaffoldKey.currentState.openDrawer(),
        iconSize: 28,
        icon: Icon(Icons.menu,color: Colors.white),
      ),
      actions: <Widget>[
        IconButton(
          onPressed: () {
            print('Button pressed');
          },
          icon: Icon(Icons.account_box),
          padding: EdgeInsets.all(0.0),
          ),
      ],
  );
}

Here I'm using a very similar Widget of what you have.
Hope this help.

like image 56
Hosar Avatar answered Oct 19 '22 19:10

Hosar