abstract class function not able to access anywhere in flutter

Viewed 280

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

Declaring a Styles file in Flutter

But its says this error

enter image description here

2 Answers

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(),
    ),

Make function static:

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

import correct library for TextStyle,

'package:flutter/material.dart'

not

'dart:ui' 
Related