Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a mixin for a generic class State<T extends StatefulWidget>

I'm in need to write an extension for Flutter's State<T extends StatefulWidget> so I can use a function in all of my States, let's say showSnackBar("Hello world", 5). I tried writing a mixin

mixin BaseState on State<ProfileScreen> {
  final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();

  void showSnackBar(String text) {
    setState(() {
      scaffoldKey.currentState.showSnackBar(new SnackBar(
          content: new Row(
            children: <Widget>[
              new CircularProgressIndicator(),
              new Text(text == null ? "  Logging in" : "      $text")
            ],
          )));
    });
  }

  void hideSnackBar() {
    setState(() {
      scaffoldKey.currentState.hideCurrentSnackBar();
    });
  }
}

As you can see, it is now mixed on State<ProfileScreen>. It's a problem because I only can use this mixin in class ProfileScreenState extends State<ProfileScreen>. Without the type notation I end up with an error:

error: The class 'ProfileScreenState' cannot implement both 'State<ProfileScreen>' and 'State<StatefulWidget>' because the type arguments are different. (conflicting_generic_interfaces at [mobile] lib/Screens/profile.dart:17)
error: Type parameters could not be inferred for the mixin 'BaseState' because no type parameter substitution could be found matching the mixin's supertype constraints (mixin_inference_no_possible_substitution at [mobile] lib/Screens/profile.dart:17)

I tried to Google a lot, seen questions like these but without a success.

And yes I know composition is preferred over inheritance in Flutter, but I think this is a thing I don't know I would make work with composition and I feel it will be OK with inheritance.

like image 677
Martin. Avatar asked Feb 10 '19 15:02

Martin.


People also ask

Which keyword is used for the mixin to implement in a class?

Mixins can be used in two ways, the first case is when we want to make use of class code in such a way that the class doesn't have any constructor and the object of the class is extended. In such a case, we use the with keyword.

What is state mixin in Flutter?

Mixin for State classes that require knowledge of changing MaterialState values for their child widgets. This mixin does nothing by mere application to a State class, but is helpful when writing build methods that include child InkWell, GestureDetector, MouseRegion, or Focus widgets.

How do you make a mixin Flutter?

To implement a mixin , create a class that extends Object and declares no constructors. Unless you want your mixin to be usable as a regular class, use the mixin keyword instead of class . In this way, your Car can run , but cannot be ' handleControlled '.

Can Mixins have constructors?

It's common to have mixins that want to provide a specific constructor. These will usually be applied last to create a concrete class. This is impossible now, so you have to create a new concrete class with only a constructor to give it the correct signature.


1 Answers

I think you are looking for something like https://github.com/flutter/flutter/blob/3fbd140e77601686acb336cd2af2838d03ba0572/packages/flutter/lib/src/widgets/ticker_provider.dart#L155

mixin BaseState<T extends StatefulWidget> on State<T> {
like image 150
Günter Zöchbauer Avatar answered Nov 15 '22 10:11

Günter Zöchbauer