Home > OS >  abstract class function not able to access anywhere in flutter
abstract class function not able to access anywhere in flutter

Time:02-01

I m trying to use text style using abstract function like this

import 'dart:ui';
abstract class AppStyles {
  TextStyle getNameStyle() {
  return  TextStyle(
    fontSize: 40.0,
   );
}
}

this style I m trying to access like this

const Text(
          'Welcome to Flutter app',
          style: AppStyles.getNameStyle(),
        ),

Refer to this example also

enter image description here

CodePudding user response:

Make function static:

  abstract class AppStyles {
     static TextStyle getNameStyle() {
       return  TextStyle(
         fontSize: 40.0,
       );
     }
  }

CodePudding user response:

Either create function static

abstract class AppStyles {
     static TextStyle getNameStyle() {
       return  TextStyle(
         fontSize: 40.0,
       );
     }
  }

or create subclass of AppStyles and instantiate it:

class AppStylesImpl extends AppStyles {}

const Text(
      'Welcome to Flutter app',
      style: AppStylesImpl().getNameStyle(),
    ),
  •  Tags:  
  • Related