How I can expand a height of a Widget dynamically to a parent widget.
Specially in my example I want to expand the red bar to parent widgets. My text can be changed to x lines.
Current code:
import 'package:flutter/material.dart';
class ExampleView extends StatelessWidget {
final String title;
const ExampleView({Key key, this.onTap, this.title}) : super(key: key);
final text = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et';
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(left: 40),
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
color: Colors.grey[200],
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
margin: const EdgeInsets.only(right: 12, top: 4, bottom: 4),
width: 6,
height: 80, // FIXME: Dynamic
color: Colors.red,
),
Expanded(
child: Text(text),
)
],
),
);
}
}
Current solution:
Desired solution:
Thank you in advance for your help!
You can copy paste run full code below
You can use IntrinsicHeight
code snippet
IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
margin: const EdgeInsets.only(right: 12, top: 4, bottom: 4),
width: 6,
color: Colors.red,
),
working demo
full code
import 'package:flutter/material.dart';
class ExampleView extends StatelessWidget {
final String title;
const ExampleView({Key key, this.title}) : super(key: key);
final text = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et';
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(left: 40),
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
color: Colors.grey[200],
child: IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
margin: const EdgeInsets.only(right: 12, top: 4, bottom: 4),
width: 6,
color: Colors.red,
),
Expanded(
child: Text(text),
)
],
),
),
);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ExampleView(),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Solution #1
In this particular case, you do not need to use Row widget. You can add your red column as a Border instead of a Container widget. The code would then look like this:
class ExampleView extends StatelessWidget {
final String title;
const ExampleView({Key key, this.title}) : super(key: key);
final text =
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et';
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(left: 40),
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
color: Colors.grey[200],
child: Container(
decoration: BoxDecoration(
border: Border(left: BorderSide(width: 6, color: Colors.red))),
margin: const EdgeInsets.only(right: 12, top: 4, bottom: 4),
padding: const EdgeInsets.only(left: 8),
child: Text(text),
),
);
}
}
Solution #2
Instead of a border, you can translate your child Container and wrap it with one more Container that has a gradient.
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(left: 40),
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
color: Colors.grey[200],
child: Container(
margin: const EdgeInsets.only(right: 12, top: 4, bottom: 4),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color.fromRGBO(255, 25, 25, 1),
Color.fromRGBO(180, 21, 236, 1)
],
)),
child: Container(
color: Colors.grey[200],
transform: Matrix4.translationValues(6, 0, 0),
padding: const EdgeInsets.only(left: 8),
child: Text(text),
),
),
);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.