Снимок экрана
Полное решение:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(scaffoldBackgroundColor: Colors.black, brightness: Brightness.dark),
home: Scaffold(
appBar: AppBar(title: Text("Choose Language")),
body: HomePage(),
),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _languageIndex = -1;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Stack(
alignment: Alignment.bottomCenter,
children: <Widget>[
GridView.count(
crossAxisCount: 2,
crossAxisSpacing: 10,
mainAxisSpacing: 10,
childAspectRatio: 2.4,
children: <Widget>[
GestureDetector(
child: _buildWidget("English", 0),
onTap: () => setState(() => _languageIndex = 0),
),
GestureDetector(
child: _buildWidget("Española", 1),
onTap: () => setState(() => _languageIndex = 1),
),
GestureDetector(
child: _buildWidget("Française", 2),
onTap: () => setState(() => _languageIndex = 2),
),
GestureDetector(
child: _buildWidget("Gaeilge", 3),
onTap: () => setState(() => _languageIndex = 3),
),
GestureDetector(
child: _buildWidget("العربية", 4),
onTap: () => setState(() => _languageIndex = 4),
),
GestureDetector(
child: _buildWidget("جغتای", 5),
onTap: () => setState(() => _languageIndex = 5),
),
GestureDetector(
child: _buildWidget("ગુજરાતી", 6),
onTap: () => setState(() => _languageIndex = 6),
),
GestureDetector(
child: _buildWidget("हिन्दी", 7),
onTap: () => setState(() => _languageIndex = 7),
),
GestureDetector(
child: _buildWidget("日本語", 8),
onTap: () => setState(() => _languageIndex = 8),
),
GestureDetector(
child: _buildWidget("коми", 9),
onTap: () => setState(() => _languageIndex = 9),
),
GestureDetector(
child: _buildWidget("ਲਹਿੰਦੀ", 10),
onTap: () => setState(() => _languageIndex = 10),
),
GestureDetector(
child: _buildWidget("Schwyzerdütsch", 11),
onTap: () => setState(() => _languageIndex = 11),
),
],
),
SizedBox(
width: double.maxFinite,
height: 44,
child: RaisedButton(
child: Text("SUBMIT"),
onPressed: () {
print("languageIndex = ${_languageIndex}");
},
),
),
],
),
);
}
Widget _buildWidget(String language, int index) {
bool isSelected = _languageIndex == index;
return Container(
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(6),
border: Border.all(color: isSelected ? Colors.blue[300].withOpacity(0.8) : Colors.grey[700]),
color: Colors.grey[900],
),
child: Text(
language,
style: TextStyle(fontSize: 16, color: isSelected ? Colors.blue[400] : null),
),
);
}
}
Вы можете посмотреть, какой язык пользователь выбрал, используя _languageIndex
,