Flutter 如何分别链接和查看项目?
在学习了一个关于交错边视图的教程之后,我成功地构建了一个页面来显示交错的文本网格和图标。教程没有解释如何分别链接每个网格项。我知道onTap、GestureDetector、OnPressed,但我不知道如何在这个网格布局中实现这些功能,以便每个元素都可以链接到不同的材料页面路由(或_UrlLauncher,等等)Flutter 如何分别链接和查看项目?,flutter,dart,gesturedetector,staggered-gridview,flutter-onpressed,Flutter,Dart,Gesturedetector,Staggered Gridview,Flutter Onpressed,在学习了一个关于交错边视图的教程之后,我成功地构建了一个页面来显示交错的文本网格和图标。教程没有解释如何分别链接每个网格项。我知道onTap、GestureDetector、OnPressed,但我不知道如何在这个网格布局中实现这些功能,以便每个元素都可以链接到不同的材料页面路由(或_UrlLauncher,等等) 子项:材质( 子项:staggedGridView.count( 交叉轴计数:2, 横轴间距:8, 平均间距:16, 收缩膜:对, 填充:边缘组。对称(水平:6.0,垂直:6.0),
子项:材质(
子项:staggedGridView.count(
交叉轴计数:2,
横轴间距:8,
平均间距:16,
收缩膜:对,
填充:边缘组。对称(水平:6.0,垂直:6.0),
子项:[
MyItems(Icons.shop,“此处为Tecxt”,0xff42a5f5),
MyItems(Icons.shop,“此处文本”,0xff42a5f5),
MyItems(Icons.shop,“此处文本”,0xff42a5f5),
MyItems(Icons.shop,“此处文本”,0xff42a5f5),
MyItems(Icons.shop,“此处文本”,0xff42a5f5),
MyItems(Icons.shop,“此处文本”,0xff42a5f5),
MyItems(Icons.shop,“此处文本”,0xff42a5f5),
],
交错文件:[
交错文件范围(2150.0),
交错文件范围(1150.0),
交错文件范围(1150.0),
交错文件范围(2150.0),
交错文件范围(1150.0),
交错文件范围(1150.0),
交错文件范围(2150.0),
],
),
),//材料
我们为每个“MyItems”创建了一个方法和参数:
Material MyItems(IconData icon, String heading, int color) {
return Material(color: Colors.white,
elevation: 12.0,
shadowColor: Color(0xff2962ff),
borderRadius: BorderRadius.circular(20.0),
child: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: < Widget > [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: < Widget > [
//Text here
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(heading,
style: TextStyle(
color: new Color(color),
fontSize: 20.0,
),
),
), //text
//icon
Material(
color: new Color(color),
borderRadius: BorderRadius.circular(24.0),
child: Padding(padding: const EdgeInsets.all(16.0),
child: Icon(
icon,
color: Colors.white,
size: 20.0,
),
),
),
],
),
]))),
);
材质MyItems(IConda图标、字符串标题、整型颜色){
退货材料(颜色:Colors.white,
标高:12.0,
阴影颜色:颜色(0xff2962ff),
边界半径:边界半径。圆形(20.0),
儿童:中心(
孩子:填充(
填充:常数边集全部(16.0),
孩子:排(
mainAxisAlignment:mainAxisAlignment.center,
子项:[
纵队(
mainAxisAlignment:mainAxisAlignment.center,
子项:[
//此处文本
填充物(
填充:常数边集全部(8.0),
子:文本(标题,
样式:TextStyle(
颜色:新颜色(颜色),
字体大小:20.0,
),
),
),//文本
//图标
材料(
颜色:新颜色(颜色),
边界半径:边界半径。圆形(24.0),
子项:填充(填充:常量边集全部(16.0),
子:图标(
偶像
颜色:颜色,白色,
尺寸:20.0,
),
),
),
],
),
]))),
);
似乎没有关于如何解决这个问题的任何信息。我能从作者那里找到的唯一后续信息是“有两种方法可以解决它”:
但是经过很多努力,我还是无法理解这一点。是否有可能将每个交错的网格单独链接到它们自己的唯一链接(页面路由、URL启动器等)?您可以复制粘贴运行下面的完整代码
您可以将路由名称作为字符串传递
代码片段
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: '/',
routes: {
// When navigating to the "/" route, build the FirstScreen widget.
'/': (context) => Example01(),
'/first': (context) => FirstScreen(),
'/second': (context) => SecondScreen(),
},
);
...
List<Widget> _tiles = const <Widget>[
const MyItems(Icons.shop, "Text1 Here", 0xff42a5f5, "/first"),
const _Example01Tile(Colors.green, Icons.widgets),
const _Example01Tile(Colors.amber, Icons.panorama_wide_angle),
const MyItems(Icons.shop, "Text 2 Here", 0xff42a5f5, "/second"),
...
class MyItems extends StatelessWidget {
const MyItems(this.icon, this.heading, this.color, this.routeName);
...
IconButton(
icon: Icon(icon),
iconSize: 20,
color: Colors.white,
onPressed: () {
Navigator.pushNamed(context, routeName);
},
)
return MaterialApp(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
initialRoute:“/”,
路线:{
//当导航到“/”路径时,构建FirstScreen小部件。
“/”:(上下文)=>Example01(),
“/first”:(上下文)=>FirstScreen(),
“/second”:(上下文)=>SecondScreen(),
},
);
...
列表_tiles=const[
const MyItems(Icons.shop,“Text1 Here”,0xff42a5f5,“/first”),
const_example1tile(颜色、绿色、图标、小部件),
常量示例01瓷砖(颜色、琥珀色、图标、全景、广角),
const MyItems(Icons.shop,“此处文本2”,0xff42a5f5,“/second”),
...
类MyItems扩展了无状态小部件{
const MyItems(this.icon、this.heading、this.color、this.routeName);
...
图标按钮(
图标:图标(图标),
iconSize:20,
颜色:颜色,白色,
已按下:(){
pushNamed(上下文,routeName);
},
)
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
List<StaggeredTile> _staggeredTiles = const <StaggeredTile>[
StaggeredTile.extent(2, 150.0),
StaggeredTile.extent(1, 150.0),
StaggeredTile.extent(1, 150.0),
StaggeredTile.extent(2, 150.0),
StaggeredTile.extent(1, 150.0),
StaggeredTile.extent(1, 150.0),
StaggeredTile.extent(2, 150.0),
];
List<Widget> _tiles = const <Widget>[
const MyItems(Icons.shop, "Text1 Here", 0xff42a5f5, "/first"),
const _Example01Tile(Colors.green, Icons.widgets),
const _Example01Tile(Colors.amber, Icons.panorama_wide_angle),
const MyItems(Icons.shop, "Text 2 Here", 0xff42a5f5, "/second"),
const _Example01Tile(Colors.deepOrange, Icons.send),
const _Example01Tile(Colors.indigo, Icons.airline_seat_flat),
const _Example01Tile(Colors.red, Icons.bluetooth),
const _Example01Tile(Colors.pink, Icons.battery_alert),
const _Example01Tile(Colors.purple, Icons.desktop_windows),
const _Example01Tile(Colors.blue, Icons.radio),
];
class Example01 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Example 01'),
),
body: Padding(
padding: const EdgeInsets.only(top: 12.0),
child: StaggeredGridView.count(
crossAxisCount: 4,
staggeredTiles: _staggeredTiles,
children: _tiles,
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
padding: const EdgeInsets.all(4.0),
)));
}
}
class _Example01Tile extends StatelessWidget {
const _Example01Tile(this.backgroundColor, this.iconData);
final Color backgroundColor;
final IconData iconData;
@override
Widget build(BuildContext context) {
return Card(
color: backgroundColor,
child: InkWell(
onTap: () {},
child: Center(
child: Padding(
padding: const EdgeInsets.all(4.0),
child: Icon(
iconData,
color: Colors.white,
),
),
),
),
);
}
}
class MyItems extends StatelessWidget {
const MyItems(this.icon, this.heading, this.color, this.routeName);
final int color;
final IconData icon;
final String heading;
final String routeName;
@override
Widget build(BuildContext context) {
return Material(
color: Colors.white,
elevation: 12.0,
shadowColor: Color(0xff2962ff),
borderRadius: BorderRadius.circular(20.0),
child: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//Text here
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
heading,
style: TextStyle(
color: new Color(color),
fontSize: 18.0,
),
),
), //text
//icon
Material(
color: new Color(color),
borderRadius: BorderRadius.circular(24.0),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: IconButton(
icon: Icon(icon),
iconSize: 20,
color: Colors.white,
onPressed: () {
Navigator.pushNamed(context, routeName);
},
),
),
),
],
),
]))),
);
}
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: '/',
routes: {
// When navigating to the "/" route, build the FirstScreen widget.
'/': (context) => Example01(),
'/first': (context) => FirstScreen(),
'/second': (context) => SecondScreen(),
},
);
}
}
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text("First Screen");
}
}
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text("Second Screen");
}
}
导入“包装:颤振/材料.省道”;
导入“包:颤振交错网格视图/颤振交错网格视图.省道”;
列表_交错文件=常数[
交错文件范围(2150.0),
交错文件范围(1150.0),
交错文件范围(1150.0),
交错文件范围(2150.0),
交错文件范围(1150.0),
交错文件范围(1150.0),
交错文件范围(2150.0),
];
列表_tiles=const[
const MyItems(Icons.shop,“Text1 Here”,0xff42a5f5,“/first”),
const_example1tile(颜色、绿色、图标、小部件),
常量示例01瓷砖(颜色、琥珀色、图标、全景、广角),
const MyItems(Icons.shop,“此处文本2”,0xff42a5f5,“/second”),
const_example1tile(Colors.deepOrange,Icons.send),
常数示例01
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
List<StaggeredTile> _staggeredTiles = const <StaggeredTile>[
StaggeredTile.extent(2, 150.0),
StaggeredTile.extent(1, 150.0),
StaggeredTile.extent(1, 150.0),
StaggeredTile.extent(2, 150.0),
StaggeredTile.extent(1, 150.0),
StaggeredTile.extent(1, 150.0),
StaggeredTile.extent(2, 150.0),
];
List<Widget> _tiles = const <Widget>[
const MyItems(Icons.shop, "Text1 Here", 0xff42a5f5, "/first"),
const _Example01Tile(Colors.green, Icons.widgets),
const _Example01Tile(Colors.amber, Icons.panorama_wide_angle),
const MyItems(Icons.shop, "Text 2 Here", 0xff42a5f5, "/second"),
const _Example01Tile(Colors.deepOrange, Icons.send),
const _Example01Tile(Colors.indigo, Icons.airline_seat_flat),
const _Example01Tile(Colors.red, Icons.bluetooth),
const _Example01Tile(Colors.pink, Icons.battery_alert),
const _Example01Tile(Colors.purple, Icons.desktop_windows),
const _Example01Tile(Colors.blue, Icons.radio),
];
class Example01 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Example 01'),
),
body: Padding(
padding: const EdgeInsets.only(top: 12.0),
child: StaggeredGridView.count(
crossAxisCount: 4,
staggeredTiles: _staggeredTiles,
children: _tiles,
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
padding: const EdgeInsets.all(4.0),
)));
}
}
class _Example01Tile extends StatelessWidget {
const _Example01Tile(this.backgroundColor, this.iconData);
final Color backgroundColor;
final IconData iconData;
@override
Widget build(BuildContext context) {
return Card(
color: backgroundColor,
child: InkWell(
onTap: () {},
child: Center(
child: Padding(
padding: const EdgeInsets.all(4.0),
child: Icon(
iconData,
color: Colors.white,
),
),
),
),
);
}
}
class MyItems extends StatelessWidget {
const MyItems(this.icon, this.heading, this.color, this.routeName);
final int color;
final IconData icon;
final String heading;
final String routeName;
@override
Widget build(BuildContext context) {
return Material(
color: Colors.white,
elevation: 12.0,
shadowColor: Color(0xff2962ff),
borderRadius: BorderRadius.circular(20.0),
child: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//Text here
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
heading,
style: TextStyle(
color: new Color(color),
fontSize: 18.0,
),
),
), //text
//icon
Material(
color: new Color(color),
borderRadius: BorderRadius.circular(24.0),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: IconButton(
icon: Icon(icon),
iconSize: 20,
color: Colors.white,
onPressed: () {
Navigator.pushNamed(context, routeName);
},
),
),
),
],
),
]))),
);
}
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: '/',
routes: {
// When navigating to the "/" route, build the FirstScreen widget.
'/': (context) => Example01(),
'/first': (context) => FirstScreen(),
'/second': (context) => SecondScreen(),
},
);
}
}
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text("First Screen");
}
}
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text("Second Screen");
}
}