簡體   English   中英

Flutter:使用小部件中另一個文件的 function

[英]Flutter : Use function from another file in widget

我正在使用 flutter,我完全是這個框架的新手。 我正在嘗試使用可見性小部件來顯示和隱藏Row

當我在有狀態或無狀態小部件中聲明變量 bool _isVisible = false 時,我正在嘗試使用此小部件,但是當我想通過另一個 dart 文件中的小部件顯示和隱藏 row() 時出現問題。 我不知道該怎么做。 為了解決這個問題,我正在嘗試創建一個 dart 文件來創建一個 function 和變量 bool _isVisible = false; 以便所有小部件都可以訪問,但我無法在我的小部件中使用此 dart 文件的 bool 變量和 function。

在這張圖片中:綠色圓圈中的 ontap 我希望所有帶有評級的行()都為粉紅色,並且按鈕被隱藏。

我的應用程序中的問題截圖

PS:對不起我的英語

這是當用戶點擊 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)),
                                ),
                              ),
                            ],
                          ),
                        )
                      ])
                    ],
                  )),
            ),
          ),
        ]),
      ),
    );
  }
}

這是電影簡歷(圖中綠色)

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,
                                  ),
                                ),
                              )
                            ],
                          )
                        ],
                      ),
                    ),
                  )
                ])
              ],
            )),
      ),
    );
  }
}

您可以將 function 提供給另一個 dart 文件,然后您可以在另一個 ZBB141276780EA20FAE 文件中調用 function77678960FAE7539。 例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM