Answers for "flutter copywith()"

2

dart copywith

//copyWith allows us to copy a T and pass in arguments that overwrite 
//settable values.
class _HomePageState extends State<HomePage> {
  List<Product> products = [];

  @override
  void initState() {
    super.initState();

    Product sixSeaterDiningTableBrown = Product(
      id: "0",
      name: "6 Seater Dining Table",
      color: Colors.brown,
    );

    Product sixSeaterDiningTableBlack =
        sixSeaterDiningTableBrown.copyWith(color: Colors.black, id: "1");

    products.add(sixSeaterDiningTableBrown);
    products.add(sixSeaterDiningTableBlack);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Hello, copyWith!"),
      ),
      body: ListView(
          children: products
              .map((Product product) => ListTile(
                    trailing: Container(
                      width: 14,
                      height: 14,
                      color: product.color,
                    ),
                    title: Text(product.name),
                    subtitle: Text(product.id),
                  ))
              .toList()),
    );
  }
}
Posted by: Guest on August-08-2021

Code answers related to "Dart"

Browse Popular Code Answers by Language