Home > front end >  How a create a Flutter Frameworking using an MVC pattern
How a create a Flutter Frameworking using an MVC pattern

Time:01-15

This is my github project link https://github.com/RajTechnologiesPvtLtd/flutter-moonlight

How to convert to MVC patten and also given suggetion on it.

Create a Own Framework using MVC pattern

CodePudding user response:

I think the issue might be for you to understand what MVC is. From my understanding the idea of M-V-C is abstract and can be applied using different state management packages in flutter

Model: Model means your classes

class ExampleClass {
 final String name;
   
  ExampleClass({required this.name});
}

Controller: This is where you handle your business logic. You can use Bloc package, Getx package, Provider package etc.

class ExampleController extends ChangeNotifier {
 final ExampleClass? _data;
 ExampleClass? get data => _data;

  setMyClass(ExampleClass value) {
    _data = value;
    notifyListeners();
  }
}

Then View (your UI page): With Controllers, you try to manage all the states for your UIs and just keep your UI simple and only for the UI.

class ExampleUI extends StatelessWidget {
  const ExampleUI({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Consumer<ExampleController>(builder: (ctx, controller, v) {
            return TextButton(
                onPressed: () {
                  controller.setMyClass(ExampleClass(name:'Bhargav Raviya'));
                },
                child: Text(controller.data!= null ? controller.data.name :'Button'));
          }),
          
        ],
      ),
    );
  }
}
  • Related