flutter获取全局的context

BuildContext

BuildContext 实际上是 Element 对象。BuildContext 接口用于阻止对 Element 对象的直接操作。

我们日常开发中,一般接触的都是 Widget,并没有使用到 Element,其实我们也在一直操作着 Element,BuildContext 对象实际上就是 Element 对象, Element 实现了 BuildContext,告诉了使用者控件在哪里、可以做什么。BuildContext 接口设计用于阻止对 Element 对象的直接操作

通过navigatorKey的方式 

void main() {
  runApp(MyApp());
}
 
final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();
 
class MyApp extends StatelessWidget {
  MyApp() {
  }
 
  // This widget is the view.common.root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: navigatorKey,
    );
  }
}

获取context:

 Context context = navigatorKey.currentState.overlay.context

注意:通过这种方式获取的context在某些情况下(使用Overlays(比如Dialog)、MediaQuery等)需要在如下代码里面使用如:

 /// 跳转到登录界面   全局context
          Future.delayed(const Duration(microseconds: 0),(){
            NavigatorUtils.push(
                navigatorKey.currentState.overlay.context, LoginRouter.loginPage);

          });

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Flutter中实现全局计时可以使用`Ticker`类和`TickerProvider`类。下面是一个简单的例子: 1. 创建一个`CountdownTimer`类,该类将使用`Ticker`来定时更新计时器。 ```dart class CountdownTimer { StreamController<int> _streamController; int _countdownTime = 0; Ticker _ticker; CountdownTimer() { _streamController = StreamController<int>(); _ticker = Ticker(_onTick); } Stream<int> get stream => _streamController.stream; void start(int countdownTime) { _countdownTime = countdownTime; _ticker.start(); } void _onTick(Duration duration) { _countdownTime--; _streamController.add(_countdownTime); if (_countdownTime <= 0) { _ticker.stop(); _streamController.close(); } } void dispose() { _ticker.dispose(); _streamController.close(); } } ``` 2. 在`main.dart`中,创建一个`StatefulWidget`,并用`CountdownTimer`类来实现全局计时。 ```dart class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin { CountdownTimer _countdownTimer; @override void initState() { super.initState(); _countdownTimer = CountdownTimer(); _countdownTimer.stream.listen((countdownTime) { setState(() { // 更新UI }); }); } @override void dispose() { _countdownTimer.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Text( _countdownTimer._countdownTime.toString(), style: Theme.of(context).textTheme.headline4, ), ), floatingActionButton: FloatingActionButton( onPressed: () { _countdownTimer.start(60); // 开始计时,60秒 }, tooltip: 'Start', child: Icon(Icons.play_arrow), ), ); } } ``` 在这个例子中,我们使用了Flutter的`Stream`和`StreamController`来实现计时器的更新。`CountdownTimer`类通过Ticker定时器来更新计时器,并且使用Stream和StreamController来将更新通知给UI。在`main.dart`中,我们创建了一个带有`TickerProviderStateMixin`的`StatefulWidget`,以便我们可以使用Flutter的`Ticker`类。我们还通过监听`CountdownTimer`类的流来更新UI。最后,在`floatingActionButton`的`onPressed`回调中,我们开始了计时器,并将其设置为60秒。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值