【问题标题】:How to run widget over the container screen in flutter如何在颤动中在容器屏幕上运行小部件
【发布时间】:2021-10-22 21:34:14
【问题描述】:

我想在屏幕上显示一些小部件 onTap() 事件,如下图image

这是我的代码 在此构建方法是返回Container()。 Container 有一个名为SingleChildScrollView 的孩子,并且它还有一些孩子。

所以我不想在创建新小部件时更改所有子级。

简单地说,在运行的屏幕上看到小部件onTap() 而不会打扰另一个小部件。

class _SettingScreenState extends State<SettingScreen> {

  List<Widget> _iconList=[];
  List<Widget> _titleList=[];
  List<Widget> _settingLIst=[];


  @override
  void initState() {
    super.initState();
    for(int i=0;i<5;i++){
      _iconList.add(_addInIcon(i));
    }
    for(int i=0;i<5;i++){
      _titleList.add(_addInTitle(i));
    }
    for(int i=0;i<24;i++){
      _settingLIst.add(_addInSetting(i));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
          color: Colors.white
      ),
      child: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            _titleList[0],
            _addInSetting(0),
            _addInSetting(1),
            _titleList[1],
            _settingLIst[2],
            _settingLIst[3],
            _titleList[2],
            _settingLIst[4],
            _settingLIst[5],
            _settingLIst[6],
            _settingLIst[7],
            _settingLIst[8],
            _settingLIst[9],
            GestureDetector(
            onTap: (){
              //open widget over the this screen
            },
            child: Button(
              image: _coverImage(),
              width: double.infinity,
              height: 50,
              alignment: Alignment.center,
              fit: BoxFit.fitHeight,
            ),
          ),
          ],
        ),
      ),
    );
  }
}

Ans,在创建新的小部件时是否需要 setState() 来覆盖之前的 Container() 小部件? 因为我们不能改变以前的Container()

在Android-java开发中,我做了什么

onClick()
    {
        ImageView imageView = new ImageView(this);
        Glide.with(getApplicationContext()).load(position).into(imageView);

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( Math.round((float) 35 * density),  Math.round((float) 35 * density));
        imageView.setX(reactionButton.getX());
        imageView.setY(reactionButton.getY());
        imageView.setLayoutParams(layoutParams);
        messageRelativeLayout.addView(imageView);
        imageView.bringToFront();
        animateReaction(imageView);
    }

这意味着每次新的 ImageView 都会添加到树上,无论之前的 ImageView 是否出现。 简单的单击按钮并创建新的 ImageView 并显示在屏幕前面。

这个功能我想在flutter中应用

【问题讨论】:

  • 是固定位置可见还是随滚动移动?
  • 是的,它将在固定位置,请不要建议 alertDialog,创建的小部件将是一个动画小部件
  • 搜索小部件堆栈
  • @M.Nasri 经过几个小时的搜索,比我来这里问的是,我找不到像这样的正确模型

标签: flutter flutter-layout


【解决方案1】:

OverlayWidget 之子



class OverlayChild extends StatefulWidget {
  final Function clearCallBack;
  final double maxWidth;
  final double maxHeight;
  final int itemIndex;

  const OverlayChild({
    Key? key,
    required this.clearCallBack,
    required this.maxWidth,
    required this.maxHeight,
    required this.itemIndex,
  }) : super(key: key);

  @override
  _OverlayChildState createState() => _OverlayChildState();
}

class _OverlayChildState extends State<OverlayChild> {
  late Timer timer;
  final Random random = Random();

  @override
  void initState() {
    super.initState();
    print("int  ${widget.itemIndex}");
    timer = Timer.periodic(Duration(seconds: 5), (timer) {
      setState(() {
        widget.clearCallBack(widget.itemIndex);
        timer.cancel();
        print("after delay");
      });
    });
  }

  @override
  void dispose() {
    timer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
      top: random.nextDouble() * widget.maxHeight,
      left: random.nextDouble() * widget.maxWidth,
      child: Container(
        height: 20,
        width: 20,
        padding: EdgeInsets.all(3),
        decoration: BoxDecoration(
          shape: BoxShape.circle,
          color:
              widget.itemIndex.isEven ? Colors.deepPurple : Colors.cyanAccent,
        ),
        child: Center(child: Text("${widget.itemIndex}")),
      ),
    );
  }
}

主小部件


class HomeOverLay extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<HomeOverLay> {
  List<OverlayChild> overlayItems = [];

  int itemId = 0;
  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) => Scaffold(
        floatingActionButton: FloatingActionButton(
          onPressed: () async {
            setState(() {
              overlayItems.add(
                OverlayChild(
                  key: UniqueKey(),
                  clearCallBack: (id) {
                    setState(() {
                      overlayItems
                          .removeWhere((element) => element.itemIndex == id);
                    });
                  },
                  itemIndex: itemId,
                  //same as container height
                  maxHeight: constraints.maxHeight * .1,
                  maxWidth: constraints.maxWidth,
                ),
              );

              itemId++;
            });
          },
        ),
        body: Stack(
          children: [
            Align(
              alignment: Alignment.topCenter, // you may want some changes here
              child: SizedBox(
                height: constraints.maxHeight,
                width: constraints.maxWidth,
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      ...List.generate(
                        22,
                        (index) => Container(
                          height: 100,
                          width: double.infinity,
                          color: index.isEven
                              ? Colors.deepPurple
                              : Colors.orangeAccent,
                        ),
                      )
                    ],
                  ),
                ),
              ),
            ),
            Positioned(
              top: constraints.maxHeight * .2,
              left: 0,
              right: 0,
              child: Container(
                width: overlayItems.length > 0 ? constraints.maxWidth : 0,
                height:
                    overlayItems.length > 0 ? constraints.maxHeight * .1 : 0,
                color: Colors.pinkAccent,
                child: Stack(
                  children: [
                    ...overlayItems.toList(),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

【讨论】:

  • 在您声明的这些代码中。 show 和 !show.means 小部件只会创建一次,但在我的情况下,新小部件每次都会创建 onTap();
  • 您不想控制可见性吗?然后你可以删除这个布尔值。还是您正在寻找的其他东西?
  • 我更新的答案是否满足您关于叠加的条件?
  • 你可以看到这张图片google.com/…,在这张GIF中所有的图片都是由onTap()创建的小部件,背景屏幕是Container()
猜你喜欢
  • 2021-11-26
  • 1970-01-01
  • 2019-05-07
  • 2021-03-01
  • 2020-10-29
  • 2019-12-29
  • 2020-10-26
  • 1970-01-01
相关资源
最近更新 更多