我想要的是:

enter image description here

我的小工具:

class Buttons extends StatelessWidget {
      const Buttons({
      Key key,
      @required this.selectBloc,
      @required this.selectStatus,
    }) : super(key: key);

    final SelectBloc selectBloc;
    final int selectStatus;

    @override
    Widget build(BuildContext context) {
      return BlocBuilder<SelectBloc, SelectState>(
          bloc: selectBloc,
          builder: (context, state) {
            return OutlinedButton(
              onPressed: () {
            },
            child: Text( dynamic, style: TextStyle(color: Colors.grey[500])),
            style: ButtonStyle(
              side: MaterialStateProperty.all(BorderSide(
                  color: state.statusSelected == selectStatus
                      ? Colors.blue
                      : Colors.grey)),
            ),
          );
        });
  }
}

我用它:

    Buttons(
      selectBloc: selectBloc,
      selectStatus: 0,
    ),
    Buttons(
      selectBloc: selectBloc,
      selectStatus: 1,
    ),

推荐答案

您应该在Buttons中有另一个字段final Function() onTap. 然后在定义按钮的地方实现它.

我的意思是这样的东西:

class Buttons extends StatelessWidget {
      const Buttons({
      Key key,
      @required this.selectBloc,
      @required this.isSelecteed,
      @required this.onTap,
    }) : super(key: key);

    final SelectBloc selectBloc;
    final bool isSelecteed;
    final Function() onTap;

    @override
    Widget build(BuildContext context) {
      return BlocBuilder<SelectBloc, SelectState>(
          bloc: selectBloc,
          builder: (context, state) {
            return OutlinedButton(
              onPressed: onTap,
            child: Text( dynamic, style: TextStyle(color: Colors.grey[500])),
            style: ButtonStyle(
              side: MaterialStateProperty.all(BorderSide(
                  color: isSelecteed
                      ? Colors.blue
                      : Colors.grey)),
            ),
          );
        });
  }
}

然后:

Buttons(
      selectBloc: selectBloc,
      isSelecteed: 1 == selectedIndex,
      onTap:(){
         selectedIndex = 1;
         setState((){});
      }
    ),

我希望您明白这一点:)

Flutter相关问答推荐

在Flutter中创建具有固定行的压缩水平列表

使用ThemeProvider不在亮模式和暗模式之间切换

将目标平台设置为Flutter 构建

来自FutureProvider的数据只打印两个不同的变量,它们对一个实例只有一次相同的值,为什么?

Flutter AppBar Animation反向调试

为什么Flutter 翼扩展卡有上下边框?

就像Flutter 中的头文件一样?

如何在抖动中预加载一条路由,或者即使在屏幕外也保持加载小工具?

来自FirebaseAuth的错误DisplayName和邮箱在Flutter应用程序中给出错误

为什么Spotify Auth API返回400,而";code_verier不正确?

使用现有Map方法对子类克隆方法的逻辑进行Dart标准化

如何在Flutter中的textField中在可编辑文本和前缀图标之间添加空格?

Flutter 判断 Uint8List 是否有效 Image

使用 Riverpod 和 copyWith 方法更新数据类中列表中变量的值

如何从底部导航栏打开 bottomModal 表?

alert 对话框内的 ListView 显示空的透明窗口

访问 AsyncSnapshot 中的嵌套属性

Flutter:如何判断嵌套列表是否包含值

Flutter 在气泡聊天消息上对齐时间戳文本

如何使用 Navigator 2.0 导航到任何页面?