how to disable tooltip dynamcically in flutter?

爷,独闯天下 提交于 2021-01-29 05:30:45

问题


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();
    }
  }
}

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.


回答1:


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();
    }
  }
}


来源:https://stackoverflow.com/questions/62699702/how-to-disable-tooltip-dynamcically-in-flutter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!