How To Create Abstract Class Methods Using Flutter Dart

admin_img Posted By Bajarangi soft , Posted On 05-11-2020

To declare abstract class in flutter dart we have to put Abstract keyword on class declaration time. There are some major differences between abstract class and normal class, We would discuss all of them. Abstract class always extends in children class and we cannot directly create object of Abstract class. We have to extends the abstract class in children class then using the children class we can use abstract class methods. In today’s tutorial we would learn about Abstract methods how to declare and call abstract class methods in flutter dart main class.

How To Create Abstract Class Methods Using Flutter Dart

 Abstract Class Methods 
Complete Code For  Abstract Class Methods  In Flutter
main.dart

import 'package:flutter/material.dart';
void main() => runApp(MyApp());
abstract class Message{
  void run();
  void fun();
}

class Second extends Message {
  void run(){
    print('Run Fast......');
  }
  void fun(){
    print('Have Fun......');
  }

}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}
class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  final second = new Second();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.black,
        appBar: AppBar(
          backgroundColor: Colors.red[800],
          title: Text('Abstract Class Methods'),
        ),
        body: Center(
            child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Container(
                      child: RaisedButton(
                        onPressed: () => second.fun(),
                        child: Text('Call Abstract Class Fun Function',
                          textAlign: TextAlign.center,),
                        textColor: Colors.white,
                        color: Colors.red,
                      )
                  ),

                  Container(
                      child: RaisedButton(
                        onPressed: () => second.run(),
                        child: Text('Call Abstract Class Run Function',
                            textAlign: TextAlign.center),
                        textColor: Colors.white,
                        color: Colors.red,
                      )
                  ),
                ])
        )
    );
  }
}

Related Post