Flutter Tutorials

Flutter app development tutorials


flutter – PageView disable swipe

main.dart


import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

class FlutterExample extends StatelessWidget {
  const FlutterExample({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Example',
        theme: ThemeData(primarySwatch: Colors.orange),
        home: const StateExample()
    );
  }
}

class StateExample extends StatefulWidget {
  const StateExample({Key? key}) : super(key: key);

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

class _StateExampleState extends State<StateExample> {
  final PageController controller = PageController(initialPage: 0);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: const Color(0xFFF5F5F5),
        appBar: AppBar(
            title: const Text("Flutter - PageView Disable Swipe")
        ),
        body: bodyContent()
    );
  }


  bodyContent(){
    return PageView(
      scrollDirection: Axis.horizontal,
      controller: controller,
      // scroll physics that does not let the user scroll.
      physics:const NeverScrollableScrollPhysics(),
      children: <Widget>[
        pageContent(Colors.grey, "Grey"),
        pageContent(Colors.purple, "Purple"),
        pageContent(Colors.teal, "Teal"),
      ],
    );
  }

  pageContent(Color color, String text){
    return Container(
      color: color,
      child: Center(
        child: Text(
          text,
          style: const TextStyle(
              fontSize: 45,
              fontWeight: FontWeight.bold
          ),
        ),
      ),
    );
  }
}

More flutter tutorials



About Me

Flutter Developer

Design a site like this with WordPress.com
Get started