【问题标题】:How to get a context in a function?如何在函数中获取上下文?
【发布时间】:2021-03-31 05:57:43
【问题描述】:

我需要为我的 Navigator 推送获取上下文,我的函数 _navigate 上有这个 Navigator。我尝试了类似 _navigate(BuildContext context) 但我得到了一个错误,比如“type (BuildContext) => dynamic is not a subtype of type() => void。这是 Navigator.push 的第一个上下文我不知道如何得到它。

import 'package:flutter/material.dart';
import 'package:pandora_etna/assistance.dart';
import 'dart:math';
import 'package:vector_math/vector_math.dart' show radians;
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          backgroundColor: Color(0xFFB3E5FC),
          body: SizedBox.expand(child: RadialMenu())),
    );
  }
}

class RadialMenu extends StatefulWidget {
  createState() => _RadialMenuState();
}

class _RadialMenuState extends State<RadialMenu>
    with SingleTickerProviderStateMixin {
  AnimationController controller;

  void initState() {
    super.initState();
    controller =
        AnimationController(duration: Duration(milliseconds: 900), vsync: this);
  }

  Widget build(BuildContext context) {
    return RadialAnimation(controller: controller);
  }
}

class RadialAnimation extends StatelessWidget {
  RadialAnimation({Key key, this.controller})
      : scale = Tween<double>(
          begin: 1.5,
          end: 0.0,
        ).animate(
          CurvedAnimation(parent: controller, curve: Curves.fastOutSlowIn),
        ),
        translation = Tween<double>(
          begin: 0.0,
          end: 100.0,
        ).animate(
          CurvedAnimation(parent: controller, curve: Curves.linear),
        ),
        rotation = Tween<double>(begin: 0.0, end: 360.0).animate(
          CurvedAnimation(
              parent: controller,
              curve: Interval(
                0.0,
                0.7,
                curve: Curves.decelerate,
              )),
        ),
        super(key: key);

  final AnimationController controller;
  final Animation<double> scale;
  final Animation<double> translation;
  final Animation<double> rotation;

  build(context) {
    return AnimatedBuilder(
        animation: controller,
        builder: (context, builder) {
          return Transform.rotate(
              angle: radians(rotation.value),
              child: Stack(alignment: Alignment.center, children: [
                _buildButton(0, _close,
                    color: Color(0xFF29B6F6), icon: FontAwesomeIcons.chartBar),
                _buildButton(60, _close,
                    color: Color(0xFF29B6F6), icon: FontAwesomeIcons.clipboard),
                _buildButton(120, _close,
                    color: Color(0xFF29B6F6), icon: FontAwesomeIcons.chartBar),
                _buildButton(180, _close,
                    color: Color(0xFF29B6F6), icon: FontAwesomeIcons.book),
                _buildButton(240, _close,
                    color: Color(0xFF29B6F6),
                    icon: FontAwesomeIcons.arrowsAltV),
                _buildButton(300, _navigate,
                    color: Color(0xFF29B6F6), icon: FontAwesomeIcons.phoneAlt),
                _buildButton(360, _close,
                    color: Color(0xFF29B6F6),
                    icon: FontAwesomeIcons.compressAlt),
                Transform.scale(
                  scale: scale.value - 1.5,
                  child: FloatingActionButton(
                      child: Icon(FontAwesomeIcons.timesCircle),
                      onPressed: _close,
                      backgroundColor: Color(0xFF29B6F6)),
                ),
                Transform.scale(
                  scale: scale.value,
                  child: FloatingActionButton(
                      child: Icon(FontAwesomeIcons.solidDotCircle),
                      onPressed: _open,
                      backgroundColor: Color(0xFF29B6F6)),
                ),
              ]));
        });
  }

  _buildButton(double angle, Function callback, {Color color, IconData icon}) {
    final double rad = radians(angle);
    return Transform(
        transform: Matrix4.identity()
          ..translate(
              (translation.value) * cos(rad), (translation.value) * sin(rad)),
        child: FloatingActionButton(
            child: Icon(icon), backgroundColor: color, onPressed: callback));
  }

  _open() {
    controller.forward();
  }

  _close() {
    controller.reverse();
  }

  _navigate(BuildContext context) {
    Navigator.push(
        context, new MaterialPageRoute(builder: (context) => Assistance()));
  }
}

【问题讨论】:

    标签: flutter android-context


    【解决方案1】:

    当调用你的函数 _buildButton 时,你必须将 _navigate 函数包装到另一个函数中,因为它需要你当前的上下文才能工作
    试试这个

    _buildButton(300, () {
       _navigate(context);
    }, color: Color(0xFF29B6F6), icon: FontAwesomeIcons.phoneAlt),
    

    【讨论】:

    • 感谢您的回答!
    【解决方案2】:

    我认为这是因为您在调用 _buildButton 时丢失了 context 参数。

    /// By writing your Function parameter like this you will pass the
    /// BuildContext parameter needed.
    _buildButton(
       300,
       () => _navigate(context),
       color: Color(0xFF29B6F6),
       icon: FontAwesomeIcons.phoneAlt
    ),
    

    【讨论】:

    • 这似乎有效,但我收到一个错误“子树中有多个英雄共享相同的标签”
    • 这与您的导航无关。
    【解决方案3】:

    您需要在引用时将 BuildContext 传递给 _navigate 函数。您可以使用匿名函数来执行此操作。 _buildButton(300,(context) =&gt; _navigate(context), ...

    【讨论】:

    • 这似乎有效,但我收到一个错误“子树中有多个英雄共享相同的标签”
    • 我从来没有遇到过这个错误,但从stackoverflow.com/questions/51125024/… 来看,这可能与您在同一屏幕上有两个 FloatingActionButtons 的事实有关。如果您一次只需要一个堆栈子级,则可以尝试使用 IndexStack。
    • 是的,我在网上寻找答案。我用一些 heroTag 修复它。 Flutter 太奇怪了,好像 Android Studio 更容易了哈哈。非常感谢您的帮助!
    【解决方案4】:

    Try out below example for more idea:-

     _buildButton(300, _navigate(context),   // Added Context here,It will contains context of build function 
                        color: Color(0xFF29B6F6), icon: FontAwesomeIcons.phoneAlt),
    
      _navigate(BuildContext context) {
        Navigator.push(
            context, new MaterialPageRoute(builder: (context) => Assistance()));
      }
    

    【讨论】:

    • 感谢您的回答!
    【解决方案5】:

    它不起作用,因为您没有给出函数方法的上下文参数。我想是的

          _buildButton(300, _navigate(context),
                    color: Color(0xFF29B6F6), icon: FontAwesomeIcons.phoneAlt),
    

    【讨论】:

    • 感谢您的回答!
    猜你喜欢
    • 2021-01-10
    • 2020-05-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多