如何动态地禁用工具提示? [英] how to disable tooltip dynamcically in flutter?

查看:56
本文介绍了如何动态地禁用工具提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以静态禁用工具提示.但是我想在我单击flatbutton时动态禁用工具提示.但是无法动态禁用,我不知道这样做.这是我的代码:

I can disable the tooltip statically. But I want to disable tooltip dynamically when i click flatbutton.But Couldnt disable dynamically and i have no idea to do that. This is my code:

import 'package:flutter/material.dart';
void main(){
  runApp(MaterialApp(home: HelloWorld(),debugShowCheckedModeBanner: false,));
}
class HelloWorld extends StatefulWidget {
  @override
  _HelloWorldState createState() => _HelloWorldState();
}

class _HelloWorldState extends State<HelloWorld> {

  bool check = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
          child: Center(
            child: Column(children: <Widget>[
              TopToolbar(),
              FlatButton(
                child: Text("Disable Tooltip"),
                onPressed: () {
                  setState(() {
                    TopToolbar toolbar = new TopToolbar();
                    toolbar.showTooltip = false;
                  });
                },
              ),
            ]),
          ),
        ));
  }
}

class TopToolbar extends StatefulWidget {

  bool showTooltip;
  final Color backgroundColor;
  final double height;
  bool isVisible;
  TopToolbar({
    this.height = 55,
    this.isVisible = true,
    this.backgroundColor = const Color(0xFFEEEEEE),
    Key key,this.showTooltip=true,
  }) : super(key: key);
  @override
  _TopToolbarState createState() => _TopToolbarState();
}

class _TopToolbarState extends State<TopToolbar> {
  @override
  Widget build(BuildContext context) {
    if (widget.isVisible) {
      return Container(
        foregroundDecoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              color: Colors.grey,
            ),
          ),
        ),
        margin: EdgeInsets.only(bottom: 1),
        color: widget.backgroundColor,
        height: widget.height,
        child: Stack(
          children: <Widget>[
            Positioned(
              top: 7,
              right: 60,
              height: 40,
              width: 40,
              child: RawMaterialButton(
                elevation: 0.0,
                fillColor: widget.backgroundColor,
                splashColor: Colors.grey[300],
                child: IconButton(
                  icon: Icon(
                    Icons.bookmark,
                    color: Colors.grey[500],
                    size: 25,
                  ),
                  onPressed: (){},
                  tooltip: widget.showTooltip ? "Bookmark" : null,
                ),
                onPressed: (){},
              ),
            ),
          ],
        ),
      );
    } else {
      return Container();
    }
  }
}

如果我静态给出错误的话.它工作正常.例如:如果添加类似TopToolbar的子项(showTooltip:false),则可以正常工作,但是,如果我在Flatbutton onPressed方法中给出toolbar.showTooltip = false,则无法正常工作.我想动态地散布它.请帮助我做到这一点.

If I give statically false. it works fine. For example : If add child like TopToolbar(showTooltip : false),it works fine, But If i give toolbar.showTooltip = false in Flatbutton onPressed method,it doesnt work. I want to disble it in dynamically. please help me to do that.

推荐答案

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: HelloWorld(),
    debugShowCheckedModeBanner: false,
  ));
}

class HelloWorld extends StatefulWidget {
  @override
  _HelloWorldState createState() => _HelloWorldState();
}

class _HelloWorldState extends State<HelloWorld> {
  bool check = false;
  bool showTooltip = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
      child: Center(
        child: Column(children: <Widget>[
          TopToolbar(showTooltip: showTooltip),
          FlatButton(
            child: Text("Disable Tooltip"),
            onPressed: () {
              setState(() {
                showTooltip = false;
              });
            },
          ),
        ]),
      ),
    ));
  }
}

class TopToolbar extends StatefulWidget {
  final bool showTooltip;
  final Color backgroundColor;
  final double height;
  final bool isVisible;
  TopToolbar({
    this.height = 55,
    this.isVisible = true,
    this.backgroundColor = const Color(0xFFEEEEEE),
    Key key,
    this.showTooltip = true,
  }) : super(key: key);
  @override
  _TopToolbarState createState() => _TopToolbarState();
}

class _TopToolbarState extends State<TopToolbar> {
  @override
  Widget build(BuildContext context) {
    if (widget.isVisible) {
      return Container(
        foregroundDecoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              color: Colors.grey,
            ),
          ),
        ),
        margin: EdgeInsets.only(bottom: 1),
        color: widget.backgroundColor,
        height: widget.height,
        child: Stack(
          children: <Widget>[
            Positioned(
              top: 7,
              right: 60,
              height: 40,
              width: 40,
              child: RawMaterialButton(
                elevation: 0.0,
                fillColor: widget.backgroundColor,
                splashColor: Colors.grey[300],
                child: IconButton(
                  icon: Icon(
                    Icons.bookmark,
                    color: Colors.grey[500],
                    size: 25,
                  ),
                  onPressed: () {},
                  tooltip: widget.showTooltip ? 'Bookmark' : null,
                ),
                onPressed: () {},
              ),
            ),
          ],
        ),
      );
    } else {
      return Container();
    }
  }
}

这篇关于如何动态地禁用工具提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆