Skip to content

Instantly share code, notes, and snippets.

@passsy
Created June 17, 2020 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save passsy/f4fd3fd0c72cee180a84b03ba789409a to your computer and use it in GitHub Desktop.
Save passsy/f4fd3fd0c72cee180a84b03ba789409a to your computer and use it in GitHub Desktop.
wrap_content match_parent in Flutter
import 'package:flutter/material.dart';
Future<void> main() async {
runApp(Builder(
builder: (context) => const MyApp(),
));
}
class MyApp extends StatelessWidget {
const MyApp({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("Title"),
),
body: Align(
alignment: Alignment.topCenter,
child: AndroidLayout(
height: 48,
width: wrap_content, // <-- Replace wrap_content with match_parent to fix the layout
// width: match_parent,
child: Container(
color: Colors.black26,
child: Stack(
alignment: Alignment.center,
children: const [
Positioned(
left: 8,
top: 0,
bottom: 0,
child: Icon(Icons.arrow_back),
),
Text("Centered"),
],
),
),
),
),
),
);
}
}
class AndroidLayout extends StatelessWidget {
const AndroidLayout({Key key, @required this.width, @required this.height, this.child}) : super(key: key);
final double width;
final double height;
final Widget child;
@override
Widget build(BuildContext context) {
return ConstrainedBox(
constraints: BoxConstraints(
minWidth: width,
minHeight: height,
),
child: child,
);
}
}
const double match_parent = double.infinity;
const double wrap_content = 0.0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment