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

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

最佳答案

您需要将 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),
          ),
      ],
  );
}

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

关于flutter - 如何在有状态或无状态小部件之外访问 BuildContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55956707/

相关文章:

mobile - 如何动态地连续排列卡片?

flutter - 如何在 flutter 中为 ios 和 android 检测模拟位置

flutter - NestedScrollView with tabbarview scrolling not fired

Windows 上 VS Code 中的 Flutter 代码覆盖率报告?

用于标记的 Flutter Lasso 选择

flutter - Vscode 不将 chrome 显示为设备

flutter - 如何在 flutter 中创建带有透明孔的小部件

flutter |右对齐不起作用

flutter - 是否可以从具有填充的列中排除 1 个小部件?

android - Flutter AlertDialog Navigator 弹出黑屏问题