在Flutter中完成CircularProgressIndicator后,如何导航到另一个页面?

时间:2020-02-08 07:29:04

标签: flutter flutter-layout flutter-animation flutter-navigation flutter-widget

你好,我是Flutter的新手,目前正在构建一个聊天应用程序。
我有一个配置文件制作器屏幕,用户可以在其中上传图像以设置其头像。我正在使用val = subprocess.check_call显示上传屏幕。我想知道如何在上载完成后自动导航到下一个屏幕,即我的主主屏幕,从而使用户不必等待任何按钮按下。

这是我尝试过的代码

CircularProgressIndicator()

上传代码

progressString != '100% Completed' ? Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
                  CircularProgressIndicator(
                        backgroundColor: Colors.blue,
                  ),
                  SizedBox(
                      height: 20.0,
                  ),
                   Text("Uploading File : $progressString",
                        style: TextStyle(
                            color: Colors.white54,
                            fontSize: 20.0,
                            fontWeight: FontWeight.w900,
                           ),
                          ),
                         ],
                        ) : Navigator.pushReplacement(context,
                              MaterialPageRoute(builder: (context) {
                                 return LoginPage();
                              }),
                            ), 

3 个答案:

答案 0 :(得分:1)

上传完成后,您应该将导航器设置在上传功能内

Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: <Widget>[
         CircularProgressIndicator(
             backgroundColor: Colors.blue,
         ),
         SizedBox(
             height: 20.0,
         ),
         Text("Uploading File : $progressString",
              style: TextStyle(
                   color: Colors.white54,
                   fontSize: 20.0,
                   fontWeight: FontWeight.w900,
              ),
         ),
     ],
), 

FormData data = FormData.fromMap({
    "username": userName.toString(),
    "name": naMe.toString(),
    "birthday": birthDay.toString(),
    "about": aboutUser.toString(),
    "sender": sendUser.toString(),
    "mobile": userMobile.toString(),
    "avatar": _image != null
        ? await MultipartFile.fromFile(_image.path,
            filename: avatarName.toString())
        : Text('Invalid Avatar'),
  });

  if (_validateAndSave()) {
    final token = widget.token;

    try {
      Dio dio = Dio();
      dio.options.headers['Accept'] = "application/json";
      dio.options.headers['Authorization'] = "Bearer $token";
      dio.options.headers['Content-Type'] = "multipart/form-data";
      dio.options.followRedirects = false;

      var response = await dio.post(url,
          data: data, onSendProgress: (int rec, int total) {
        setState(() {
          uploading = true;
          progressString = ((rec / total * 100).toString());
        });
      });

      var responseCode = response.statusCode;
      print('Dio responseCode : $responseCode');

 } on DioError catch (err) {
      var responseCode = err.response.statusCode;
      print(responseCode);
    }

      Future.delaye(Duration(milliseconds: 100), (){
          Navigator.pushReplacement(this.context,
             MaterialPageRoute(builder: (context) {
                  return LoginPage();
             }),
          );
      });
  }

答案 1 :(得分:0)

上传完成后,更新用户界面以向用户显示上传完成 ,并添加一个后框架回调以导航到下一页。

答案 2 :(得分:0)

步骤 1:在 lib 文件夹下创建一个新文件,即 splashscreen.dart 文件。在 main.dart 文件中引用 SplashScreen()。

文件名:main.dart

import 'package:flutter/material.dart';
import 'package:mfitz/splashscreen.dart';

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

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

第 2 步:在 splashscreen.dart 文件下创建初始屏幕所需的 UI,并在 void initState() 方法下包含以下代码以在 5 秒后导航到新屏幕。

Timer(Duration(seconds: 5), () {
              Navigator.of(context)
                  .pushReplacement(MaterialPageRoute(builder: (_) => MainScreen()));

启动画面代码

文件名:splashscreen.dart

    import 'dart:async';
    
    import 'package:flutter/material.dart';
    import 'package:google_fonts/google_fonts.dart';
    
    import 'mainScreen.dart';
    
    class SplashScreen extends StatefulWidget {
      @override
      _SplashScreenState createState() => _SplashScreenState();
    }
    
    class _SplashScreenState extends State<SplashScreen> {
      @override
      void initState() {
        super.initState();
//Navigates to new screen after 5 seconds.
        Timer(Duration(seconds: 5), () {
          Navigator.of(context)
              .pushReplacement(MaterialPageRoute(builder: (_) => MainScreen()));
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Stack(
            fit: StackFit.expand,
            children: [
              Container(
                constraints: BoxConstraints.expand(),
                decoration: BoxDecoration(
                  image: new DecorationImage(
                    image: AssetImage('assets/images/img2.jpg'),
                    fit: BoxFit.fill,
                  ),
                ),
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Expanded(
                    flex: 2,
                    child: Container(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          CircleAvatar(
                            backgroundColor: Colors.grey[100],
                            radius: 80.0,
                            child: Text(
                              "MOBIFIT.",
                              style: GoogleFonts.aldrich(
                                  fontWeight: FontWeight.bold,
                                  color: Colors.black,
                                  fontSize: 30.0),
                              textAlign: TextAlign.center,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                      flex: 1,
                      child: Column(
                        children: [
                          Padding(
                            padding: EdgeInsets.only(right: 100.0, left: 100.0),
                            child: LinearProgressIndicator(
                              backgroundColor: Colors.white,
                              valueColor:
                                  AlwaysStoppedAnimation<Color>(Colors.grey),
                              minHeight: 10.0,
                            ),
                          ),
                          Padding(padding: EdgeInsets.only(bottom: 10.0))
                        ],
                      ))
                ],
              )
            ],
          ),
        );
      }
    }

启动画面 Splashscreen along with Linear Progress Indicator

主屏幕 enter image description here