Let's assume I have this simple app:
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Generated App',
theme: new ThemeData(
primarySwatch: Colors.blue,
primaryColor: const Color(0xFF2196f3),
accentColor: const Color(0xFF2196f3),
canvasColor: const Color(0xFFfafafa),
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('App Name'),
),
body:
new Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
/* widgets to extract */
new Text(
"qWerty1",
style: new TextStyle(fontSize:12.0,
color: const Color(0xFF000000),
fontWeight: FontWeight.w200,
fontFamily: "Roboto"),
),
new Icon(
Icons.insert_emoticon,
color: const Color(0xFF000000),
size: 48.0),
/* widgets to extract */
new Icon(
Icons.insert_emoticon,
color: const Color(0xFF000000),
size: 48.0),
new Icon(
Icons.insert_emoticon,
color: const Color(0xFF000000),
size: 48.0)
]
),
);
}
}
If I want to extract the first Text
and Icon
widgets into separate widget (for e.g. I want to create a reusable widget), I need to wrap them with another Column
. So now instead of having one Column
, I now have 2 nested ones.
My extracted widget:
class Media extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Text(
"qWerty1",
style: new TextStyle(fontSize:12.0,
color: const Color(0xFF000000),
fontWeight: FontWeight.w200,
fontFamily: "Roboto"),
),
new Icon(
Icons.insert_emoticon,
color: const Color(0xFF000000),
size: 48.0),
]
);
}
}
What I'm trying to say here is, that my purpose with extracting widgets into separate ones was to clean up my code, but now I introduced another widget (Column) into the widget three, which doesn't makes sense to me in the long run. For e.g. If I have a long dashboard screen, where I have a lot of widgets. If I then "refactor" them into separate widgets like above, then I will got a cluttered widget three with lots of Columns
. Maybe I'm totally wrong and I should extract them in a different way. Can someone correct me on this?