Вы можете скопировать и вставить полный код ниже
Ваш код, показанный в вопросе, работает хорошо, я только добавил ListView.builder
part
Я изменяю размер иконки на 30 для демонстрации
фрагмент кода
child: ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: _categories.length,
itemBuilder: (BuildContext context, int index) {
return _categories[index];
}),
рабочая демоверсия
полный код
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class Category extends StatelessWidget {
final String title;
final IconData icon;
Category({@required this.title, @required this.icon});
@override
Widget build(BuildContext context) {
return Center(
child: Container(
color: Colors.blue,
height: 100.0,
width: 200.0,
child: InkWell(
onTap: () {
print('$title was tapped!');
},
child: Padding(
padding: EdgeInsets.all(10),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Icon(
icon,
color: Colors.white,
size: 30,
), // <===== My problem is here
Text(
this.title,
style: TextStyle(fontSize: 24),
)
],
)),
),
),
);
}
}
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;
var _categories = [
Category(title: "Test 1", icon: Icons.airline_seat_flat),
Category(title: "Test 2", icon: Icons.airline_seat_flat_angled),
Category(title: "Test 3", icon: Icons.airline_seat_individual_suite),
Category(title: "Test 4", icon: Icons.airline_seat_legroom_extra)
];
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>[
Expanded(
child: ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: _categories.length,
itemBuilder: (BuildContext context, int index) {
return _categories[index];
}),
),
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),
),
);
}
}