import 'package:flutter/material.dart';
class LayOutBuilder extends StatelessWidget {
const LayOutBuilder({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: LayoutBuilder(
builder: (context, p1) {
if (p1.maxHeight < 400) {
return Container();
}
},
),
);
}
}
I dont know why it does not run.
CodePudding user response:
you're returning a Container
only if p1.maxHeight < 400
, but you didn't specify what to return if p1.maxHeight < 400
is not true, hence it will return null, and that's not allowed because it has to return something
if (p1.maxHeight < 400) {
return Container();
} else {
return Text('some widget');
}
CodePudding user response:
The builder
argument needs to be a function that returns a Widget. Your implementation only returns a Widget under some if-condition. In the else-case, it does not return anything. This is not allowed and throws a compile error.
You should return a Widget in all cases. Which widget specifically depends on your use case. But something like this will compile:
return Scaffold(
body: LayoutBuilder(
builder: (context, p1) {
if (p1.maxHeight < 400) {
return Container();
} else {
return SizedBox(height: 0) // Or any other widget
}
}),
);