TypeScript 3.5.2中模块(命名空间)的最佳实践

时间:2019-07-08 21:59:30

标签: typescript

我正在为基本网站使用一些旧的TS代码。我正在更新以通过Gulp进行编译,并且通常会进行现代化。

我应该删除所有这些行吗?我觉得文件现在是模块了,变得多余了?

import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'dart:async';

class AppModel {


 TextEditingController nameController;
 TextEditingController surnameController;
 StreamController<String> fullnameStreamController;


 AppModel() {
   nameController = TextEditingController();
   surnameController = TextEditingController(); 
   fullnameStreamController = StreamController.broadcast();
 }

  update() {
  String fullname;
   if (nameController.text != null && surnameController.text != null) {
     fullname = nameController.text + ' ' + surnameController.text;
   } else {
     fullname = 'Please enter both names';
   }
   fullnameStreamController.add(fullname);
  }
}

GetIt getIt = new GetIt();
final appModel = getIt.get<AppModel>();


void main() {
  getIt.registerSingleton<AppModel>(AppModel());
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(title: 'Singleton Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String text;

  update() {
    setState(() {   
    });
  }

  @override 
  void initState() {  
    text = 'waiting for input';
    appModel.fullnameStreamController.stream.listen((data) {
      text = data;
      update();
    });
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        decoration: BoxDecoration(color: Colors.amberAccent),
        child: Column(
            children: <Widget> [
              Card(
                color: Colors.white,
                child: Text('Name'),
              ),
              Card(
                color: Colors.yellow,
                child: NameTextField()
              ),
              Divider(),
              Card(
                color: Colors.white,
                child: Text('Surname'),
              ),
              Card(
                color: Colors.yellow,
                child: SurnameTextField()
                ),
              OkButton(),
              Card(
                color: Colors.white,
                child: Text('Full name'),
              ),
              Card(
                color: Colors.orange,
                child: FullnameText(text),
                ),
              ],
          ),
        ),
    );
  }
}

class NameTextField extends StatefulWidget {
  NameTextField({Key key}) : super(key: key);

  _NameTextFieldState createState() => _NameTextFieldState();
}

class _NameTextFieldState extends State<NameTextField> {
  @override
  Widget build(BuildContext context) {
    return Container(
       child: TextField(
         controller: appModel.nameController,
       ),
    );
  }
}


class SurnameTextField extends StatefulWidget {
  SurnameTextField({Key key}) : super(key: key);

  _SurnameTextFieldState createState() => _SurnameTextFieldState();
}

class _SurnameTextFieldState extends State<SurnameTextField> {

  @override
  Widget build(BuildContext context) {
    return Container(
       child: TextField(
         controller: appModel.surnameController,
       ),
    );
  }
}

class FullnameText extends StatefulWidget {
  FullnameText(this.text,{Key key}) : super(key: key);
  final String text;


  _FullnameTextState createState() => _FullnameTextState();
}

class _FullnameTextState extends State<FullnameText> {

  @override
  Widget build(BuildContext context) {
    return Container(
       child: Text(widget.text),
    );
  }
}



class OkButton extends StatefulWidget {
  OkButton({Key key}) : super(key: key);

  _OkButtonState createState() => _OkButtonState();
}

class _OkButtonState extends State<OkButton> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white10,
       child: RaisedButton(
         color: Colors.white,
         child: Icon(Icons.check),
         onPressed: () {appModel.update();},
      ),
    );
  }
}

我想我删除了所有这些内容并替换为“常规”导入语句?

module Something { ... }

0 个答案:

没有答案