如何从小部件访问上下文

时间:2018-10-03 14:19:28

标签: dart flutter

我正在处理Flutter应用程序,并且有一个Inherited Widget类。 我的InheritedWidget类中有一个函数需要访问BuildContext。 是否可以从小部件对象访问context(BuildContext)? 如果是,请问如何访问它。

class ApiClient extends InheritedWidget{

  final String baseUrl;

  ApiClient({
    @required this.baseUrl,
    @required Widget child
  }):super(child:child);

  static ApiClient of(BuildContext context) {
    return context.inheritFromWidgetOfExactType(ApiClient);
  }

  Future<http.Response> login(String email, String password){

    Map<String, String> body = {
       "username":email,
       "password":password,
       "client_secret": //This I want to read from an EnvironmentConfig
 //file which is also an InheritedWidget And In order to read that I need context,
    };
    return _network.post(baseUrl+ApiEndpoints.LOGIN, body:json.encode(body), headers: ApiHeaders.headers);
  }



  @override
  bool updateShouldNotify(InheritedWidget oldWidget) => false;


}

0 个答案:

没有答案