Skip to content

Instantly share code, notes, and snippets.

@jezell
Created June 1, 2024 00:19
Show Gist options
  • Save jezell/19e28738fb0b82fc2bd9bde8e3af6b38 to your computer and use it in GitHub Desktop.
Save jezell/19e28738fb0b82fc2bd9bde8e3af6b38 to your computer and use it in GitHub Desktop.
// Copyright 2019 the Dart project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class CenteredText extends StatelessWidget {
const CenteredText(this.text, {super.key, required this.style});
final TextStyle style;
final String text;
Size measureHeight(String text, TextStyle style, double maxWidth) {
final textPainter = TextPainter(
text: TextSpan(text: text, style: style),
maxLines: 1,
textDirection: TextDirection.ltr,
textAlign: TextAlign.left);
try {
textPainter.layout();
final height =
textPainter.computeDistanceToActualBaseline(TextBaseline.ideographic);
return Size(textPainter.width, height);
} finally {
textPainter.dispose();
}
}
@override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, constraints) {
final size = measureHeight(text, style, constraints.maxWidth);
return Stack(children: [
Positioned(
left: (constraints.maxWidth - size.width) / 2,
top: (constraints.maxHeight - size.height) / 2,
child: Text(text, style: style)),
]);
});
}
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
super.key,
required this.title,
});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
final decoration1 = BoxDecoration(
color: Colors.black,
border: Border.all(color: Colors.red, width: 15),
borderRadius: BorderRadius.circular(100));
final decoration2 = BoxDecoration(
color: Colors.black, borderRadius: BorderRadius.circular(100));
final style = TextStyle(
fontSize: 150, color: Colors.white, height: 1, fontFamily: "Helvetica");
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(mainAxisSize: MainAxisSize.min, children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
decoration: decoration1,
height: 160,
width: 400,
child: Center(child: Text("123", style: style))),
SizedBox(width: 10),
Container(
decoration: decoration1,
height: 160,
width: 400,
child: CenteredText("123", style: style)),
],
),
SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
decoration: decoration2,
height: 160,
width: 400,
child: Center(child: Text("123", style: style))),
SizedBox(width: 10),
Container(
decoration: decoration2,
height: 160,
width: 400,
child: CenteredText("123", style: style)),
],
),
]),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment