Skip to content

Instantly share code, notes, and snippets.

@romanejaquez
Created March 1, 2022 02:46
Show Gist options
  • Save romanejaquez/365759c64de2c59aed6f7361c711bf47 to your computer and use it in GitHub Desktop.
Save romanejaquez/365759c64de2c59aed6f7361c711bf47 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
List<String> _brandnames = ['Toyota', 'Honda', 'BMW', 'Mercedes'];
List<TextEditingController> controllers = [];
@override
Widget build(BuildContext context) {
controllers = [];
return Column(
children: [
Expanded(
child: Container(
child: ListView.builder(
itemCount: _brandnames.length,
itemBuilder: (BuildContext ctx, int index) {
TextEditingController ctrl = TextEditingController();
controllers.add(ctrl);
return BrandNamesWidget(
brandName: _brandnames[index].toString(),
controller: ctrl,
index: index);
}),
)
),
TextButton(
onPressed: () {
for(var ctrl in controllers) {
print(ctrl.text);
}
},
child: Text('Click me to get their values')
)
]
);
}
}
class BrandNamesWidget extends StatelessWidget {
final TextEditingController? controller;
final String? brandName;
final int? index;
BrandNamesWidget({ this.brandName, required this.index, this.controller });
@override
Widget build(BuildContext context) {
double height = 100;
double width = MediaQuery.of(context).size.width;
return TextField(
controller: controller!,
keyboardType: TextInputType.text,
decoration: InputDecoration(
constraints: BoxConstraints(maxWidth: width * 0.45),
labelText: 'Brand name: ' + index.toString(),
prefixIcon: const Icon(Icons.medication),
labelStyle: TextStyle(
fontSize: height * 0.03,
color: Colors.grey,
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(50),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment