Flutter 颤振:使用小部件中另一个文件的函数

Flutter 颤振:使用小部件中另一个文件的函数,flutter,Flutter,我使用的是颤振,我对这个框架完全是个新手。我正在尝试使用可见性小部件显示和隐藏行 当我在有状态或无状态小部件中声明变量bool _isVisible=false时,我试图使用这个小部件,但是当我想通过小部件从另一个dart文件显示和隐藏行()时,我遇到了一个问题。我不知道怎么做。为了解决这个问题,我尝试创建一个dart文件,用于创建函数和变量bool\u isVisible=false;因此,所有小部件都可以访问,但我无法在小部件中使用bool变量和此dart文件的函数 在这张图片中:绿色圆圈中

我使用的是颤振,我对这个框架完全是个新手。我正在尝试使用可见性小部件显示和隐藏

当我在有状态或无状态小部件中声明变量bool _isVisible=false时,我试图使用这个小部件,但是当我想通过小部件从另一个dart文件显示和隐藏行()时,我遇到了一个问题。我不知道怎么做。为了解决这个问题,我尝试创建一个dart文件,用于创建函数和变量bool\u isVisible=false;因此,所有小部件都可以访问,但我无法在小部件中使用bool变量和此dart文件的函数

在这张图片中:绿色圆圈中的ontap我希望所有行()的粉红色以及分级和按钮都被隐藏

附言:对不起我的英语

当用户点击CardAnimeMovie小部件时,这是一个带有评级部分的小部件(AnimeMovieDialog),我想隐藏(或显示)该部分




    
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Tutorial by Woolha.com',
      home: AnimeMovieDialog(),
      debugShowCheckedModeBanner: false,
    );
  }
}





class AnimeMovieDialog extends StatefulWidget {
  @override
  _AnimeMovieDialogState createState() => _AnimeMovieDialogState();
}

class _AnimeMovieDialogState extends State<AnimeMovieDialog> {
  double _rating = 1;
  double count = 1;
  bool _isVisible = true;
  void _change() {
    setState(() {
      _isVisible = !_isVisible;

      print("tap succes");
    });
  }

  void _close() {
    setState(() {
      _isVisible = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;
    return SafeArea(
      child: Visibility(
        visible: _isVisible,
        child: Stack(children: [
          Container(
            width: width,
            height: height,
            decoration: BoxDecoration(color: Color(0xffffff).withOpacity(0.6)),
          ),
          Center(
            child: SingleChildScrollView(
              child: Container(
                  width: width / 1.05,
                  height: height / 1.2,
                  decoration: BoxDecoration(color: Color(0xff212529)),
                  child: Column(
                    children: [
                      Row(
                        children: [
                          Container(
                            width: width / 1.05,
                            height: 60,
                            decoration: BoxDecoration(color: Color(0xff212529)),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Padding(
                                  padding:
                                      const EdgeInsets.fromLTRB(4.0, 5, 35, 0),
                                  child: GestureDetector(
                                    onTap: () {
                                      _close();
                                    },
                                    child: Container(
                                        width: 65,
                                        height: 45,
                                        color: Colors.blue,
                                        child: Icon(Icons.clear,
                                            color: Colors.white)),
                                  ),
                                ),
                                Container(
                                  width: width / 2,
                                  height: height / 2,
                                  child: TextField(
                                      cursorColor: Colors.orange,
                                      style: TextStyle(
                                        color: Colors.white,
                                      ),
                                      maxLines: 1,
                                      decoration: InputDecoration(
                                        enabledBorder: UnderlineInputBorder(
                                            borderSide: BorderSide(
                                                color: Colors.white)),
                                        focusedBorder: UnderlineInputBorder(
                                            borderSide: BorderSide(
                                                color: Colors.orange)),
                                        border: UnderlineInputBorder(
                                            borderSide: BorderSide(
                                                color: Colors.white)),
                                        hintText: 'Rechercher',
                                        hintStyle:
                                            TextStyle(color: Colors.white),
                                      )),
                                ),
                                Padding(
                                  padding:
                                      const EdgeInsets.fromLTRB(15, 0, 8, 0),
                                  child: Container(
                                      child: Icon(Icons.search,
                                          color: Colors.white)),
                                )
                              ],
                            ),
                          )
                        ],
                      ),
                      Row(
                        children: [
                          Container(
                            width: width / 1.05,
                            height: height / 1.5,
                            child: ListView(
                              children: [
                                CardAnimeMovie(),
                                CardAnimeMovie(),
                                CardAnimeMovie(),
                                CardAnimeMovie(),
                                CardAnimeMovie(),
                                CardAnimeMovie()
                              ],
                            ),
                          )
                        ],
                      ),
                      Row(children: [
                        Column(
                          children: [
                            Container(
                                width: width / 2,
                                height: height / 11.25,
                                color: null,
                                child: Row(
                                  children: [
                                    Padding(
                                      padding: const EdgeInsets.fromLTRB(
                                          25, 0, 0, 0),
                                      child: GFRating(
                                        onChanged: (value) {
                                          setState(() {
                                            _rating = value;
                                          });
                                        },
                                        value: _rating,
                                        size: 25,
                                        color: Colors.orange,
                                        borderColor: Colors.orange,
                                      ),
                                    )
                                  ],
                                )),
                          ],
                        ),
                        Padding(
                          padding: const EdgeInsets.fromLTRB(35, 0, 8, 0),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Container(
                                width: width / 3,
                                height: height / 18,
                                decoration: BoxDecoration(
                                    color: Colors.orange,
                                    borderRadius: BorderRadius.only(
                                        topLeft: Radius.circular(25.0),
                                        topRight: Radius.circular(25.0),
                                        bottomLeft: Radius.circular(25.0),
                                        bottomRight: Radius.circular(25.0))),
                                child: TextButton(
                                  style: TextButton.styleFrom(
                                    primary: Colors.blue,
                                    onSurface: Colors.red,
                                  ),
                                  onPressed: null,
                                  child: Text('Noter',
                                      style: TextStyle(
                                          color: Colors.white,
                                          fontFamily: 'DBIcons',
                                          fontSize: 17)),
                                ),
                              ),
                            ],
                          ),
                        )
                      ])
                    ],
                  )),
            ),
          ),
        ]),
      ),
    );
  }
}

void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“Woolha.com的颤振教程”,
主页:AnimeMovieDialog(),
debugShowCheckedModeBanner:false,
);
}
}
类AnimeMovieDialog扩展StatefulWidget{
@凌驾
_AnimeMovieDialogState createState()=>\u AnimeMovieDialogState();
}
类_AnimeMovieDialogState扩展状态{
双重_评级=1;
重复计数=1;
bool _isVisible=true;
void_change(){
设置状态(){
_isVisible=!\u isVisible;
打印(“点击成功”);
});
}
void_close(){
设置状态(){
_isVisible=false;
});
}
@凌驾
小部件构建(构建上下文){
double width=MediaQuery.of(context).size.width;
double height=MediaQuery.of(context).size.height;
返回安全区(
孩子:可见度(
可见:_是可见的,
子:堆栈(子:[
容器(
宽度:宽度,
高度:高度,,
装饰:框装饰(颜色:颜色(0xffffff)。不透明度(0.6)),
),
居中(
子:SingleChildScrollView(
子:容器(
宽度:宽度/1.05,
高度:高度/1.2,
装饰:盒子装饰(颜色:颜色(0xff212529)),
子:列(
儿童:[
划船(
儿童:[
容器(
宽度:宽度/1.05,
身高:60,
装饰:盒子装饰(颜色:颜色(0xff212529)),
孩子:排(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
填充物(
衬垫:
LTRB的常数边集(4.0,5,35,0),
儿童:手势检测器(
onTap:(){
_close();
},
子:容器(
宽度:65,
身高:45,
颜色:颜色,蓝色,
子:图标(Icons.clear,
颜色:颜色。白色),
),
),
容器(
宽度:宽度/2,
高度:高度/2,
孩子:TextField(
cursorColor:Colors.orange,
样式:TextStyle(
颜色:颜色,白色,
),
maxLines:1,
装饰:输入装饰(
enabledBorder:UnderlineInputBorder(
边界边(
颜色:颜色。白色),
FocusedOrder:下划线输入边框(
边界边(
颜色:颜色。橙色),
边框:下划线输入边框(
边界边(
颜色:颜色。白色),
hintText:“Rechercher”,
辛茨风格:
TextStyle(颜色:Colors.white),
)),
),
填充物(
衬垫:
LTRB(15,0,8,0)的常数边集,
子:容器(
子:图标(Icons.search,
颜色:颜色。白色),
)
],
),
)
],
),
划船(
儿童:[
容器(
宽度:宽度/1.05,
高度:高度/1.5,
子:ListView(
儿童:[
CardAnimeMovie(),
class CardAnimeMovie extends StatefulWidget {
  @override
  _CardAnimeMovieState createState() => _CardAnimeMovieState();
}

class _CardAnimeMovieState extends State<CardAnimeMovie> {
  // bool _visible = false;
  // void ratechange() {
  //   setState(() {
  //     _visible = !_visible;
  //   });
  // }

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;
    return Padding(
      padding: const EdgeInsets.all(2.0),
      child: GestureDetector(
        onTap: () {
          ratechange();
        },
        child: Container(
            width: width / 5,
            height: height / 3.2,
            decoration: BoxDecoration(color: Color(0xff272824)),
            child: Row(
              children: [
                Column(children: [
                  Container(
                    width: width / 2.1,
                    height: height / 3.2,
                    child: Image.network(
                      'https://image.noelshack.com/fichiers/2014/31/1406628082-btlbdfqiyaasamj.jpg',
                      fit: BoxFit.cover,
                    ),
                  )
                ]),
                Column(children: [
                  Container(
                    width: width / 2.15,
                    height: height / 3.2,
                    decoration: BoxDecoration(
                      border: Border(
                        bottom: BorderSide(color: Colors.white, width: 0.5),
                      ),
                    ),
                    child: Padding(
                      padding: const EdgeInsets.fromLTRB(15, 5, 0, 0),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              Text(
                                'Titre : oeuvre',
                                style: TextStyle(
                                  color: Colors.white,
                                  fontFamily: 'DBIcons',
                                  fontSize: 18,
                                ),
                              )
                            ],
                          ),
                          SizedBox(
                            height: 0.5,
                          ),
                          Row(
                            children: [
                              Text(
                                'Auteur : XXXX',
                                style: TextStyle(
                                  color: Colors.white,
                                  fontFamily: 'DBIcons',
                                  fontSize: 18,
                                ),
                              )
                            ],
                          ),
                          SizedBox(height: 7),
                          Row(
                            children: [
                              Container(
                                width: width / 2.5,
                                height: height / 5,
                                child: Text(
                                  "Résume : Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries,",
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontFamily: 'DBIcons',
                                    fontSize: 18,
                                  ),
                                ),
                              )
                            ],
                          )
                        ],
                      ),
                    ),
                  )
                ])
              ],
            )),
      ),
    );
  }
}

class AnotherWidget extends StatelessWidget{
    final Function myFunction;
    AnotherWidget ({this.myFunction});
    
    @override
    Widget build(BuildContext context) {
        return FlatButton(onPressed: myFunction,....);
}