You can hide children (rows) inside a Rows Widget by using a combination of if statement and spread operator in dart. This is applicabe to almost all the widgets that has children property. The code would look something like this -
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(
"Row 1",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
Text(
"Row 2",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
//Our conditional Rows
if (_condition) ...[
Text(
"Row 3",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
Text(
"Row 4",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
]
],
),
As you can see first two rows are fixed while the last two rows can be added/removed based on the _condition. This conditional list will be evaluated if _condition is true.
Here is sample app that demonstrate this behavior, this app uses a button to toggle the _condition.
import 'package:flutter/material.dart';
void main() {
//Run the App Widget
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Demo App",
theme: ThemeData(
primarySwatch: Colors.amber,
),
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
bool _condition = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text("Dynamic Row"),
),
body: Container(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(
"Row 1",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
Text(
"Row 2",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
if (_condition) ...[
Text(
"Row 3",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
Text(
"Row 4",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
]
],
),
),
),
floatingActionButtonAnimator: FloatingActionButtonAnimator.scaling,
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButton: RaisedButton(
color: Colors.red,
animationDuration: Duration(microseconds: 500),
child: Text(
"Toggle",
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
fontWeight: FontWeight.w600,
),
),
onPressed: () {
setState(() {
_condition = !_condition;
});
},
),
);
}
}
Here's the output -

Hope this helps!