I am currently trying to make the content of my Container scrollable. The child of the Container is a Stack Widget, however, if SingleChildScrollView is used as the parent of Stack, then an exception is thrown.
Is there a solution to the problem I am getting?
Please check below the minimum reproducible example:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(title: const Text('Minimum reproducible code')),
body: Card(
child: ListTile(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
title: Row(children: [
Expanded(
flex: 1,
child: Container(
decoration: BoxDecoration(border: Border.all(color: Colors.black38), borderRadius: BorderRadius.circular(10)),
height: 130,
width: 500,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Stack(children: const [
Positioned(
top: 16,
child: Text(
'First line: My first line is to long and needs to be scrollable',
),
),
Positioned(
top: 42,
child: Text(
'Second line: Short Text',
),
),
]),
),
),
),
const SizedBox(
width: 20,
),
Expanded(
flex: 1,
child: Container(
decoration: BoxDecoration(border: Border.all(color: Colors.black38), borderRadius: BorderRadius.circular(10)),
height: 130,
width: 200,
),
),
]),
),
),
),
);
}
}
Thank you in advance!
CodePudding user response:
Here is a possible solution. Just replace Scaffold widget in your code with the Scaffold widget I am sharing below.
Scaffold(
appBar: AppBar(title: const Text('Minimum reproducible code')),
body: Card(
child: ListTile(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
title: Row(mainAxisSize: MainAxisSize.min, children: [
Container(
decoration: BoxDecoration(border: Border.all(color: Colors.black38), borderRadius: BorderRadius.circular(10)),
height: 130,
width: 200,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: const [
Text(" First Line: A text that is tooo long to fit in the container"),
Text(" Second Line: short text")
]),
),
),
const SizedBox(
width: 20,
),
Container(
decoration: BoxDecoration(border: Border.all(color: Colors.black38), borderRadius: BorderRadius.circular(10)),
height: 130,
width: 200,
),
]),
),
),
)