Created
January 27, 2026 14:30
-
-
Save hectorAguero/028f25ef06345443ebebe36ad1c78d4b to your computer and use it in GitHub Desktop.
First and Last item padding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| void main() { | |
| runApp(const MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| const MyApp({super.key}); | |
| static const items = ['First', "Second", "Thirds"]; | |
| @override | |
| Widget build(BuildContext context) { | |
| final screenWidth = MediaQuery.of(context).size.width; | |
| const itemWidth = 305.0; | |
| const horizontalPadding = 24.0; | |
| final viewportFraction = itemWidth / (screenWidth - horizontalPadding); | |
| return MaterialApp( | |
| debugShowCheckedModeBanner: false, | |
| home: Scaffold( | |
| body: PageView.builder( | |
| padEnds: false, | |
| controller: PageController(viewportFraction: viewportFraction), | |
| itemCount: items.length, | |
| itemBuilder: (context, index) { | |
| return Padding( | |
| padding: EdgeInsets.only( | |
| left: index == 0 ? 24 : 0, | |
| right: index == items.length - 1 ? 24 : 0, | |
| ), | |
| child: SizedBox( | |
| width: 305, | |
| child: Card(child: Center(child: Text(items[index]))), | |
| ), | |
| ); | |
| }, | |
| ), | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment