Flutterpageview切換指示器的實(shí)現(xiàn)代碼

PageView 是一個(gè)滑動(dòng)視圖列表,它也是繼承至 CustomScrollView 的。

站在用戶(hù)的角度思考問(wèn)題,與客戶(hù)深入溝通,找到都勻網(wǎng)站設(shè)計(jì)與都勻網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶(hù)體驗(yàn)好的作品,建站類(lèi)型包括:成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、主機(jī)域名、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋都勻地區(qū)。

在 PageView 里有三個(gè)構(gòu)造函數(shù):

  • PageView - 創(chuàng)建一個(gè)可滾動(dòng)列表。
  • PageView.builder - 創(chuàng)建一個(gè)滾動(dòng)列表,指定數(shù)量。
  • PageView.custom - 創(chuàng)建一個(gè)可滾動(dòng)的列表,自定義子項(xiàng)。

效果

Flutter pageview切換指示器的實(shí)現(xiàn)代碼

代碼

// Copyright 2017, the Flutter project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

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

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

class MyApp extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
  return new MaterialApp(
   title: 'Flutter Demo',
   home: new MyHomePage(),
   debugShowCheckedModeBanner: false,
  );
 }
}

/// An indicator showing the currently selected page of a PageController
class DotsIndicator extends AnimatedWidget {
 DotsIndicator({
  this.controller,
  this.itemCount,
  this.onPageSelected,
  this.color: Colors.white,
 }) : super(listenable: controller);

 /// The PageController that this DotsIndicator is representing.
 final PageController controller;

 /// The number of items managed by the PageController
 final int itemCount;

 /// Called when a dot is tapped
 final ValueChanged<int> onPageSelected;

 /// The color of the dots.
 ///
 /// Defaults to `Colors.white`.
 final Color color;

 // The base size of the dots
 static const double _kDotSize = 8.0;

 // The increase in the size of the selected dot
 static const double _kMaxZoom = 2.0;

 // The distance between the center of each dot
 static const double _kDotSpacing = 25.0;

 Widget _buildDot(int index) {
  double selectedness = Curves.easeOut.transform(
   max(
    0.0,
    1.0 - ((controller.page ?? controller.initialPage) - index).abs(),
   ),
  );
  double zoom = 1.0 + (_kMaxZoom - 1.0) * selectedness;
  return new Container(
   width: _kDotSpacing,
   child: new Center(
    child: new Material(
     color: color,
     type: MaterialType.circle,
     child: new Container(
      width: _kDotSize * zoom,
      height: _kDotSize * zoom,
      child: new InkWell(
       onTap: () => onPageSelected(index),
      ),
     ),
    ),
   ),
  );
 }

 Widget build(BuildContext context) {
  return new Row(
   mainAxisAlignment: MainAxisAlignment.center,
   children: new List<Widget>.generate(itemCount, _buildDot),
  );
 }
}

class MyHomePage extends StatefulWidget {
 @override
 State createState() => new MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> {

 final _controller = new PageController();

 static const _kDuration = const Duration(milliseconds: 300);

 static const _kCurve = Curves.ease;

 final _kArrowColor = Colors.black.withOpacity(0.8);

 final List<Widget> _pages = <Widget>[
  new ConstrainedBox(
   constraints: const BoxConstraints.expand(),
   child: new FlutterLogo(colors: Colors.blue),
  ),
  new ConstrainedBox(
   constraints: const BoxConstraints.expand(),
   child: new FlutterLogo(style: FlutterLogoStyle.stacked, colors: Colors.red),
  ),
  new ConstrainedBox(
   constraints: const BoxConstraints.expand(),
   child: new FlutterLogo(style: FlutterLogoStyle.horizontal, colors: Colors.green),
  ),
 ];

 @override
 Widget build(BuildContext context) {
  return new Scaffold(
   body: new IconTheme(
    data: new IconThemeData(color: _kArrowColor),
    child: new Stack(
     children: <Widget>[
      new PageView.builder(
       physics: new AlwaysScrollableScrollPhysics(),
       controller: _controller,
       itemBuilder: (BuildContext context, int index) {
        return _pages[index % _pages.length];
       },
      ),
      new Positioned(
       bottom: 0.0,
       left: 0.0,
       right: 0.0,
       child: new Container(
        color: Colors.grey[800].withOpacity(0.5),
        padding: const EdgeInsets.all(20.0),
        child: new Center(
         child: new DotsIndicator(
          controller: _controller,
          itemCount: _pages.length,
          onPageSelected: (int page) {
           _controller.animateToPage(
            page,
            duration: _kDuration,
            curve: _kCurve,
           );
          },
         ),
        ),
       ),
      ),
     ],
    ),
   ),
  );
 }
}

PageView 有以下常用屬性:

  • childrenDelegate → SliverChildDelegate - 子項(xiàng)列表。
  • controller → PageController - 控制臺(tái)。
  • onPageChanged → ValueChanged - 索引改變時(shí)觸發(fā)。
  • pageSnapping → bool - 設(shè)置為 false 以禁用頁(yè)面捕捉,對(duì)自定義滾動(dòng)行為很有用。
  • physics → ScrollPhysics - 頁(yè)面視圖如何響應(yīng)用戶(hù)輸入,即滾動(dòng)的動(dòng)畫(huà)表現(xiàn)。
  • reverse → bool - 是否反方向。
  • scrollDirection → Axis - 視圖滾動(dòng)的方向。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

名稱(chēng)欄目:Flutterpageview切換指示器的實(shí)現(xiàn)代碼
文章源于:http://bm7419.com/article6/iiojog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航Google、域名注冊(cè)品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)