I want align the following nested list in such a way that black line should be horizontally center of the green box (parent list) i cant add any padding or extra space or positioned or increase the red column width
please note black line is for reference the line actually not part of the code
dartpad ready code here
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Horizontal List of Vertical Lists')),
body: ParentLayout(),
),
);
}
}
class ParentLayout extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
children: [
// Header content (Green Box at the top)
Container(
height: 100,
width: 200,
color: Colors.green[100],
child: Center(child: Text("Parent ListView Content Above")),
),
// Horizontal layout with dividing line centered to green boxes
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Blue List
_generateBlueList(),
// Dividing line
// Red List
_generateRedList(),
],
),
),
// Footer content (Green Box at the bottom)
Container(
height: 100,
width: 200,
color: Colors.green[100],
child: Center(child: Text("Parent ListView Content Below")),
),
],
);
}
// Generate blue list with varying sizes
Widget _generateBlueList() {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: List.generate(
5,
(innerIndex) => Container(
height: 40 + (innerIndex * 25), // Varying height
width: 80 + (innerIndex * 40), // Varying width
color: Colors.blue[100 * (innerIndex + 1)],
child: Center(
child: Text(
'Blue $innerIndex',
style: TextStyle(color: Colors.white),
),
),
),
),
);
}
// Generate red list with fixed sizes
Widget _generateRedList() {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: List.generate(
5,
(innerIndex) => Container(
height: 60, // Fixed height
width: 100, // Fixed width
color: Colors.red[100 * (innerIndex + 1)],
child: Center(
child: Text(
'Red $innerIndex',
style: TextStyle(color: Colors.white),
),
),
),
),
);
}
}
2
Answers
I did bit of math to find necessary x axis translate, and achieved it.
to make the other neighbouring widget respect the translated widget position, im adding padding to entire parent column. And its not wasting the space, because transformed widget is laying there.
Here’s what you want,
just changing some cross axis alignments of both columns of green and red containers, and i have added expanded widget.
Here’s your refactored code: