如何在有状态或无状态窗口小部件之外访问BuildContext?

时间:2019-05-02 16:31:16

标签: flutter flutter-layout

我创建了一个在Flutter中扩展AppBar类的类,以便可以在需要时重用它。 我的问题是如何访问有状态/无状态窗口小部件构建上下文?

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),
          ),
      ],
  );
}

1 个答案:

答案 0 :(得分:1)

您需要将 Scaffold 包装在Staless或Stateful小部件中,以便获取上下文,例如

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),
          ),
      ],
  );
}

在这里,我使用的是您所拥有的非常相似的小部件。
希望有帮助。