简体   繁体   English

Flutter - 使用 for 循环或列表创建具有可编辑属性的自定义小部件

[英]Flutter- Using for loops or lists to create custom widgets with editable properties

I created a custom class, RectButton, with editable properties of buttonChild, bgColor, and onPress.我创建了一个自定义的 class,RectButton,具有 buttonChild、bgColor 和 onPress 的可编辑属性。 I was hoping to make this widget further dynamic by creating a new widget that can create a row of these RectButtons based on a variable integer (ie 4 buttons on one screen, 3 on another screen, etc) but cant figure out how to continue to have completely editable properties (bgColor isn't depended on index, ie bgColor: Colors.red[100 + 100 * index]) in the new widget.我希望通过创建一个新的小部件来使这个小部件更加动态,该小部件可以基于变量 integer(即一个屏幕上有 4 个按钮,另一个屏幕上有 3 个按钮等)创建一行这些 RectButtons,但无法弄清楚如何继续在新的小部件中具有完全可编辑的属性(bgColor 不依赖于索引,即 bgColor: Colors.red[100 + 100 * index])。

class RectButton extends StatelessWidget {
  RectButton({this.buttonChild, this.bgColor, this.onPress});

  final Widget buttonChild;
  final Color bgColor;
  final Function onPress;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onPress,
      child: Container(
        constraints: BoxConstraints.expand(width: 100, height: 50),
        child: Center(child: buttonChild),
        decoration: BoxDecoration(
            color: bgColor,
            shape: BoxShape.rectangle,
            border: Border.all(width: 1, color: Colors.white)),
        padding: EdgeInsets.fromLTRB(12, 12, 12, 12),
      ),
    );
  }
}

Any thoughts?有什么想法吗? Any help is much appreciated.任何帮助深表感谢。 Ive been googling everything I can find about for loops and lists with no luck.我一直在谷歌搜索我能找到的关于循环和列表的所有东西,但没有成功。 Also any resources are also appreciated-- kinda new to flutter:)也感谢任何资源——flutter 有点新:)

Edit: Updated Code编辑:更新代码

import 'package:flutter/material.dart';
import 'rect_button.dart';

enum Options { option0, option1, option2, option3 }

class Screen1 extends StatefulWidget {
  @override
  _Screen1State createState() => _Screen1State();
}

class _Screen1State extends State<Screen1> {
  List<Widget> makeButtons(int num, List<Widget> children, List<Color> colors,
      List<Function> onPresses) {
    List<Widget> buttons = new List();
    for (int i = 0; i < num; i++) {
      buttons.add(RectButton(children[i], colors[i], onPresses[i]));
    }
    return buttons;
  }

  Options selectedOption;

  @override
  Widget build(BuildContext context) {
    int num = 2;
    List<Widget> children = [
      Text("A"),
      Text("B"),
    ];
    List<Color> colors = [
      selectedOption == Options.option0 ? Colors.red : Colors.green,
      selectedOption == Options.option1 ? Colors.red : Colors.green
    ];
    List<Function> onPresses = [
      () {
        setState(() {
          selectedOption = Options.option0;
        });
      },
      () {
        setState(() {
          selectedOption = Options.option1;
        });
      },
    ];
// 3rd method does nothing
    return Scaffold(
      appBar: AppBar(
        title: Text('title'),
      ),
      body: Row(
        children: makeButtons(3, children, colors, onPresses),
      ),
    );
  }
}

If you can create Lists for each child, color, onPress you want, you can use the code below to loop through and create a list of RectButton :如果您可以为您想要的每个子项、颜色、onPress 创建列表,则可以使用下面的代码循环并创建一个RectButton列表:

List<Widget> makeButtons(int num, List<Widget> children, List<Color> colors, List<Function> onPresses){
  List<Widget> buttons = new List();
  for(int i = 0; i < num; i ++){
    buttons.add(RectButton(buttonChild: children[i], bgColor: colors[i], onPress: onPresses[i]));
  }
  return buttons;
}

You can use it with a Row like:您可以将它与Row一起使用,例如:

Row(
  children: makeButtons(...),
),

You can also modify the makeButtons method to add optional parameters in case you want one color consistently/with a [100+100*i] difference etc.您还可以修改makeButtons方法以添加可选参数,以防您希望一种颜色始终如一/具有 [100+100*i] 差异等。

Edit: Example with build method:编辑:构建方法示例:

Widget build(BuildContext context) {
    int num = 2;
    List<Widget> children = [Text("A"), Text("B"), Text(_counter.toString())];
    List<Color> colors = [Colors.red, Colors.blue, Colors.green];
    List<Function> onPresses = [_incrementCounter, _decrementCounter, (){}];
// 3rd method does nothing
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Row(
      children: makeButtons(3, children, colors, onPresses),
      ),
    );
  }

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

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