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

2024-03-03

我创建了一个类来扩展 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在无状态或有状态小部件中,这样您就可以获得上下文,例如

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

在这里,我使用了一个与您所拥有的非常相似的小部件。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在有状态或无状态小部件之外访问 BuildContext? 的相关文章

随机推荐