Skip to content

Instantly share code, notes, and snippets.

@hectorAguero
Created January 27, 2026 14:30
Show Gist options
  • Select an option

  • Save hectorAguero/028f25ef06345443ebebe36ad1c78d4b to your computer and use it in GitHub Desktop.

Select an option

Save hectorAguero/028f25ef06345443ebebe36ad1c78d4b to your computer and use it in GitHub Desktop.
First and Last item padding
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