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. To create a method in abstract class we have to instantiate method name with return keyword and ends the name along with semicolon. Now using inheritance with extends keyword we can easily inherit all the Abstract class methods in children class. So in this tutorial we would learn about Create Use Abstract Class Methods in Flutter Dart Example Android iOS Tutorial.
Contents in this project Create Use Abstract Class Methods in Flutter Dart Android iOS Example Tutorial:
1. Import material.dart package in your app’s main.dart file.
1 |
import 'package:flutter/material.dart'; |
2. Create void main runApp() method and now we would call our main MyApp main class.
1 |
void main() => runApp(MyApp()); |
3. Create a Abstract class named as Message with 2 Abstract class methods. In Abstract class we are making 2 functions named as run() and fun(). We are not defining function body in abstract class because this is the main purpose of Abstract class. So we can use these function names anywhere according to our requirement.
1 2 3 4 5 6 7 |
abstract class Message{ void run(); void fun(); } |
4. Create another class named as Second and extends or inherits Message class inside it. Now we can override both Abstract class methods according to our needs and declare the function body part.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Second extends Message { void run(){ print('Run Fast............'); } void fun(){ print('Have Fun.............'); } } |
5. Create our main MyApp class extends with StatelessWidget. This is our main View class.
1 2 3 4 |
class MyApp extends StatelessWidget { } |
6. Creating Second class object using new Keyword named as second. We would call all abstract class methods using second class object.
1 |
final second = new Second(); |
7. Creating Widget build area in MyApp class. Now we would make Two Raised Button widget in main class and call both Abstract class methods on Button onPress event. We are printing a simple console message in both functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( margin: const EdgeInsets.fromLTRB(0, 0, 0, 0), child: RaisedButton( onPressed: () => second.fun(), child: Text('Call Abstract Class Fun Function', textAlign: TextAlign.center,), textColor: Colors.white, color: Colors.pink, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), Container( margin: const EdgeInsets.fromLTRB(0, 10, 0, 0), child: RaisedButton( onPressed: () => second.run(), child: Text('Call Abstract Class Run Function', textAlign: TextAlign.center), textColor: Colors.white, color: Colors.cyan, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), ]) ) ) ); } |
8. Complete source code for main.dart file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
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 { final second = new Second(); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( margin: const EdgeInsets.fromLTRB(0, 0, 0, 0), child: RaisedButton( onPressed: () => second.fun(), child: Text('Call Abstract Class Fun Function', textAlign: TextAlign.center,), textColor: Colors.white, color: Colors.pink, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), Container( margin: const EdgeInsets.fromLTRB(0, 10, 0, 0), child: RaisedButton( onPressed: () => second.run(), child: Text('Call Abstract Class Run Function', textAlign: TextAlign.center), textColor: Colors.white, color: Colors.cyan, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), ]) ) ) ); } } |
