简体   繁体   English

其他文件中的单独小部件颤动

[英]Separate widgets in other files flutter

I want to make my code neater but I have a problem when I separate widgets that I use often in 1 file我想让我的代码更整洁,但是当我在 1 个文件中分离我经常使用的小部件时遇到问题

here is it my main widget这是我的主要小部件

import 'package:a_tiket/Helpers/widget_helper.dart';
class LoginPage extends StatefulWidget {    
  @override
  _LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {

  bool _isLoading = false;
  var _message = '';
  var _hasError = false;

  @override
  Widget build(BuildContext context) {
    return
      _isLoading ?
      _loadingWidget(context)
          :
      Scaffold(
          body: SingleChildScrollView(
              child: Container(
                    ),
                  ],
                ),
              )
          )
      )
    ;
  }
}

this is my widget_helper.dart这是我的 widget_helper.dart

Widget _loadingWidget (BuildContext context){
  return Scaffold(
    body: Center(
      child: CircularProgressIndicator(
        backgroundColor: ACCENT_COLOR,
        valueColor: new AlwaysStoppedAnimation<Color>(PRIMARY_COLOR),
      ),
    ),
  );
}

the problem is i got some error.问题是我有一些错误。 i have add import for widget_helper but still get error我已经为 widget_helper 添加了导入,但仍然出现错误

lib/Pages/loginPage.dart:193:7: Error: The method '_loadingWidget' isn't defined for the class '_LoginPageState'.

what should i do?我该怎么办? i just want to make the code neater我只是想让代码更整洁

please remove underline请删除underline
change from从改变

_loadingWidget(context)

to

loadingWidget(context)

There are a few issues with your code:您的代码存在一些问题:

  • For such a small piece of code like showing a CircularProgressIndicator you should not be putting a method in a separate file.对于像显示 CircularProgressIndicator 这样的一小段代码,您不应该将方法放在单独的文件中。 Instead of making your code "neater", you are making it harder to read.不是让你的代码“更整洁”,而是让它更难阅读。 If you really want to have it in a separate file, create a Stateless widget that shows the code you want.如果你真的想把它放在一个单独的文件中,创建一个无状态小部件来显示你想要的代码。 But then again you are just using a CircularProgressIndicator.但话说回来,您只是在使用 CircularProgressIndicator。 You aren't saving any code, just creating more unnecessary code.您没有保存任何代码,只是创建了更多不必要的代码。
  • You already have a Scaffold where your are going to show the CircularProgressIndicator.您已经有一个 Scaffold,您将在其中显示 CircularProgressIndicator。 You don't need to have another one.你不需要有另一个。 It's not doing anything.它什么都不做。
  • While Dart uses camelCase for variable naming, file names use snake_case. Dart 使用驼峰命名法来命名变量,而文件名使用蛇形大小写。 Try to use it when naming files.命名文件时尝试使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM