【问题标题】:How do I access BuildContext outside of a stateful or stateless widget?如何在有状态或无状态小部件之外访问 BuildContext?
【发布时间】:2019-09-21 05:26:54
【问题描述】:

我创建了一个类来扩展 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),
          ),
      ],
  );
}

【问题讨论】:

  • 你需要在构造函数中传递上下文,就像你传递 appBarTitle 一样
  • @Hosar 我使用以下代码做到了这一点:static BuildContext _context = HomeState().context; 但是当我在构造函数中传递 _context 并运行应用程序时出现错误:Reading static variable '_context@25445118' during its initialization
  • 你在哪里使用 AppBarLayout,在 Scaffold 中?
  • @Hosar 是的,我的意图是让它可用于所有屏幕。只有标题是需要更改的属性,当然还有一些抽屉操作的脚手架键
  • 为什么需要获取另一个小部件的上下文? AppBar 本身就是一个小部件,所以它有自己的 context 字段。

标签: flutter flutter-layout


【解决方案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),
          ),
      ],
  );
}

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

【讨论】:

猜你喜欢
  • 2021-05-19
  • 2021-02-16
  • 2021-09-21
  • 2021-06-10
  • 2019-10-06
  • 2021-06-07
  • 2021-12-15
  • 2021-06-11
  • 2018-09-01
相关资源
最近更新 更多