【问题标题】:How to pass data to stateless widget calling from another stateless widget?如何将数据传递给从另一个无状态小部件调用的无状态小部件?
【发布时间】:2021-12-15 18:50:49
【问题描述】:

我正在尝试将一些颜色信息从一个小部件传递到另一个小部件,但我无法在目标小部件中获取该颜色。我想构建一个包含一些 UI 代码的类,然后在我的主小部件中调用这个类,这样我就不必再次重复代码,但我无法传递数据, 这是我正在处理的代码:我做错了什么?

void main(List<String> args) => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark(),
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Stateless/Clean Code'),
        ),
        body: const StatelessOne(),
      ),
    );
  }
}

class StatelessOne extends StatelessWidget {
  const StatelessOne({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          const Text(
              'Widget color can be customised without \nhaving to retype the entire code'),
          StatelessTwo(key: key, param: Colors.green),
          StatelessTwo(key: key, param: Colors.pink),
          StatelessTwo(key: key, param: Colors.blue),
          StatelessTwo(key: key, param: Colors.orange),
        ],
      ),
    );
  }
}

class StatelessTwo extends StatelessWidget {
  StatelessTwo({Key? key, @required param}) : super(key: key);

  final Map param = {};

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 120,
      width: 250,
      color: param['color'],
      child: Center(child: Text('Your Repetitive Widget $key')),
    );
  }
}

【问题讨论】:

    标签: flutter parameter-passing statelesswidget


    【解决方案1】:

    简单的方法是

    
    class StatelessTwo extends StatelessWidget {
      final Color color;
    
      const StatelessTwo({
        Key? key,
        required this.color,
      }) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Container(
          height: 120,
          width: 250,
          color: color,
          child: Center(
              child: Text('Your Repetitive Widget ${key ?? "no key found"}')),
        );
      }
    }
     -----
      color: color, //use
    
    

    通过颜色 StatelessTwo(key: key, color: Colors.green), 并且您似乎传递了相同的 key,避免 Ui 逻辑没有必要。很可能,你不需要传递key,如果你仍然想传递使用UinqueKey()

    【讨论】:

    • All final variables must be initialized, but 'color' isn't. Try adding an initializer for the field,给出此错误,如果我添加默认颜色,它将显示在所有 Ui 渲染中,而不是我传递的颜色
    • 并在颜色前加上this 表示:The parameter 'color' can't have a value of 'null' because of its type, but the implicit default value is 'null'. Try adding either an explicit non-'null' default value or the 'required' modifier
    • 一个错字错误@required,检查更新答案。
    • 好的,完美,它正在工作.. 谢谢Key? key 是一个可选参数,所以它应该是构造函数中最后一个预期的参数。
    猜你喜欢
    • 2021-08-29
    • 2021-08-17
    • 1970-01-01
    • 2021-01-22
    • 2020-10-19
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多