切换到其他屏幕时黑屏抖动

时间:2020-10-02 17:32:58

标签: flutter dart

我在Flutter中制作了第二个屏幕。我有main.dart,January.dart和YearScreen.dart。 我想从一月屏幕切换到年屏幕。

但是当我想切换到YearScreen屏幕时,显示会变成黑色。

一个小时前,它使用了相同的Code。 有任何人的经验。

main.dart


import 'package:flutter/material.dart';
import 'package:flutter_calendar/January.dart';

void main() {
  runApp(Calendar());
}

class Calendar extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(debugShowCheckedModeBanner: false,
        home: JanuaryScreen());
  }
}

January.dart


import 'package:flutter/material.dart';
import 'package:flutter_calendar/YearScreen.dart';

class JanuaryScreen extends StatefulWidget {
  @override
  _JanuaryScreenState createState() => _JanuaryScreenState();
}
class _JanuaryScreenState extends State<JanuaryScreen> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Scaffold(
          floatingActionButton: Stack(children: [
            Positioned(
                top: 300,
                left: 285,
                child: SizedBox(
                    height: 60,
                    width: 101,
                    child: FloatingActionButton.extended(
                      onPressed: () {
                        Navigator.push(context,
                            MaterialPageRoute(builder: (context) {
                              return YearScreen();
                            }));
                      },
                      elevation: 0,
                      isExtended: true,
                      backgroundColor: Colors.black,
                      splashColor: Colors.blueGrey,
                      highlightElevation: 0,
                      label: Text("J"),
                    ))),
        Positioned(
        top: 600,
            right: 285,
            child: SizedBox(
                height: 60,
                width: 101,
                child: FloatingActionButton.extended(
                  onPressed: () {
                    Navigator.push(context,
                        MaterialPageRoute(builder: (context) {
                          return YearScreen();
                        }));
                  },
                  elevation: 0,
                  isExtended: true,
                  backgroundColor: Colors.black,
                  splashColor: Colors.blueGrey,
                  highlightElevation: 0,
                  label: Text("2020"),
                )))
          ]),
          backgroundColor: Colors.blueGrey,
          appBar: PreferredSize(
            preferredSize: Size.fromHeight(65.0),
            child: AppBar(
              backgroundColor: Colors.blueGrey,
              elevation: 0.0,
              title: Transform(
                transform: Matrix4.translationValues(-85.0, 0.0, 0.0),
                child: Text(
                  "January",
                  style: TextStyle(fontSize: 50),
                ),
              ),
            ),),
            body: ListView(
              children: [
                RaisedButton(
                  child: Text(
                    "1",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("j");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "2",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("2021");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "3",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("j");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "4",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("2021");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "5",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("j");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "6",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("2021");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "7",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("j");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "8",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("2021");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "9",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("j");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "10",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("2021");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "11",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("j");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "12",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("2021");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "13",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("j");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "14",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("2021");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "15",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("j");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "16",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("2021");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "17",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("j");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "18",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("2021");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "19",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("j");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "20",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("2021");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "21",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("j");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "22",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("2021");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "23",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("j");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "24",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("2021");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "25",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("j");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "26",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("2021");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "27",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("j");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "28",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("2021");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "29",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("j");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "30",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("2021");
                  },
                ),
                RaisedButton(
                  child: Text(
                    "31",
                    style: TextStyle(fontSize: 130),
                  ),
                  color: Colors.blueGrey,
                  splashColor: Colors.transparent,
                  elevation: 0,
                  onPressed: () {
                    print("j");
                  },
                ),
              ],
              itemExtent: 211.89,
            ),
          ),
        );
  }
}

YearScreen.dart


import 'package:flutter/material.dart';
import 'package:flutter_calendar/January.dart';






class YearScreen extends StatefulWidget {
  @override
  _YearScreenState createState() => _YearScreenState();
}










class _YearScreenState extends State<YearScreen> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        backgroundColor: Colors.blueGrey,
        body: ListView(
            children: [
              RaisedButton(
                child: Text(
                  "2020",
                  style: TextStyle(fontSize: 130),
                ),
                color: Colors.blueGrey,
                elevation: 0,
                splashColor: Colors.transparent,
                onPressed: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) {
                        return JanuaryScreen();
                      }));
                },
              ),
              RaisedButton(
                child: Text(
                  "2021",
                  style: TextStyle(fontSize: 130),
                ),
                color: Colors.blueGrey,
                elevation: 0,
                splashColor: Colors.transparent,
                onPressed: () {
                  print("2021");
                },
              ),
              RaisedButton(
                child: Text(
                  "2022",
                  style: TextStyle(fontSize: 130),
                ),
                color: Colors.blueGrey,
                elevation: 0,
                splashColor: Colors.transparent,
                onPressed: () {
                  print("2022");
                },
              ),
              RaisedButton(
                child: Text(
                  "2023",
                  style: TextStyle(fontSize: 130),
                ),
                color: Colors.blueGrey,
                elevation: 0,
                splashColor: Colors.transparent,
                onPressed: () {
                  print("2023");
                },
              ),
              RaisedButton(
                child: Text(
                  "2024",
                  style: TextStyle(fontSize: 130),
                ),
                color: Colors.blueGrey,
                elevation: 0,
                splashColor: Colors.transparent,
                onPressed: () {
                  print("2024");
                },
              ),
              RaisedButton(
                child: Text(
                  "2025",
                  style: TextStyle(fontSize: 130),
                ),
                color: Colors.blueGrey,
                elevation: 0,
                splashColor: Colors.transparent,
                onPressed: () {
                  print("2025");
                },
              ),
              RaisedButton(
                child: Text(
                  "2026",
                  style: TextStyle(fontSize: 130),
                ),
                onPressed: () {
                  print("2026");
                },
                color: Colors.blueGrey,
                elevation: 0,
                splashColor: Colors.transparent,
              ),
              RaisedButton(
                child: Text(
                  "2027",
                  style: TextStyle(fontSize: 130),
                ),
                onPressed: () {
                  print("2027");
                },
                color: Colors.blueGrey,
                elevation: 0,
                splashColor: Colors.transparent,
              ),
              RaisedButton(
                child: Text(
                  "2028",
                  style: TextStyle(fontSize: 130),
                ),
                onPressed: () {
                  print("2028");
                },
                color: Colors.blueGrey,
                elevation: 0,
                splashColor: Colors.transparent,
              ),
              RaisedButton(
                child: Text(
                  "2029",
                  style: TextStyle(fontSize: 130),
                ),
                onPressed: () {
                  print("2029");
                },
                color: Colors.blueGrey,
                elevation: 0,
                splashColor: Colors.transparent,
              ),
              RaisedButton(
                child: Text(
                  "2030",
                  style: TextStyle(fontSize: 130),
                ),
                color: Colors.blueGrey,
                elevation: 0,
                splashColor: Colors.transparent,
                onPressed: () {
                  print("2030");
                },
              ),
            ],
            itemExtent: 300,
      ),
    ),);
  }
  }



1 个答案:

答案 0 :(得分:1)

可能是因为您还从JanuaryScreen和YearScreen的生成方法中返回了MaterialApp。您不需要从任何地方返回MaterialApp,只需用MaterialApp包装小部件树的最开始部分,然后从树下的子元素返回诸如Scaffold之类的东西。如果从YearScreen和JanuaryScreen中删除MaterialApp,它应该可以正常工作。