【问题标题】:Flutter layout, alignment, page controller, and how to make OnboardingScreen only one time?Flutter 布局、对齐、页面控制器,如何让 Onboarding Screen 只有一次?
【发布时间】:2021-03-16 13:26:46
【问题描述】:

我正在为我的大学项目做项目,但我遇到了问题。

首先,我想在我的入职屏幕上添加一个背面,但我已经在右下角使用 align 来表示“下一个”,而我希望在左下角使用背面

第二,我想把标题(欢迎,目的,创作者)放在中间,但我不能把它放在中间,因为我使用的是 sizebox

第三,我希望引导屏幕只出现一次,所以当打开它的用户不会再看到它(基本上是第一次打开,引导屏幕,但第二次直接进入主页)

对不起我的英语 如果您想可视化,请拍照: Photo 代码如下:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../home_screen.dart';

class Onboarding extends StatefulWidget {
  @override
  _OnboardingScreen createState() => _OnboardingScreen();
}

class _OnboardingScreen extends State<Onboarding> {
  final int _numPages = 3;
  final PageController _pageController = PageController(initialPage: 0);
  int _currentPage = 0;

  List<Widget> _buildPageIndicator() {
    List<Widget> list = [];
    for (int i = 0; i < _numPages; i++) {
      list.add(i == _currentPage ? _indicator(true) : _indicator(false));
    }
    return list;
  }

  Widget _indicator(bool isActive) {
    return AnimatedContainer(
      duration: Duration(milliseconds: 150),
      margin: EdgeInsets.symmetric(horizontal: 8.0),
      height: 8.0,
      width: isActive ? 24.0 : 16.0,
      decoration: BoxDecoration(
        color: isActive ? Colors.white : Color(0xFF7B51D3),
        borderRadius: BorderRadius.all(Radius.circular(12)),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: AnnotatedRegion<SystemUiOverlayStyle>(
        value: SystemUiOverlayStyle.light,
        child: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              stops: [0.1, 0.4, 0.7, 0.9],
              colors: [
                Colors.black,
                Color(0xff112339),
                Color(0xff112339),
                Colors.black,
              ],
            ),
          ),
          child: Padding(
            padding: EdgeInsets.symmetric(vertical: 40.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                Container(
                  alignment: Alignment.centerRight,
                  child: FlatButton(
                    onPressed: () {
                      Navigator.push(
                      context,
                      MaterialPageRoute(builder: (context) => HomePage()),
                      );
                    },
                    child: Text(
                      'Skip',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 20.0,
                      ),
                    ),
                  ),
                ),
                Container(
                  height: 600.0,
                  child: PageView(
                    physics: ClampingScrollPhysics(),
                    controller: _pageController,
                    onPageChanged: (int page) {
                      setState(() {
                        _currentPage = page;
                      });
                    },
                    children: <Widget>[
                      Padding(
                        padding: EdgeInsets.only(top:70.0,left: 40.0,right: 40.0,),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Center(
                              child: Image(
                                image: AssetImage(
                                  'assets/images/Logo.png',
                                ),
                                height: 120.0,
                                width: 120.0,
                              ),
                            ),
                            SizedBox(height: 50.0),
                            Text(
                              'Welcome',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 21.0,
                                height: 1.5,
                              ),
                            ),
                            SizedBox(height: 30.0),
                            Text(
                              'Welcome to Moviez, the place where you will spend your time magically',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 16.0,
                                height: 1.2,
                                fontFamily: 'Raleway',
                              ),
                            ),
                          ],
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.only(top:70.0,left: 40.0,right: 40.0,),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Center(
                              child: Image(
                                image: AssetImage(
                                  'assets/images/Logo.png',
                                ),
                                height: 120.0,
                                width: 120.0,
                              ),
                            ),
                            SizedBox(height: 50.0),
                            Text(
                              'Purpose',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 21.0,
                                height: 1.5,
                              ),
                            ),
                            SizedBox(height: 30.0),
                            Text(
                              'This App is for educational purposes only',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 16.0,
                                height: 1.2,
                              ),
                            ),
                          ],
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.only(top:70.0,left: 40.0,right: 40.0,),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Center(
                              child: Image(
                                image: AssetImage(
                                  'assets/images/Logo.png',
                                ),
                                height: 120.0,
                                width: 120.0,
                              ),
                            ),
                            SizedBox(height: 50.0),
                            Text(
                              'Creator',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 21.0,
                                height: 1.5
                              ),
                            ),
                            SizedBox(height: 30.0),
                            Text(
                              'Adela, Caroline, Cordellya, David, Valentino',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 16.0,
                                height: 1.2,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: _buildPageIndicator(),
                ),
                _currentPage != _numPages - 1
                    ? Expanded(
                        child: Align(
                          alignment: FractionalOffset.bottomRight,
                          child: FlatButton(
                            onPressed: () {
                              _pageController.nextPage(
                                duration: Duration(milliseconds: 500),
                                curve: Curves.ease,
                              );
                            },
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              mainAxisSize: MainAxisSize.min,
                              children: <Widget>[
                                Text(
                                  'Next',
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 22.0,
                                  ),
                                ),
                                SizedBox(width: 10.0),
                                Icon(
                                  Icons.arrow_forward,
                                  color: Colors.white,
                                  size: 30.0,
                                ),
                              ],
                            ),
                          ),
                        ),
                      )
                    : Text(''),
              ],
            ),
          ),
        ),
      ),
      bottomSheet: _currentPage == _numPages - 1
          ? Container(
              height: 100.0,
              width: double.infinity,
              color: Colors.white,
              child: GestureDetector(
               onTap: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => HomePage()),
                );
                },
                child: Center(
                  child: Padding(
                    padding: EdgeInsets.only(bottom: 30.0),
                    child: Text(
                      'Get started',
                      style: TextStyle(
                        color: Color(0xFF5B16D0),
                        fontSize: 20.0,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ),
              ),
            )
          : Text(''),
    );
  }
}

这是我的 main.dart

import 'package:flutter/material.dart';
import 'package:movie_app/screens/home_screen/widgets/onboard.dart';
import 'screens/home_screen/home_screen.dart';

void main() {
  runApp(MyApp());
  Paint.enableDithering = true;
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Movie App',
      theme: ThemeData(
          primarySwatch: Colors.blue,
          iconTheme: IconThemeData(
            color: Colors.white,
          ),
          textTheme: ThemeData.light().textTheme.copyWith(
                bodyText1: TextStyle(
                  // fontFamily: 'OpenSans',
                  // fontWeight: FontWeight.bold,
                  fontSize: 22,
                  color: Colors.white,
                ),
                button: TextStyle(
                  color: Colors.white,
                ),
              ),
          appBarTheme: AppBarTheme(
            iconTheme: IconThemeData(color: Colors.white),
            actionsIconTheme: IconThemeData(size: 30),
          )),
      home: Onboarding(),
    );
  }
}

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    回答你的第一点: 假设您希望“后退”按钮与“下一个”按钮位于同一行,我建议您按如下方式编辑您的 Row 小部件。

    Row(children: [
      Icon(Icons.arrow_back),
      Text('Back'),
      Spacer(),
      Text('Next'),
      Icon(Icons.arrow_forward),
    ])
    

    我使用了两个Text 小部件只是为了向您展示它的外观。在您的应用程序中,您可以使用具有不同文本和不同onTaponPressed 方法的两个不同按钮。您可以查看this pageButton 部分,了解更多关于 Flutter 中可用的不同按钮的信息。

    第二:我认为你所要做的就是将相关的ColumncrossAxisAlignment属性从CrossAxisAlignment.start更改为CrossAxisAlignment.center

    【讨论】:

    • 至于第三个,我建议将其作为一个单独的问题发布。
    • 第二点是正确的,谢谢,我认为它会改变包括底部在内的整个文本。并回答第一点 pagecontroller 呢?如果我这样做不是两个按钮都会做下一页吗? cmiiw 我是 Flutter 的新手
    • 我编辑了我的答案来回答你的问题
    • 嘿,很抱歉回复晚了,我在 stackoverflow 上不是很活跃,是的,它确实帮助了我。再次感谢您,非常抱歉回复晚了
    猜你喜欢
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 2022-08-20
    • 2019-06-14
    • 1970-01-01
    相关资源
    最近更新 更多