简体   繁体   English

名称与 Flutter 中的自定义小部件冲突

[英]Name conflicts with custom widgets in Flutter

What is the best way to make custom widgets in Flutter that don't conflict with those exported by the material package?在 Flutter 中制作与材料 package 导出的小部件不冲突的自定义小部件的最佳方法是什么? Specifically: I'm trying to make a design system based on atomic design .具体来说:我正在尝试制作一个基于atomic design的设计系统。 So, let's say I want my own Text and Card widgets.所以,假设我想要自己的TextCard小部件。 I can't name them Text or Card because both of these are exported by the material package.我不能将它们命名为TextCard ,因为它们都是由材料 package 导出的。

Some options that I've considered:我考虑过的一些选项:

  1. Creating a ui library and importing using as创建一个ui库并使用as导入
// components/atoms/Text.dart
import "package:flutter/material.dart" hide Text;
import "package:flutter/material.dart" as material show Text;

class Text extends StatelessWidget {
  Text(String content);

  Widget build(BuildContext context) {
    // Imagine some more complicated styling specific to my design system
    return material.Text(content);
  }
}
// components/ui.dart
library ui

export "package:my_app/components/atoms/Text";
// screens/HomeScreen.dart

import "package:flutter/material.dart";
import "package:my_app/components/ui" as ui;

class HomeScreen extends StatelessWidget {
  Widget build(BuildContext context) {
    // Example usage
    return ui.Text(content);
  }
}

This doesn't feel great... First, I need to remember to include as whenever I import the ui library, and second, it's slightly annoying to have to hide and show classes in my Text implementation.这感觉不太好......首先,我需要记住在导入ui库时包含as其次,在我的Text实现中hideshow类有点烦人。

  1. Just hide the conflicting classes只是hide冲突的类
// screens/HomeScreen.dart

import "package:flutter/material.dart" hide Text;
import "package:my_app/components/Text";

class HomeScreen extends StatelessWidget {
  Widget build(BuildContext context) {
    // Example usage
    return Text(content);
  }
}

Similar to option (1), I don't want to have to remember to hide all the classes that conflict with my custom widgets.与选项 (1) 类似,我不想记住hide所有与我的自定义小部件冲突的类。

  1. Just use different names只需使用不同的名称

Maybe AppText or ThemeText or UIText or AtomText ?也许AppTextThemeTextUITextAtomText This doesn't feel great either, especially for some of the other widgets like IconButton .这也感觉不太好,尤其是对于像IconButton这样的其他一些小部件。

Is there a convention around this, or some decent solution?有没有围绕这个的约定,或者一些体面的解决方案? I haven't seen any custom UI libraries, but this is mostly useful for teams that want to reuse widgets across apps and keep a consistent style.我还没有看到任何自定义 UI 库,但这对于希望跨应用程序重用小部件并保持一致风格的团队非常有用。

From what I gather, you want your custom Text class to do the exact same thing as a normal Text widget, but with a different design.据我所知,您希望您的自定义文本 class 与普通文本小部件执行完全相同的操作,但设计不同。 If that really is the case, I propose that you change the ThemeData of your project.如果确实如此,我建议您更改项目的 ThemeData。 The MaterialApp widget takes a theme argument, so you can style your TextTheme, CardTheme, IconButtonTheme etc. in there. MaterialApp小部件采用主题参数,因此您可以在其中设置 TextTheme、CardTheme、IconButtonTheme 等样式。 This would prevent any naming conflicts, save you a lot of coding, and makes it easy to tweak.这将防止任何命名冲突,为您节省大量编码,并使其易于调整。

Things such as TextTheme allow for different types of styles, such as headlines, bodytext, etc., as described here .诸如 TextTheme 之类的东西允许不同类型的 styles,例如标题、正文等,如此 所述。

  1. You can add 'My' at the beginning of the widget, for example, 'MyText' instead of 'Text' and 'MyCard' instead of 'Card'.您可以在小部件的开头添加“我的”,例如,用“MyText”代替“Text”,用“MyCard”代替“Card”。

  2. You can use a tricky way, in my case I use an intentionally wrong dictation for naming.您可以使用一种棘手的方式,在我的情况下,我故意使用错误的听写来命名。 For instance, instead of 'Card' you can name 'Cart', or instead of 'Text', you can name tekst.例如,您可以将“Card”命名为“Cart”,或者将“Text”命名为 tekst。

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

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