dart 如何在有状态小部件之外使用setState()

bmp9r5qi  于 2023-02-27  发布在  其他
关注(0)|答案(1)|浏览(126)

我正在为报表底部创建此模式。我想在悬停时更改背景色,但它需要setstate才能做到这一点,没有setState我怎么能做到呢?我问chatGpt,它给了我此代码。但它仍然给出错误。

这是我的代码:

import 'package:flutter/material.dart';
import 'package:pre/utility/static.data.dart';
import 'package:pre/utility/theme.dart';
import 'package:pre/widgets/custom_button.widget.dart';
import 'package:pre/widgets/social_widget/report-message.widget.dart';

bool isHover = true;
messageButtomSheet(BuildContext context, String text) {
  showModalBottomSheet(
    backgroundColor: Colors.transparent,
    context: context,
    builder: (builder) {
      return Container(
        height: ScreenUnit.heigh(context) * 8,
        decoration: const BoxDecoration(
          color: white,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(10.0),
            topRight: Radius.circular(10.0),
          ),
        ),
        child: Padding(
            padding: const EdgeInsets.only(
              top: 30,
              left: 20,
              right: 20,
              bottom: 30,
            ),
            child: Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    CustomIconButton(
                        iconButton: Icons.close,
                        iconsize: 20,
                        color: secondarytextColor),
                    const Text(''),
                    const Text('')
                  ],
                ),
                const Padding(
                  padding: const EdgeInsets.only(top: 4.0, bottom: 15),
                  child: Text(
                    '',
                    style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
                  ),
                ),
                Expanded(
                  flex: 2,
                  child: ListView.builder(
                    itemCount: 4,
                    itemBuilder: (context, index) {
                      return MouseRegion(
                        onHover: (event) {
                          setState(() {
                            isHover = true;
                          });
                        },
                        onExit: (event) {
                          setState(() {
                            isHover = false;
                          });
                        },
                        child: customButtom(
                            title:' report',
                            backgroundColor: isHover ? grey : white,
                            fontColor: black,
                            borderColor: black.withOpacity(0.5),
                            height: ScreenUnit.heigh(context) * 0.067,
                            margin: const EdgeInsets.only(bottom: 8),
                            onPress: () {}),
                      );
                    },
                  ),
                ),
                customButtom(
                    title: 'تۆمارکردن',
                    width: double.infinity,
                    height: ScreenUnit.heigh(context) * 0.067,
                    margin: const EdgeInsets.only(top: 20),
                    onPress: () {
                      ReportMessageButtomSheet(context, 'text');
                    })
              ],
            )),
      );
    },
  );
}

尝试给予悬停影响到这个按钮。期待与工作,把它放在有状态的小部件。

goucqfw6

goucqfw61#

可以使用StatefulBuilder更新对话框ui。

showModalBottomSheet(
    backgroundColor: Colors.transparent,
    context: context,
    builder: (builder) {
      return StatefulBuilder(
        builder: (context, setState) =>  
            Container(

相关问题