xcode - slider 内 flutter 图像未出现

标签 xcode android-studio flutter dart flutter-layout

我对应用程序开发还很陌生,我有一些关于 Flutter 的问题,希望有人能帮助我!

首先,我尝试在我的代码中编写介绍幻灯片部分。我在网上找到了这段代码( https://flutterawesome.com/simple-and-configurable-app-introduction-slider-for-flutter/ ),当我尝试使用我自己的图像执行它时,似乎只打印背景颜色。当我删除背景颜色时,它只是一个白色的屏幕。我的 pageImage 部分正确吗?我在各处保存了一个断言文件夹,所以我不确定这是否是问题所在。我在最后添加了我的代码。

感谢您的宝贵时间!

class _MyHomePageState extends State<MyHomePage> {
   List<Slide> slides = new List();

  @override
  void initState() {
    super.initState();

    slides.add(
      new Slide(
        title: "ERASER",
        description: "Allow miles wound place the leave had. To sitting subject no improve studied limited",
        pathImage: "assets/images/1.png",
        backgroundColor: Colors.pink[200],
      ),
    );
    slides.add(
      new Slide(
        title: "PENCIL",
        description: "Ye indulgence unreserved connection alteration appearance",
        pathImage: "assets/images/1.png",
        backgroundColor: Colors.blue[200],
      ),
    );
    slides.add(
      new Slide(
        title: "RULER",
        description:
        "Much evil soon high in hope do view. Out may few northward believing attempted. Yet timed being songs marry one defer men our. Although finished blessing do of",
        pathImage: "assets/images/3.jpg",
      ),
    );
  }

  void onDonePress() {
    // TODO: go to next screen
  }

  void onSkipPress() {
    // TODO: go to next screen
  }


  @override
  Widget build(BuildContext context) {
    return new IntroSlider(
      slides: this.slides,
      onDonePress: this.onDonePress,
      onSkipPress: this.onSkipPress,
    );
  }
}

**解决方案:在 pubspec 页面中编辑资源

编辑: enter image description here

左侧(橙色部分)是我希望蓝色图像的显示方式:不滚动并填充整个页面。但是,我尝试通过编辑宽度和高度来使我的图像(右侧)填充页面,并且我开始必须滚动图像下方和上方有粉红色背景的位置(我认为这是因为它必须始终居中)图片)。 有什么方法可以让我的图像成为背景,就像左边的图片一样吗?我知道橙色背景是背景颜色,但希望通过比较两者它是有意义的。谢谢!

最佳答案

我创建了新的介绍小部件。这是代码。

import 'package:flutter/material.dart';

class MyIntroView extends StatefulWidget {
  final List<Widget> pages;
  final VoidCallback onIntroCompleted;

  const MyIntroView({
    Key key,
    @required this.pages,
    @required this.onIntroCompleted,
  })  : assert(pages != null),
        assert(onIntroCompleted != null),
        super(key: key);

  @override
  _MyIntroViewState createState() => _MyIntroViewState();
}

class _MyIntroViewState extends State<MyIntroView> {
  PageController _pageController;
  int _currentPage = 0;

  @override
  void initState() {
    _pageController = PageController(
      initialPage: _currentPage,
    );
    super.initState();
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        NotificationListener<ScrollEndNotification>(
          onNotification: (x) {
            setState(() {
              _currentPage = _pageController.page.round();
            });
            return false;
          },
          child: PageView(
            children: widget.pages,
            controller: _pageController,
          ),
        ),
        Align(
          alignment: Alignment.bottomCenter,
          child: _buildBottomButtons(),
        ),
      ],
    );
  }

  bool get _isFinalPage => _currentPage == widget.pages.length - 1;

  Widget _buildBottomButtons() {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Opacity(
            opacity: _isFinalPage ? 0.0 : 1.0,
            child: _buildButton("SKIP", _gotoLastPage),
          ),
          _buildNavIndicator(),
          _isFinalPage
              ? _buildButton("DONE", widget.onIntroCompleted)
              : _buildButton("NEXT", _gotoNextPage),
        ],
      ),
    );
  }

  Widget _buildButton(String title, VoidCallback callback) {
    return FlatButton(
      child: Text(
        title,
        style: TextStyle(color: Colors.white),
      ),
      onPressed: callback,
    );
  }

  void _gotoLastPage() {
    _pageController.animateToPage(
      widget.pages.length - 1,
      duration: const Duration(milliseconds: 600),
      curve: Curves.ease,
    );
  }

  void _gotoNextPage() {
    _pageController.nextPage(
      duration: const Duration(milliseconds: 600),
      curve: Curves.easeInOut,
    );
  }

  Widget _buildNavIndicator() {
    final indicatorList = <Widget>[];
    for (int i = 0; i < widget.pages.length; i++)
      indicatorList.add(_buildIndicator(i == _currentPage));
    return Row(children: indicatorList);
  }

  Widget _buildIndicator(bool isActive) {
    return Padding(
      padding: const EdgeInsets.all(5.0),
      child: DecoratedBox(
        decoration: BoxDecoration(
          shape: BoxShape.circle,
          color: isActive ? Colors.white : Colors.white30,
        ),
        child: SizedBox(width: 8, height: 8),
      ),
    );
  }
}

用法:

import 'package:flutter/material.dart';
import 'package:flutter_app_test3/my_intro_view.dart';

Future<void> main() async {
  runApp(MyApp());
}

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

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MyIntroView(
      pages: <Widget>[
        Image.asset("assets/images/1.png", fit: BoxFit.cover),
        Image.asset("assets/images/2.png", fit: BoxFit.cover),
        Image.asset("assets/images/3.jpg", fit: BoxFit.cover),
      ],
      onIntroCompleted: () {
        print("Into is Completed");
        //To the navigation stuff here
      },
    );
  }
}

如果您有任何疑问,请在评论中询问我

关于xcode - slider 内 flutter 图像未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59442660/

相关文章:

xcode - 启动模拟器失败并出现错误 : No template

ios - Xcode 4.3.2 缺少 iOS 4.0 模拟器

arrays - 在 swift 中向数组添加函数

android - OpenCV Android Studio 设置问题

flutter - 如何使下拉按钮创建的列表形状呈圆形?

firebase - Flutter - 删除一个 firebase 文档 onTap()

swift - macOS,工具栏不允许使用 segue 类型的弹出窗口

android - 错误 :No such property: bootClasspath for class: com. android.build.gradle.LibraryPlugin

安卓工作室 : Application Installation Failed

dart - 无法添加到onTap中的集合中