Home > Back-end >  Do you guys normally create a new .dart file for the next screen or stick the coding to the same pag
Do you guys normally create a new .dart file for the next screen or stick the coding to the same pag

Time:11-21

i am new to coding, do you guys create a new dart file for the next screen?

currently i have a homepage with have a start button , do i create a new .dart like a settingpage.dart or continue on homepage?

please advise , thanks.

If i create a new .dart file , what are the code i need to include ?

CodePudding user response:

To navigate to a new page, route is used in flutter. You can create Material App or Cupertino App with Stateless Widget and Stateful Widget. You can create multiple class in one dart file. However, it is more efficient to manage the code by dividing it into separate dart files.

There is no right answer, but learn about design-pattern and use the method that is appropriate for your project!

CodePudding user response:

Yes you can make a new class in the same Dart file, but the problem with this approach is that it is fine until you are having three or four classes in your whole project (i.e. you're creating single page or two page applications). Any bigger than that and you need to create various .dart files to be able to manage all the code so that you can distinguish any errors quickly. Also it makes easier for any other person to understand your code, I'll give you a few good articles on how to write code, what are the best code practices you can follow.

  1. https://simonauer.medium.com/best-practices-basic-tips-on-writing-good-flutter-code-dd05d56bf27c
  2. https://codewithandrea.com/videos/top-dart-tips-and-tricks-for-flutter-devs/

Also if you are creating a new .dart file you need to add two things before you'll be able to use that file :

  1. import 'package:flutter/material.dart';
  2. stful or stless depending on whether you need a stateful or a stateless class.

which is going to look like this when all put together:

import 'package:flutter/material.dart';

class ClassName extends StatefulWidget {
  const ClassName({Key? key}) : super(key: key);

  @override
  State<ClassName> createState() => _ClassNameState();
}

class _ClassNameState extends State<ClassName> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
  • Related